Skip to content

NiketaJain/js-powerkit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JS PowerKit

npm version License: MIT Build Status

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-powerkit

Quick 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'); // true

mask(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!'); // 2

removeDuplicates(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>'); // '&lt;div&gt;Test &amp; &quot;quotes&quot;&lt;/div&gt;'

isNumeric(str)

Checks if a string contains only numbers.

isNumeric('12345'); // true

isEmail(str)

Checks if a string is a valid email.

isEmail('test@example.com'); // true

isUrl(str)

Checks if a string is a valid URL.

isUrl('https://example.com'); // true

repeatString(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]); // 9

min(arr)

Finds the minimum value in an array.

min([1, 5, 3, 9, 2]); // 1

sum(arr)

Calculates the sum of array elements.

sum([1, 2, 3, 4, 5]); // 15

average(arr)

Calculates the average of array elements.

average([1, 2, 3, 4, 5]); // 3

countOccurrences(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]); // false

includesAny(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]); // false

rotate(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 obj

deepMerge(...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}); // false

invert(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); // 42

getAllKeys(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 }); // true

filterObject(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({}); // 0

has(obj, prop)

Checks if an object has a property.

has({a: 1}, 'a'); // true
has({a: 1}, 'b'); // false

hasPath(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'); // false

Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Testing

Run the test suite:

npm test

Run tests with coverage:

npm run test:coverage

License

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.