From a453d6b9c72328e4f21190ad50f29e4376e843f7 Mon Sep 17 00:00:00 2001 From: trydofor Date: Mon, 18 Nov 2024 15:06:41 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20safeArray=20converter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- layers/common/tests/safe-converter.test.ts | 55 +++++++++++++++++++++- layers/common/utils/safe-converter.ts | 27 +++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/layers/common/tests/safe-converter.test.ts b/layers/common/tests/safe-converter.test.ts index 58b5741..45bafaf 100644 --- a/layers/common/tests/safe-converter.test.ts +++ b/layers/common/tests/safe-converter.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from 'vitest'; -import { safeString, safeNumber, safeBigint, safeValues, safeKeys, safeEntries, safeJson, safeObjMap, safeArrSet, safeMapObj, safeSetArr } from '../utils/safe-converter'; +import { safeString, safeNumber, safeBigint, safeValues, safeKeys, safeEntries, safeJson, safeObjMap, safeArrSet, safeMapObj, safeSetArr, safeArray } from '../utils/safe-converter'; describe('safeString', () => { it('should return defaults if null or undefined', () => { @@ -88,6 +88,59 @@ describe('safeBigint', () => { }); }); +describe('safeArray', () => { + it('should return the default array if input is null', () => { + const result = safeArray(null, [1, 2, 3]); + expect(result).toEqual([1, 2, 3]); + }); + + it('should return the default array if input is undefined', () => { + const result = safeArray(undefined, [1, 2, 3]); + expect(result).toEqual([1, 2, 3]); + }); + + it('should return the input array if input is already an array', () => { + const input = [4, 5, 6]; + const result = safeArray(input); + expect(result).toEqual(input); + }); + + it('should return an array wrapping a single non-array value', () => { + const result = safeArray('test'); + expect(result).toEqual(['test']); + }); + + it('should handle functions that return arrays', () => { + const result = safeArray(() => [7, 8, 9]); + expect(result).toEqual([7, 8, 9]); + }); + + it('should handle functions that return non-array values', () => { + const result = safeArray(() => 'dynamic'); + expect(result).toEqual(['dynamic']); + }); + + it('should handle nested functions and unwrap the result correctly', () => { + const result = safeArray(() => () => [10, 11, 12]); + expect(result).toEqual([10, 11, 12]); + }); + + it('should wrap non-array, non-function values in an array', () => { + const result = safeArray(42); + expect(result).toEqual([42]); + }); + + it('should return an empty array when defaults are not provided and input is null', () => { + const result = safeArray(null); + expect(result).toEqual([]); + }); + + it('should return an empty array when defaults are not provided and input is undefined', () => { + const result = safeArray(undefined); + expect(result).toEqual([]); + }); +}); + describe('safeValues', () => { it('should return defaults if null or undefined', () => { expect(safeValues(null)).toEqual([]); diff --git a/layers/common/utils/safe-converter.ts b/layers/common/utils/safe-converter.ts index c1d9e8e..2a4122d 100644 --- a/layers/common/utils/safe-converter.ts +++ b/layers/common/utils/safe-converter.ts @@ -126,6 +126,33 @@ export function safeBigint(valOrFun: NumberLike | (() => NumberLike), defaults: }); } +/** + * Ensures that the input is converted to an array. If the input is `null` or `undefined`, + * it returns the provided default array. + * + * @template T - The type of elements in the array. + * @param valOrFun - The input value, which can be: + * - An array: returned as-is. + * - A function: the result of the function is recursively passed into `safeArray`. + * - Any other value: wrapped in an array. + * - `null` or `undefined`: returns the `defaults` array. + * @param defaults - The default array to return when `valOrFun` is `null` or `undefined`. + * Defaults to an empty array. + * @returns An array containing the processed input or the default array. + */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export function safeArray(valOrFun: any, defaults: T[] = []) { + if (valOrFun == null) return defaults; + if (Array.isArray(valOrFun)) return valOrFun; + + if (typeof valOrFun == 'function') { + return safeArray(valOrFun(), defaults); + } + else { + return [valOrFun]; + } +} + /** * get non-null Object.values *