Package Exports
- js-powerkit
- js-powerkit/src/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (js-powerkit) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
JS PowerKit
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.
Features
- 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
Installation
npm install js-powerkitQuick Start
import { 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 } }API Documentation
String Utilities
capitalize(str)
Capitalizes the first letter of a string.
capitalize('hello world'); // 'Hello world'toCamelCase(str)
Converts a string to camelCase.
toCamelCase('hello world'); // 'helloWorld'toKebabCase(str)
Converts a string to kebab-case.
toKebabCase('Hello World'); // 'hello-world'toSnakeCase(str)
Converts a string to snake_case.
toSnakeCase('Hello World'); // 'hello_world'toPascalCase(str)
Converts a string to PascalCase.
toPascalCase('hello world'); // 'HelloWorld'toTitleCase(str)
Converts a string to TitleCase.
toTitleCase('hello world'); // 'Hello World'reverse(str)
Reverses a string.
reverse('hello'); // 'olleh'truncate(str, length, suffix, addSuffix)
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'isPalindrome(str)
Checks if a string is a palindrome (case-insensitive, ignores non-alphanumeric).
isPalindrome('racecar'); // true
isPalindrome('A man a plan a canal Panama'); // truemask(str, visibleChars, maskChar)
Masks a string (useful for sensitive data).
mask('1234567890', 2); // '12******90'
mask('1234567890', 2, '.'); // '12......90'slugify(str)
Converts a string to a URL-friendly slug.
slugify('Hello World!'); // 'hello-world'countWords(str)
Counts the number of words in a string.
countWords('Hello world!'); // 2removeDuplicates(str)
Removes duplicate characters from a string.
removeDuplicates('hello'); // 'helo'removeWhitespace(str)
Removes all whitespace from a string.
removeWhitespace(' hello world '); // 'helloworld'extractEmails(str)
Extracts all email addresses from a string.
extractEmails('Contact us at test@example.com or support@test.org'); // ['test@example.com', 'support@test.org']extractUrls(str)
Extracts all URLs addresses from a string.
extractUrls('Visit https://example.com or http://test.org'); // ['https://example.com', 'http://test.org']stripHtml(str)
Removes HTML tags from a string.
stripHtml('<p>Hello <b>World</b></p>'); // 'Hello World'escapeHtml(str)
Escapes HTML special characters.
escapeHtml('<div>Test & "quotes"</div>'); // '<div>Test & "quotes"</div>'isNumeric(str)
Checks if a string contains only numbers.
isNumeric('12345'); // trueisEmail(str)
Checks if a string is a valid email.
isEmail('test@example.com'); // trueisUrl(str)
Checks if a string is a valid URL.
isUrl('https://example.com'); // truerepeatString(str, times)
Repeats a string n times
repeatString('ab', 3); // 'ababab'Array Utilities
chunk(arr, size)
Splits an array into chunks of specified size.
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]flatten(arr, depth)
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]unique(arr)
Removes duplicate elements from an array.
unique([1, 2, 2, 3, 4, 4]); // [1, 2, 3, 4]shuffle(arr)
Shuffles the elements of an array randomly.
shuffle([1, 2, 3, 4]); // [3, 1, 4, 2] (random order)randomElement(arr)
Gets a random element from an array.
randomElement([1, 2, 3, 4, 5]); // 5 (random element)compact(arr)
Removes falsy values from an array.
compact([0, 1, false, 2, '', 3, null, undefined, NaN]); // [1, 2, 3]sortBy(arr, prop, order)
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}]groupBy(arr, prop)
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}]}intersection(arr1, arr2)
Returns the intersection of two arrays.
intersection([1, 2, 3], [2, 3, 4]); // [2, 3]difference(arr1, arr2)
Returns the difference of two arrays.
difference([1, 2, 3], [2, 3, 4]); // [1]union(arr1, arr2)
Returns the union of two arrays.
union([1, 2, 3], [2, 3, 4]); // [1, 2, 3, 4]zip(...arrays)
Zips multiple arrays together.
zip([1, 2], ['a', 'b'], [true, false]); // [[1, 'a', true], [2, 'b', false]]range(start, end, step)
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]max(arr)
Finds the maximum value in an array.
max([1, 5, 3, 9, 2]); // 9min(arr)
Finds the minimum value in an array.
min([1, 5, 3, 9, 2]); // 1sum(arr)
Calculates the sum of array elements.
sum([1, 2, 3, 4, 5]); // 15average(arr)
Calculates the average of array elements.
average([1, 2, 3, 4, 5]); // 3countOccurrences(arr)
Counts occurrences of each element.
countOccurrences([1, 2, 2, 3, 3, 3]); // { '1': 1, '2': 2, '3': 3 }remove(arr, value)
Removes elements from array by value.
remove([1, 2, 3, 2, 4], 2); // [1, 3, 4]take(arr, n)
Takes first n elements from array.
take([10, 21, 31, 41, 51], 4); // [10, 21, 31, 41]drop(arr, n)
Drops first n elements from array.
drop([1, 2, 3, 4, 5], 2); // [3, 4, 5]includesAll(arr, values)
Checks if array includes all values.
includesAll([1, 2, 3, 4], [2, 3]); // true
includesAll([1, 2, 3], [2, 5]); // falseincludesAny(arr, values)
Checks if array includes any of the values.
includesAny([1, 2, 3], [3, 4, 5]); // true
includesAny([1, 2, 3], [4, 5, 6]); // falserotate(arr, positions)
Rotates 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]Object Utilities
deepClone(obj)
Creates a deep clone of an object.
const obj = { a: 1, b: { c: 2 } };
const cloned = deepClone(obj);
// cloned is a deep copy of objdeepMerge(...objects)
Merges 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}}pick(obj, keys)
Picks specified properties from an object.
pick({a: 1, b: 2, c: 3}, ['a', 'c']); // {a: 1, c: 3}omit(obj, keys)
Omits specified properties from an object.
omit({a: 1, b: 2, c: 3}, ['b']); // {a: 1, c: 3}isEmpty(obj)
Checks if an object is empty.
isEmpty({}); // true
isEmpty({a: 1}); // falseinvert(obj)
Inverts the keys and values of an object.
invert({a: 1, b: 2}); // {1: 'a', 2: 'b'}mapKeys(obj, fn)
Maps the keys of an object using a function.
mapKeys({a: 1, b: 2}, key => key.toUpperCase()); // {A: 1, B: 2}mapValues(obj, fn)
Maps the values of an object using a function.
mapValues({a: 1, b: 2}, val => val * 2); // {a: 2, b: 4}defaults(obj, defaults)
Sets default values for an object.
defaults({a: 1}, {a: 2, b: 3}); // {a: 1, b: 3}getNestedValue(obj, path, defaultValue)
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'setNestedValue(obj, path, value)
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); // 42getAllKeys(obj, prefix)
Gets all keys from nested object.
getAllKeys({ a: 1, b: { c: 2, d: { e: 3 } } }); // ['a', 'b.c', 'b.d.e']flattenObject(obj, prefix)
Flattens a nested object.
flattenObject({ a: 1, b: { c: 2, d: { e: 3 } } }); // {'a': 1, 'b.c': 2, 'b.d.e': 3 }unflattenObject(obj, prefix)
Unflattens a flattened object.
unflattenObject({ 'a': 1, 'b.c': 2, 'b.d.e': 3 }); // { a: 1, b: { c: 2, d: { e: 3 } } }removeNullish(obj)
Removes null and undefined values from object.
removeNullish({ a: 1, b: null, c: undefined, d: 0, e: '' }); // { a: 1, d: 0, e: '' }isEqual(obj1, obj2)
Checks if two objects are deeply equal.
isEqual({ a: 1 }, { a: 1 }); // truefilterObject(obj, fn)
Filters object by predicate function.
filterObject({ a: 1, b: 2, c: 3, d: 4 }, val => val % 2 === 0); // { b: 2, d: 4 }toQueryString(obj)
Converts object to query string.
toQueryString({ name: 'John', age: 30 }); // 'name=John&age=30'fromQueryString(queryString)
Converts query string to object.
fromQueryString('name=John&age=30'); // { name: 'John', age: 30 }size(obj)
Gets object size (number of properties).
size({ a: 1, b: 2, c: 3 }); // 3
size({}); // 0has(obj, prop)
Checks if an object has a property.
has({a: 1}, 'a'); // true
has({a: 1}, 'b'); // falsehasPath(obj, path)
Checks if object has a nested property.
hasPath({ a: { b: { c: 1 } } }, 'a.b.c'); // true
hasPath({ a: { b: { c: 1 } } }, 'a.b.d'); // falseContributing
We 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
Testing
Run the test suite:
npm testRun tests with coverage:
npm run test:coverageLicense
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
[1.0.1] - 2026-01-22
- Initial release with comprehensive string, array, and object utilities
- Full test coverage
- ES6+ module support
Support
If you find this library helpful, please give it a ⭐ on GitHub!
For issues or questions, please open an issue on GitHub.