devlin-utils
v0.1.0
Published
A general-purpose utility library for strings, arrays, objects, and dates — with a CLI.
Maintainers
Readme
corekit
A general-purpose utility library for strings, arrays, objects, and dates — with a built-in CLI.
TypeScript-first. Zero dependencies. Works in Node.js and the browser (ESM + CJS dual output).
Installation
npm install devlin-utilsUsage
As a library
import { strings, arrays, objects, dates } from "devlin-utils";
strings.slugify("Hello World!"); // "hello-world"
arrays.chunk([1, 2, 3, 4, 5], 2); // [[1,2],[3,4],[5]]
objects.pick({ a:1, b:2, c:3 }, ["a","c"]); // { a:1, c:3 }
dates.timeAgo(new Date("2024-01-01")); // "X months ago"Or import modules directly:
import { slugify, toCamelCase } from "devlin-utils/strings";
import { chunk, groupBy } from "devlin-utils/arrays";
import { deepMerge, omit } from "devlin-utils/objects";
import { formatDate, diffDates } from "devlin-utils/dates";API
devlin-utils/strings
| Function | Description |
|---|---|
| capitalize(str) | Capitalize first letter |
| toCamelCase(str) | Convert to camelCase |
| toKebabCase(str) | Convert to kebab-case |
| toSnakeCase(str) | Convert to snake_case |
| truncate(str, max, ellipsis?) | Truncate with ellipsis |
| slugify(str) | URL-safe slug |
| countOccurrences(str, sub) | Count substring occurrences |
| centerPad(str, width, char?) | Center-pad a string |
| isEmail(str) | Validate email format |
| stripWhitespace(str) | Remove all whitespace |
| reverseString(str) | Reverse a string |
devlin-utils/arrays
| Function | Description |
|---|---|
| unique(arr) | Remove duplicates |
| chunk(arr, size) | Split into chunks |
| flatten(arr, deep?) | Flatten nested arrays |
| intersection(a, b) | Common elements |
| difference(a, b) | Elements in a not in b |
| shuffle(arr) | Fisher-Yates shuffle |
| groupBy(arr, keyFn) | Group by key function |
| sum(arr) | Sum of numbers |
| minMax(arr) | { min, max } of numbers |
| sample(arr) | Random element |
| range(start, end, step?) | Generate number array |
devlin-utils/objects
| Function | Description |
|---|---|
| deepClone(obj) | Deep clone via JSON |
| deepMerge(target, source) | Recursive merge |
| pick(obj, keys) | Pick keys |
| omit(obj, keys) | Omit keys |
| flattenObject(obj) | Dot-notation flatten |
| isPlainObject(val) | Plain object check |
| isEmpty(obj) | Empty object check |
| invertObject(obj) | Swap keys/values |
| getByPath(obj, path) | Get value by dot path |
devlin-utils/dates
| Function | Description |
|---|---|
| formatDate(date, template) | Format with YYYY MM DD HH mm ss |
| addToDate(date, n, unit) | Add days/months/years |
| diffDates(a, b, unit) | Difference between dates |
| isBetween(date, start, end) | Range check |
| isSameDay(a, b) | Same calendar day |
| timeAgo(date, now?) | Relative time string |
| startOf(date, unit) | Start of day/month/year |
| isValidDate(val) | Valid Date check |
CLI
npx devlin-utils --help
# Strings
devlin-utils capitalize "hello world"
devlin-utils slugify "Hello World!"
devlin-utils camel "hello-world"
devlin-utils kebab "helloWorld"
# Arrays
devlin-utils chunk 3 a b c d e f
devlin-utils unique a b a c b d
devlin-utils range 1 10
devlin-utils range 0 20 5
# Dates
devlin-utils format 2024-03-15 YYYY/MM/DD
devlin-utils timeago 2024-01-01Development
npm install
npm run build # compile to /dist
npm test # run all tests
npm run test:watch # watch mode
npm run dev # build in watch modeLicense
MIT
