Simple functional programming utility & programming helper tools

Simple utility functions that can use browser, node.
Function list
boolean
isFalse | If argument false, return true |
isTrue | If argument true, return true |
invert | return inverted boolean value |
array
populate | populate array zero base, if you pass second argument true that populate one base |
chunk | array split given size |
last | pick last element from array |
first | pick first element from array |
toArray | make array given argument |
settify | make it set and convert it back to array |
empty
isUndefined | if argument undefiend, return true |
isNotUndefined | if argument not undefined, return true |
isNull | if argument null, return true |
isNotNull | if argument not null, return true |
isNotEmpty | if argument not null and undefined, return true |
isEmpty | if argument null or undefined, return true |
isComplexEmpty | if argument not undefined and null, do additional test isNaN, empty string, empty array, empty object |
isNotComplexEmpty | return inverted value isComplexEmpty |
misc
typedkey | same work Object.keys, but typed key in Recoed |
getRandomRange | return random value in min and max |
getRandomRangeInt | return random integer value in min and max |
isError | if argument is Error return Error or return undefined |
sleep
sleep | sleep given millisecond |
Custom Utility Types
TResolvePromise | get type resolved promise |
TResolveArray | get type resolve array |
TNullablePick | convert specific field to nullable |
TNonNullableObject | object type each field to non nullable |
TNonNullablePick | convert specific field to non nullable |
False & True checker
Why need this utility?
import { isFalse } from 'my-easy-fp';
const iamboolean = false;
if (!iamboolean) {
console.log('I am execute false-case');
}
if (isFalse(iamboolean)) {
console.log('I am execute false-case');
}
Empty checker
Why need this utility?
const iamempty?: string | null = undefined;
if (iamempty === undefined || iamempty === null || iamempty !== '') {
console.log('i am empty');
}
if (isComplexEmpty(iamempty)) {
console.log('i am empty');
}
You need only undefined
and null
check, use double equal operator.
const iamempty?: string | null = undefined;
if (iamempty == null) {
console.log('i am empty');
}
Sleep
Why need this utility?
const ms = 1000;
await new Promise((resolve) => setTimeout(() => resolve(), ms));
await sleep(ms);
Array
populate, chunk utility here.
const seq = populate(10);
const seq = populate(10, true);
const chunked = chunk([1, 2, 3, 4, 5, 6, 7], 2);
const settified = settify([1, 1, 2, 3, 2, 3, 4]);
const zeroIndex = atOrThrow([1, 2, 3], 0);
Type Helper
resolve promise, array. But you can use type-fest or ts-essentials. Recommand use type-fest and tsessentials.
type TResolvedPromiseType = TResolvePromise<ReturnType<typeof fs.promises.readFile>>;
type TResolvedArrayType = TResolveArray<number[]>;
interface IStudent {
name: string;
major: string;
grade: number;
}
type TCliArgumentStudent = TNullablePick<IStudent, 'name' | 'major'>;