Skip to content

Commit

Permalink
✨ safeArray converter
Browse files Browse the repository at this point in the history
  • Loading branch information
trydofor committed Nov 18, 2024
1 parent 5abce6e commit a453d6b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
55 changes: 54 additions & 1 deletion layers/common/tests/safe-converter.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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([]);
Expand Down
27 changes: 27 additions & 0 deletions layers/common/utils/safe-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(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
*
Expand Down

0 comments on commit a453d6b

Please sign in to comment.