diff --git a/.changeset/serious-eggs-add.md b/.changeset/serious-eggs-add.md new file mode 100644 index 0000000..bc5dcb3 --- /dev/null +++ b/.changeset/serious-eggs-add.md @@ -0,0 +1,6 @@ +--- +"@fessional/razor-common": patch +"@fessional/razor-mobile": patch +--- + +✨ Maybe is null | undefined | T #102 diff --git a/layers/common/types/common.global.d.ts b/layers/common/types/common.global.d.ts index c3a6f7b..f7310d3 100644 --- a/layers/common/types/common.global.d.ts +++ b/layers/common/types/common.global.d.ts @@ -17,6 +17,11 @@ * 0 if `null` | `undefined` | `false` | `NaN` | `''` */ type NumberLike = null | undefined | boolean | number | string | bigint; + + /** + * alias to `null` | `undefined` | `T` + */ + type Maybe = null | undefined | T; } // diff --git a/layers/common/utils/safe-converter.ts b/layers/common/utils/safe-converter.ts index b0a6da0..28e5c14 100644 --- a/layers/common/utils/safe-converter.ts +++ b/layers/common/utils/safe-converter.ts @@ -33,7 +33,7 @@ export function safeJson(obj: unknown): string { * - When `false` (the default), the function will continue to be invoked until it no longer returns a function, allowing for multiple resolutions. * @returns The converted value of type `T`, or the default value if the conversion is unsuccessful. */ -export function safeConvert(valOrFun: S | (() => S), defaults: T, convert: (value: S) => T | null, once = false): T { +export function safeConvert(valOrFun: Maybe | (() => Maybe), defaults: T, convert: (value: S) => T | null, once = false): T { while (typeof valOrFun === 'function') { valOrFun = (valOrFun as () => S)(); if (once) break; @@ -149,12 +149,12 @@ export function safeBigint(valOrFun: NumberLike | (() => NumberLike), defaults: * @param defaults - The default array to return if the input is `undefined` or `null`. Defaults to an empty array (`[]`). * @returns An array of type `T[]` containing the resolved values. */ -export function safeArray(valOrFun: undefined | T | T[] | (() => undefined | T | T[]), defaults: T[] = []): T[] { +export function safeArray(valOrFun: Maybe | (() => Maybe), defaults: T[] = []): T[] { if (valOrFun == null) return defaults; if (Array.isArray(valOrFun)) return valOrFun; - if (typeof valOrFun == 'function') { - valOrFun = (valOrFun as () => undefined | T | T[])(); + if (typeof valOrFun === 'function') { + valOrFun = (valOrFun as () => Maybe)(); } if (Array.isArray(valOrFun)) { @@ -181,11 +181,11 @@ export function safeArray(valOrFun: undefined | T | T[] | (() => undefined | * @param defaults - The default value to return when the input is `undefined` or `null`. * @returns The resolved safe value of type `T`. */ -export function safeValue(valOrFun: undefined | T | T[] | (() => undefined | T | T[]), defaults: T): T { +export function safeValue(valOrFun: Maybe | (() => Maybe), defaults: T): T { if (valOrFun == null) return defaults; - if (typeof valOrFun == 'function') { - valOrFun = (valOrFun as () => undefined | T | T[])(); + if (typeof valOrFun === 'function') { + valOrFun = (valOrFun as () => Maybe)(); } if (Array.isArray(valOrFun)) { @@ -234,27 +234,27 @@ export function safeEntries(valOrFun: any, defaults: [string, T][] = []): [st /** * convert object to Map */ -export function safeObjMap(obj?: Record | null): Map { +export function safeObjMap(obj?: Maybe>): Map { return obj == null ? new Map() : new Map(Object.entries(obj)); } /** * convert array to Set */ -export function safeArrSet(arr?: T[] | null): Set { +export function safeArrSet(arr?: Maybe): Set { return arr == null ? new Set() : new Set(arr); } /** * convert Map to object */ -export function safeMapObj(map?: Map | null): Record { +export function safeMapObj(map?: Maybe>): Record { return map == null ? {} : Object.fromEntries(map); } /** * convert Set to array */ -export function safeSetArr(set?: Set | null): T[] { +export function safeSetArr(set?: Maybe>): T[] { return set == null ? [] : Array.from(set); }