From a9eeff8d26fb38fe664a7d2866fb9444e249baaf Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 13 Oct 2022 20:29:56 +1100 Subject: [PATCH] feat(utils): extra utility methods --- lib/utils.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/lib/utils.ts b/lib/utils.ts index 19e0242..9d94479 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -25,3 +25,47 @@ export function round(value: number, precision: number): number { const multiplier = Math.pow(10, precision); return Math.round(value * multiplier) / multiplier; } + +export function isArray(value: unknown): value is T[] { + return Array.isArray(value); +} + +export function isObject(value: unknown): value is Record { + return typeof value === "object" && value !== null; +} + +export function isEmpty(value: any): boolean { + if (isArray(value)) { + return value.length === 0; + } else if (isObject(value)) { + return Object.keys(value).length === 0; + } else { + return !value; + } +} + +export function capitalize(value: string): string { + return value.charAt(0).toUpperCase() + value.slice(1); +} + +export function uppercase(value: string): string { + return value.toUpperCase(); +} + +export function lowercase(value: string): string { + return value.toLowerCase(); +} + +export function clone(value: T): T { + if (isArray(value)) { + return value.slice() as T; + } else if (isObject(value)) { + return { ...value } as T; + } else { + return value; + } +} + +export function assignOptions(defaultOptions: P, options: T): P & T { + return Object.assign({}, defaultOptions, options); +}