diff --git a/src/core/utils/__test__/toArray.test.ts b/src/core/utils/__test__/toArray.test.ts new file mode 100644 index 0000000..7e3f2f9 --- /dev/null +++ b/src/core/utils/__test__/toArray.test.ts @@ -0,0 +1,16 @@ +import {toArray} from '../toArray'; + +describe('toArray', () => { + it('Должен вернуть пустой массив', () => { + expect(toArray(undefined)).toEqual([]); + expect(toArray([])).toEqual([]); + }); + + it('Должен вернуть массив', () => { + expect(toArray('hji')).toEqual(['hji']); + expect(toArray(null)).toEqual([null]); + expect(toArray(0)).toEqual([0]); + expect(toArray([0, null, 'gh'])).toEqual([0, null, 'gh']); + expect(toArray([0, [null], 'gh'])).toEqual([0, [null], 'gh']); + }); +}); diff --git a/src/core/utils/toArray.ts b/src/core/utils/toArray.ts new file mode 100644 index 0000000..bf3f8fc --- /dev/null +++ b/src/core/utils/toArray.ts @@ -0,0 +1,9 @@ +export const toArray = (value?: T | T[]): T[] => { + if (Array.isArray(value)) { + return value; + } + + return [ + ...(value !== undefined ? [value] : []) + ]; +};