@arnonsang/utils
v0.0.3
Published
A collection of utility functions
Readme
My Utils Package
Utils package focus on web development utilities.
Installation
npm install @arnonsang/utilsUsage
Importing Utilities
import {
logger,
charAt,
charAtFrom,
capitalize,
toCamelCase,
unique,
chunk,
formatDate,
dateDiff,
clamp,
formatBytes
} from "@arnonsang/utils";Logger Utility
logger.info("This is an info message");
logger.warn("This is a warning message");
logger.error("An error occurred");
logger.success("Operation successful");
logger.debug("Debugging info");
logger.table([{ name: "Alice" }, { name: "Bob" }], "User Data");Character Utilities
console.log(charAt(3, "upper")); // "D"
console.log(charAtFrom("hello", 1)); // "e"String Utilities
capitalize("hello world"); // "Hello world"
toCamelCase("hello world"); // "helloWorld"
toKebabCase("Hello World"); // "hello-world"
toSnakeCase("Hello World"); // "hello_world"
truncate("This is a long string", 10); // "This is..."
cleanWhitespace(" hello world "); // "hello world"
isValidEmail("[email protected]"); // true
randomString(8); // "aB3xY9pQ"
pluralize("item", 5); // "items"Array Utilities
unique([1, 2, 2, 3, 3, 4]); // [1, 2, 3, 4]
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
flatten([[1, 2], [3, 4]]); // [1, 2, 3, 4]
groupBy([{type: 'a'}, {type: 'b'}, {type: 'a'}], item => item.type);
shuffle([1, 2, 3, 4, 5]); // [3, 1, 5, 2, 4] (random)
sample([1, 2, 3, 4, 5]); // 3 (random)
intersection([1, 2, 3], [2, 3, 4]); // [2, 3]
sum([1, 2, 3, 4, 5]); // 15
average([1, 2, 3, 4, 5]); // 3Date Utilities
formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss');
dateDiff(date1, date2, 'days'); // 5
addTime(new Date(), 5, 'days');
subtractTime(new Date(), 2, 'hours');
isToday(new Date()); // true
isPast(someDate); // true/false
getAge(new Date('1990-01-01')); // 34 (example)Number Utilities
clamp(15, 0, 10); // 10
round(3.14159, 2); // 3.14
randomBetween(1, 10); // 7.234 (random)
randomInt(1, 10); // 7 (random)
isEven(4); // true
percentage(25, 100); // 25
formatNumber(1234567); // "1,234,567"
formatBytes(1024); // "1 KB"
factorial(5); // 120Contributing
Feel free to open an issue or a pull request to contribute new utilities or improve existing ones.
License
This project is licensed under the MIT License.
