Skip to content

Commit

Permalink
fix(array): toArray - convert to empty array only for nullish (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
huynl-96 committed Oct 31, 2022
1 parent 7570fb0 commit 4d9373e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
19 changes: 17 additions & 2 deletions 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([])
Expand Down
6 changes: 2 additions & 4 deletions src/array.ts
Expand Up @@ -7,10 +7,8 @@ import type { Arrayable, Nullable } from './types'
* @category Array
*/
export function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T> {
array = array || []
if (Array.isArray(array))
return array
return [array]
array = array ?? []
return Array.isArray(array) ? array : [array]
}

/**
Expand Down

0 comments on commit 4d9373e

Please sign in to comment.