@amitdhoju/js-utils
v0.0.2
Published
String, number, array, and object utilities missing from native JavaScript
Downloads
12
Maintainers
Readme
@amitdhoju/js-utils
A collection of string, number, array, and object utilities missing from native JavaScript.
- Zero dependencies
- Dual ESM + CJS — works in Node.js, bundlers, and modern browsers
- Fully typed — ships with
.d.tsdeclarations - Tree-shakeable — import only what you use
Installation
npm install @amitdhoju/js-utilsUsage
import { capitalize, chunk, deepMerge } from "@amitdhoju/js-utils";String
capitalize(str)
Capitalizes the first letter of a string.
capitalize("hello world") // → "Hello world"truncate(str, maxLength, suffix?)
Truncates a string to a max length, appending a suffix (default "...") if cut.
truncate("Hello World", 7) // → "Hell..."
truncate("Hello World", 8, "…") // → "Hello W…"camelToKebab(str)
Converts camelCase to kebab-case.
camelToKebab("myVariableName") // → "my-variable-name"kebabToCamel(str)
Converts kebab-case to camelCase.
kebabToCamel("my-variable-name") // → "myVariableName"slugify(str)
Converts a string to a URL-friendly slug.
slugify("Hello, World! 2024") // → "hello-world-2024"countOccurrences(str, substring)
Counts non-overlapping occurrences of a substring.
countOccurrences("banana", "an") // → 2isPalindrome(str)
Returns true if the string is a palindrome (ignores case and non-alphanumeric characters).
isPalindrome("racecar") // → true
isPalindrome("A man a plan a canal Panama") // → true
isPalindrome("hello") // → falsepadCenter(str, width, fill?)
Pads both sides of a string to center it within a given width (default fill " ").
padCenter("hi", 10) // → " hi "
padCenter("hi", 10, "*") // → "****hi****"Number
clamp(value, min, max)
Clamps a number between a minimum and maximum.
clamp(15, 0, 10) // → 10
clamp(-5, 0, 10) // → 0
clamp(5, 0, 10) // → 5randomInt(min, max)
Returns a random integer between min and max (both inclusive).
randomInt(1, 6) // → 4roundTo(value, decimals)
Rounds a number to a specified number of decimal places.
roundTo(3.14159, 2) // → 3.14
roundTo(1000.5, 0) // → 1001isEven(n) / isOdd(n)
Checks whether a number is even or odd.
isEven(4) // → true
isOdd(7) // → truelerp(start, end, t)
Linearly interpolates between two values. t should be between 0 and 1.
lerp(0, 100, 0) // → 0
lerp(0, 100, 0.5) // → 50
lerp(0, 100, 1) // → 100toOrdinal(n)
Converts a number to its ordinal string.
toOrdinal(1) // → "1st"
toOrdinal(2) // → "2nd"
toOrdinal(3) // → "3rd"
toOrdinal(11) // → "11th"
toOrdinal(21) // → "21st"isFiniteNumber(value)
Type-safe check that a value is a finite number (not NaN, not Infinity).
isFiniteNumber(42) // → true
isFiniteNumber(NaN) // → false
isFiniteNumber(Infinity) // → false
isFiniteNumber("42") // → falseArray
chunk(arr, size)
Splits an array into chunks of a given size.
chunk([1, 2, 3, 4, 5], 2) // → [[1, 2], [3, 4], [5]]unique(arr)
Returns a new array with duplicates removed (first occurrence wins).
unique([1, 2, 2, 3, 1]) // → [1, 2, 3]groupBy(arr, keyFn)
Groups array elements by a derived key.
const items = [
{ type: "fruit", name: "apple" },
{ type: "veg", name: "carrot" },
{ type: "fruit", name: "banana" },
];
groupBy(items, (x) => x.type);
// → { fruit: [{...}, {...}], veg: [{...}] }intersection(a, b)
Returns elements present in both arrays.
intersection([1, 2, 3], [2, 3, 4]) // → [2, 3]difference(a, b)
Returns elements in a that are not in b.
difference([1, 2, 3], [2, 3]) // → [1]shuffle(arr)
Returns a new randomly shuffled array (Fisher-Yates). Does not mutate the original.
shuffle([1, 2, 3, 4, 5]) // → [3, 1, 5, 2, 4] (random)range(start, end, step?)
Generates an array of numbers from start (inclusive) to end (exclusive).
range(0, 5) // → [0, 1, 2, 3, 4]
range(0, 10, 2) // → [0, 2, 4, 6, 8]
range(5, 0, -1) // → [5, 4, 3, 2, 1]sum(arr)
Returns the sum of all numbers in an array.
sum([1, 2, 3, 4, 5]) // → 15average(arr)
Returns the arithmetic mean of all numbers in an array.
average([1, 2, 3, 4, 5]) // → 3Object
deepMerge(target, source)
Deep merges two objects. Source values overwrite target values recursively.
deepMerge({ a: { b: 1 } }, { a: { c: 2 } })
// → { a: { b: 1, c: 2 } }pick(obj, keys)
Returns a new object containing only the specified keys.
pick({ a: 1, b: 2, c: 3 }, ["a", "c"]) // → { a: 1, c: 3 }omit(obj, keys)
Returns a new object with the specified keys removed.
omit({ a: 1, b: 2, c: 3 }, ["b"]) // → { a: 1, c: 3 }deepClone(value)
Deep clones any value using the native structuredClone API.
const clone = deepClone({ a: { b: 1 } });
clone.a.b = 99; // original is unaffectedisEmpty(obj)
Returns true if an object has no own enumerable properties.
isEmpty({}) // → true
isEmpty({ a: 1 }) // → falseflattenObject(obj)
Flattens a nested object into dot-notation keys.
flattenObject({ a: { b: { c: 1 } }, d: 2 })
// → { "a.b.c": 1, "d": 2 }invertObject(obj)
Swaps the keys and values of a flat string object.
invertObject({ a: "x", b: "y" }) // → { x: "a", y: "b" }Development
npm install # install dependencies
npm test # run tests (Vitest)
npm run build # build ESM + CJS output (tsup)
npm run lint # lint and format check (Biome)
npm run example # run the demo scriptAdding a changeset before releasing
npx changeset # describe your change + pick semver bump
npm run version # apply version bump + update CHANGELOG.md
git add . && git commit -m "release: vX.Y.Z"
npm run release # build + publish to npmLicense
MIT
