A comprehensive JavaScript utility library providing essential functions for strings, arrays, and objects. Built with modern ES6+ syntax and designed for performance and ease of use.
- String Utilities: Capitalization, case conversion, truncation, palindrome checking, and more
- Array Utilities: Chunking, flattening, unique filtering, sorting, grouping, and set operations
- Object Utilities: Deep cloning, merging, property picking/omitting, and transformation
- TypeScript Support: Full type definitions included
- Zero Dependencies: Lightweight and fast
- ES6+ Modules: Modern JavaScript with tree-shaking support
npm install js-powerkitimport { capitalize, chunk, deepClone } from 'js-powerkit';
// String utilities
console.log(capitalize('hello world')); // 'Hello world'
// Array utilities
console.log(chunk([1, 2, 3, 4, 5], 2)); // [[1, 2], [3, 4], [5]]
// Object utilities
const obj = { a: 1, b: { c: 2 } };
const cloned = deepClone(obj);
console.log(cloned); // { a: 1, b: { c: 2 } }Capitalizes the first letter of a string.
capitalize('hello world'); // 'Hello world'Converts a string to camelCase.
toCamelCase('hello world'); // 'helloWorld'Converts a string to kebab-case.
toKebabCase('Hello World'); // 'hello-world'Converts a string to snake_case.
toSnakeCase('Hello World'); // 'hello_world'Converts a string to PascalCase.
toPascalCase('hello world'); // 'HelloWorld'Converts a string to TitleCase.
toTitleCase('hello world'); // 'Hello World'Reverses a string.
reverse('hello'); // 'olleh'Truncates a string to a specified length with an optional suffix.
truncate('Hello World', 5); // 'He...'
truncate('Hello World', 5, '***'); // 'He***'
truncate('Hello World', 5, '***', false); // 'Hello'Checks if a string is a palindrome (case-insensitive, ignores non-alphanumeric).
isPalindrome('racecar'); // true
isPalindrome('A man a plan a canal Panama'); // trueMasks a string (useful for sensitive data).
mask('1234567890', 2); // '12******90'
mask('1234567890', 2, '.'); // '12......90'Converts a string to a URL-friendly slug.
slugify('Hello World!'); // 'hello-world'Counts the number of words in a string.
countWords('Hello world!'); // 2Removes duplicate characters from a string.
removeDuplicates('hello'); // 'helo'Removes all whitespace from a string.
removeWhitespace(' hello world '); // 'helloworld'Extracts all email addresses from a string.
extractEmails('Contact us at test@example.com or support@test.org'); // ['test@example.com', 'support@test.org']Extracts all URLs addresses from a string.
extractUrls('Visit https://example.com or http://test.org'); // ['https://example.com', 'http://test.org']Removes HTML tags from a string.
stripHtml('<p>Hello <b>World</b></p>'); // 'Hello World'Escapes HTML special characters.
escapeHtml('<div>Test & "quotes"</div>'); // '<div>Test & "quotes"</div>'Checks if a string contains only numbers.
isNumeric('12345'); // trueChecks if a string is a valid email.
isEmail('test@example.com'); // trueChecks if a string is a valid URL.
isUrl('https://example.com'); // trueRepeats a string n times
repeatString('ab', 3); // 'ababab'Splits an array into chunks of specified size.
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]Flattens a nested array to a specified depth.
flatten([1, [2, [3, 4]], 5]); // [1, 2, [3, 4], 5]
flatten([1, [2, [3, 4]], 5], 2); // [1, 2, 3, 4, 5]Removes duplicate elements from an array.
unique([1, 2, 2, 3, 4, 4]); // [1, 2, 3, 4]Shuffles the elements of an array randomly.
shuffle([1, 2, 3, 4]); // [3, 1, 4, 2] (random order)Gets a random element from an array.
randomElement([1, 2, 3, 4, 5]); // 5 (random element)Removes falsy values from an array.
compact([0, 1, false, 2, '', 3, null, undefined, NaN]); // [1, 2, 3]Sorts an array of objects by a property.
sortBy([{a: 2}, {a: 1}], 'a'); // [{a: 1}, {a: 2}]
sortBy([{a: 2}, {a: 1}], 'a', 'desc'); // [{a: 2}, {a: 1}]Groups an array of objects by a property.
groupBy([{type: 'a', val: 1}, {type: 'b', val: 2}, {type: 'a', val: 3}], 'type');
// {a: [{type: 'a', val: 1}, {type: 'a', val: 3}], b: [{type: 'b', val: 2}]}Returns the intersection of two arrays.
intersection([1, 2, 3], [2, 3, 4]); // [2, 3]Returns the difference of two arrays.
difference([1, 2, 3], [2, 3, 4]); // [1]Returns the union of two arrays.
union([1, 2, 3], [2, 3, 4]); // [1, 2, 3, 4]Zips multiple arrays together.
zip([1, 2], ['a', 'b'], [true, false]); // [[1, 'a', true], [2, 'b', false]]Creates an array of numbers from start to end.
range(1, 5); // [1, 2, 3, 4, 5]
range(0, 10, 2); // [0, 2, 4, 6, 8, 10]Finds the maximum value in an array.
max([1, 5, 3, 9, 2]); // 9Finds the minimum value in an array.
min([1, 5, 3, 9, 2]); // 1Calculates the sum of array elements.
sum([1, 2, 3, 4, 5]); // 15Calculates the average of array elements.
average([1, 2, 3, 4, 5]); // 3Counts occurrences of each element.
countOccurrences([1, 2, 2, 3, 3, 3]); // { '1': 1, '2': 2, '3': 3 }Removes elements from array by value.
remove([1, 2, 3, 2, 4], 2); // [1, 3, 4]Takes first n elements from array.
take([10, 21, 31, 41, 51], 4); // [10, 21, 31, 41]Drops first n elements from array.
drop([1, 2, 3, 4, 5], 2); // [3, 4, 5]Checks if array includes all values.
includesAll([1, 2, 3, 4], [2, 3]); // true
includesAll([1, 2, 3], [2, 5]); // falseChecks if array includes any of the values.
includesAny([1, 2, 3], [3, 4, 5]); // true
includesAny([1, 2, 3], [4, 5, 6]); // falseRotates array elements.
rotate([1, 2, 3, 4, 5], 2); // [3, 4, 5, 1, 2]
rotate([1, 2, 3, 4, 5], -2); // [4, 5, 1, 2, 3]Creates a deep clone of an object.
const obj = { a: 1, b: { c: 2 } };
const cloned = deepClone(obj);
// cloned is a deep copy of objMerges multiple objects into one.
deepMerge({a: 1}, {b: 2}); // {a: 1, b: 2}
deepMerge({a: {b: 1}}, {a: {c: 2}}); // {a: {b: 1, c: 2}}Picks specified properties from an object.
pick({a: 1, b: 2, c: 3}, ['a', 'c']); // {a: 1, c: 3}Omits specified properties from an object.
omit({a: 1, b: 2, c: 3}, ['b']); // {a: 1, c: 3}Checks if an object is empty.
isEmpty({}); // true
isEmpty({a: 1}); // falseInverts the keys and values of an object.
invert({a: 1, b: 2}); // {1: 'a', 2: 'b'}Maps the keys of an object using a function.
mapKeys({a: 1, b: 2}, key => key.toUpperCase()); // {A: 1, B: 2}Maps the values of an object using a function.
mapValues({a: 1, b: 2}, val => val * 2); // {a: 2, b: 4}Sets default values for an object.
defaults({a: 1}, {a: 2, b: 3}); // {a: 1, b: 3}Gets a nested property value using dot notation.
getNestedValue({ a: { b: { c: 42 } } }, 'a.b.c'); // 42
getNestedValue({ a: { b: { c: 42 } } }, 'a.c', 'default'); // 'default'Sets a nested property value using dot notation.
let obj = { a: { b: 1 } };
setNestedValue(obj, 'a.c.d', 42);
console.log(obj.a.c.d); // 42Gets all keys from nested object.
getAllKeys({ a: 1, b: { c: 2, d: { e: 3 } } }); // ['a', 'b.c', 'b.d.e']Flattens a nested object.
flattenObject({ a: 1, b: { c: 2, d: { e: 3 } } }); // {'a': 1, 'b.c': 2, 'b.d.e': 3 }Unflattens a flattened object.
unflattenObject({ 'a': 1, 'b.c': 2, 'b.d.e': 3 }); // { a: 1, b: { c: 2, d: { e: 3 } } }Removes null and undefined values from object.
removeNullish({ a: 1, b: null, c: undefined, d: 0, e: '' }); // { a: 1, d: 0, e: '' }Checks if two objects are deeply equal.
isEqual({ a: 1 }, { a: 1 }); // trueFilters object by predicate function.
filterObject({ a: 1, b: 2, c: 3, d: 4 }, val => val % 2 === 0); // { b: 2, d: 4 }Converts object to query string.
toQueryString({ name: 'John', age: 30 }); // 'name=John&age=30'Converts query string to object.
fromQueryString('name=John&age=30'); // { name: 'John', age: 30 }Gets object size (number of properties).
size({ a: 1, b: 2, c: 3 }); // 3
size({}); // 0Checks if an object has a property.
has({a: 1}, 'a'); // true
has({a: 1}, 'b'); // falseChecks if object has a nested property.
hasPath({ a: { b: { c: 1 } } }, 'a.b.c'); // true
hasPath({ a: { b: { c: 1 } } }, 'a.b.d'); // falseWe welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Run the test suite:
npm testRun tests with coverage:
npm run test:coverageThis project is licensed under the MIT License - see the LICENSE file for details.
- Initial release with comprehensive string, array, and object utilities
- Full test coverage
- ES6+ module support
If you find this library helpful, please give it a ⭐ on GitHub!
For issues or questions, please open an issue on GitHub.