npmtestforazizozenir
v1.0.0
Published
A collection of useful utility functions for strings, numbers, dates, and arrays
Maintainers
Readme
npmtest
A collection of useful utility functions for strings, numbers, dates, and arrays.
Installation
npm install npmtest
# or
pnpm install npmtest
# or
yarn add npmtestUsage
Import all utilities
import utils from 'npmtest';
// String utilities
utils.slugify('Hello World!'); // 'hello-world'
utils.capitalize('hello'); // 'Hello'
utils.truncate('This is a long string', 10); // 'This is a...'
// Number utilities
utils.formatNumber(1234567); // '1,234,567'
utils.randomNumber(1, 100); // Random number between 1 and 100
utils.clamp(150, 0, 100); // 100
// Date utilities
utils.formatDate(new Date(), 'YYYY-MM-DD'); // '2024-01-15'
utils.getRelativeTime(new Date(Date.now() - 3600000)); // '1 hour ago'
utils.isToday(new Date()); // true
// Array utilities
utils.chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
utils.shuffle([1, 2, 3, 4, 5]); // [3, 1, 5, 2, 4] (random)
utils.unique([1, 2, 2, 3, 3, 3]); // [1, 2, 3]
utils.randomItem([1, 2, 3, 4, 5]); // Random item from array
// General utilities
await utils.delay(1000); // Wait 1 second
utils.deepClone({ a: 1, b: { c: 2 } }); // Deep cloned object
utils.isEmpty(''); // trueImport specific utilities
import { slugify, capitalize, formatNumber, chunk } from 'npmtest';
slugify('Hello World!'); // 'hello-world'
capitalize('hello'); // 'Hello'
formatNumber(1234567); // '1,234,567'
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]API Reference
String Utilities
slugify(str)- Converts a string to a URL-friendly slugcapitalize(str)- Capitalizes the first letter of a stringtruncate(str, length, suffix?)- Truncates a string to a specified lengthremoveWhitespace(str)- Removes all whitespace from a stringreverse(str)- Reverses a string
Number Utilities
formatNumber(num, separator?)- Formats a number with thousand separatorsrandomNumber(min, max)- Generates a random number between min and maxclamp(num, min, max)- Clamps a number between min and maxisEven(num)- Checks if a number is evenisOdd(num)- Checks if a number is odd
Date Utilities
formatDate(date, format?)- Formats a date to a readable stringgetRelativeTime(date)- Gets relative time string (e.g., "2 hours ago")isToday(date)- Checks if a date is today
Array Utilities
chunk(array, size)- Splits an array into chunks of specified sizeshuffle(array)- Shuffles an array randomlyunique(array)- Removes duplicate values from an arrayrandomItem(array)- Gets a random item from an arraygroupBy(array, keyFn)- Groups array items by a key function
General Utilities
delay(ms)- Delays execution for specified milliseconds (returns Promise)deepClone(obj)- Deep clones an objectisEmpty(value)- Checks if a value is empty
Examples
String Manipulation
import { slugify, capitalize, truncate } from 'npmtest';
slugify('Hello World!'); // 'hello-world'
capitalize('hello world'); // 'Hello world'
truncate('This is a very long string', 15); // 'This is a very...'Number Formatting
import { formatNumber, randomNumber, clamp } from 'npmtest';
formatNumber(1234567.89); // '1,234,567.89'
randomNumber(1, 10); // Random number between 1-10
clamp(150, 0, 100); // 100Date Operations
import { formatDate, getRelativeTime, isToday } from 'npmtest';
formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss');
getRelativeTime(new Date(Date.now() - 7200000)); // '2 hours ago'
isToday(new Date()); // trueArray Operations
import { chunk, shuffle, unique, groupBy } from 'npmtest';
chunk([1, 2, 3, 4, 5, 6], 2); // [[1, 2], [3, 4], [5, 6]]
shuffle([1, 2, 3, 4, 5]); // Randomly shuffled array
unique([1, 2, 2, 3, 3, 3]); // [1, 2, 3]
const users = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 25 }
];
groupBy(users, user => user.age);
// { 25: [{ name: 'Alice', age: 25 }, { name: 'Charlie', age: 25 }], 30: [{ name: 'Bob', age: 30 }] }License
ISC
