Minimalistic utilities for modern ES/TypeScript coding (assuming es2018 and above, and ESM)
Priorities:
- Exclusively designed for modern JS/ES/TS programming, i.e., Node.js 16+ and modern browsers (post-IE era).
- Carefully typed (built for and with TypeScript).
- Zero dependencies.
Overview:
Nil
for null | undefined | NaN
.
prune
, pruneNil
, pruneEmpty
to remove properties by value (returns a new object, only direct properties).
omit
to remove properties by name (returns a new object, only direct properties).
pick(obj, ...props)
to select properties by name, returns a new object.
pruneIn
to remove properties with undefined in place (no clone).
spltest(string, delim?)
to split (default ','), trim items, and filter out empty ones.
asArr(val | vals[])
returns [val] if single value, or vals[] if already an array. Null/undefined pass through.
equal
a fast, strict, and deep equality function for objects, maps, sets, dates, regexes, and primitive types.
shortUuid
to shorten a UUID to a base 58 format (bitcoin alphabet).
toUuid
to convert from base 58 to UUID format.
encoder
to create an encode(src: string)
function from source and destination alphabets.
- All functions are nullipotent regarding arguments, except
pruneIn
.
- All functions allow null pass-through, meaning if null or undefined is passed, it returns the value passed.
- Null pass-through is appropriately typed in TS with conditional typing when needed.
Roadmap:
omitIn(obj, ...props)
to omit in place (not sure we need/want this one).
asBool(val | vals[])
to return true for 'true' or >0, false for 'false', <=0, null, undefined, or NaN.
- (only if requested)
deepPrune...
, deepOmit
.
npm install utils-min
bun add utils-min
Dev
This lib can be used with nodejs
and bunjs
for both server and browser.
However, lib uses bunjs
for development. Build and test and all.
bun test
bun --watch test
bun run build
API
const nan = parseInt('not-a-number');
isObject({});
isObject(1);
isObject([]);
isObject(nan);
isObject(null);
isObject(undefined);
isNil(null);
isNil(undefined);
isNil(nan);
isNil([]);
isNil('');
isEmpty(null);
isEmpty(undefined);
isEmpty(nan);
isEmpty([]);
isEmpty({}):
isEmpty('');
isEmpty('\n\t \r');
isEmpty(0);
isEmpty([undefined]);
let arr: any[] | undefined | null = ...;
if (isNotEmpty(arr)){
arr.length;
}
isString('hello');
isString({some: 'text'});
isString(123);
isString(['hello']);
isString(null);
isString(undefined);
isNum(123);
isNum(-1);
isNum([123]);
isNum('123');
isNum(null);
isNum(undefined);
prune({a: undefined, b: 123, c: [], d: null, e: nan});
prune({ a: undefined, b: 123, c: [], d: null, e: nan }, 123);
prune([undefined,1]);
prune([undefined]);
prune(null);
prune(undefined);
pruneNil({a: undefined, b: null, c: 0, d: [], e: '', f: nan});
pruneNil([undefined, null, 0, [], '', nan]);
pruneNil([undefined, null, 0, 1, 2, nan], 0, 1);
pruneEmpty({a: undefined, b: null, c: 0, d: [], e: ''});
pruneEmpty([undefined, null, 0, [], '']);
pruneEmpty([undefined, null, 123, [], ''], 123);
omtest({a: 1, b: 'BBB', c: 'CCC'}, 'b', 'c');
omtest({a: 1, b: 'BBB', c: 'CCC', d: null, e: undefined}, 'b', 'c');
pick({a: 1, b: 'BBB', c: 'CCC'}, 'b', 'c');
spltest('1 ,2, 3');
spltest('1 ,2,, \t,\n 3,,');
spltest('1 ;2, 3', ';');
asNum('12.5');
asNum(12.5);
asNum('12a');
asNum('12a', -1);
asNum(['12', 13, 'aa']);
asNum(['12', 13, 'aa'], -1);
asNum('');
asNum(['', ' ']);
asNum([undefined, undefined]);
asNum(null);
asNum(undefined);
asArray(1);
asArray([1, 2, 3]);
asArray(['one', 2]);
asArray({some:'text'});
asArray([undefined]);
asArray(null);
asArray(undefined);
equal(1, 1);
equal({a: 1, b: 2}, {b: 2, a: 1});
equal([1, 2], [1, 2]);
equal([1, {two: 2}], [1, {two: 2}]);
equal({a: 1, b: undefined}, {a: 1});
equal([1, 2], [2, 1]);
equal([1, 2], [1, 2, undefined]);
deepClone({a: 1, b: 'hello'});
deepClone([1, {b: 'hello'}]);
deepClone(null);
deepClone('hello');
await watest(1000);
shortUuid('2ab0968f-b122-4342-aa84-9a216dbfc6ff');
toUuid('6GkPTNGKjCKKsmYrhw1imc');
const hex_to_b58 = encoder(BASE_16_ALPHABET, BASE_58_ALPHABET);
const b58 = hex_to_b58('2ce109e9d0faf820b2434e166297934e6177b65ab9951dbc3e204cad4689b39c');
const obj = {a: 1, b: undefined, c: null};
pruneIn(obj);
const arr = [1, undefined, null];
pruneIn(arr);
Thanks to