diff --git a/src/array.test.ts b/src/array.test.ts index 4308d81..c569b38 100644 --- a/src/array.test.ts +++ b/src/array.test.ts @@ -1,5 +1,20 @@ -import { expect, it } from 'vitest' -import { flattenArrayable, partition, range } from './array' +import { describe, expect, it } from 'vitest' +import { flattenArrayable, partition, range, toArray } from './array' + +describe('toArray', () => { + it.each([ + [undefined, []], + [null, []], + [false, [false]], + [0, [0]], + ['', ['']], + [[], []], + ['foo', ['foo']], + [['foo'], ['foo']], + ])('%s => %s', (input, expected) => { + expect(toArray(input)).toEqual(expected) + }) +}) it('flattenArrayable', () => { expect(flattenArrayable()).toEqual([]) diff --git a/src/array.ts b/src/array.ts index cbd215c..173bfc8 100644 --- a/src/array.ts +++ b/src/array.ts @@ -7,10 +7,8 @@ import type { Arrayable, Nullable } from './types' * @category Array */ export function toArray(array?: Nullable>): Array { - array = array || [] - if (Array.isArray(array)) - return array - return [array] + array = array ?? [] + return Array.isArray(array) ? array : [array] } /**