-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compat): implement unzip (#770)
* feat(compat): implement unzip * jsdoc * make lint happy --------- Co-authored-by: Sojin Park <[email protected]>
- Loading branch information
Showing
4 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { unzip } from './unzip'; | ||
import { zip } from '../../array/zip'; | ||
|
||
describe('unzip', () => { | ||
const object = { | ||
'an empty array': [[], []], | ||
'2-tuples': [ | ||
[ | ||
['barney', 'fred'], | ||
[36, 40], | ||
], | ||
[ | ||
['barney', 36], | ||
['fred', 40], | ||
], | ||
], | ||
'3-tuples': [ | ||
[ | ||
['barney', 'fred'], | ||
[36, 40], | ||
[false, true], | ||
], | ||
[ | ||
['barney', 36, false], | ||
['fred', 40, true], | ||
], | ||
], | ||
}; | ||
|
||
Object.entries(object).forEach(([key, pair]) => { | ||
it(`\`_.unzip\` should work with ${key}`, () => { | ||
const actual = unzip(pair[1]); | ||
expect(actual).toEqual(pair[0]); | ||
}); | ||
}); | ||
|
||
it(`\`_.unzip\` should work with tuples of different lengths`, () => { | ||
const pair = [ | ||
[ | ||
['barney', 36], | ||
['fred', 40, false], | ||
], | ||
[ | ||
['barney', 'fred'], | ||
[36, 40], | ||
[undefined, false], | ||
], | ||
]; | ||
const actual = unzip(pair[1]) as any; | ||
expect('2' in actual[0]).toBeTruthy(); | ||
expect(actual).toEqual([ | ||
['barney', 36, undefined], | ||
['fred', 40, false], | ||
]); | ||
}); | ||
|
||
it(`\`_.unzip\` should support consuming its return value`, () => { | ||
const expected = [ | ||
['barney', 'fred'], | ||
[36, 40], | ||
]; | ||
// @ts-expect-error - TS doesn't support array types in rest parameters | ||
expect(unzip(zip(...unzip(zip(...expected))))).toEqual(expected); | ||
}); | ||
|
||
it(`\`_.unzip\` should work with array-like object`, () => { | ||
const array = { 0: { 0: 'a', 1: 1, length: 2 }, 1: { 0: 'b', 1: 2, length: 2 }, length: 2 }; | ||
const actual = unzip(array); | ||
expect(actual).toEqual([ | ||
['a', 'b'], | ||
[1, 2], | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { unzip as unzipToolkit } from '../../array/unzip.ts'; | ||
import { isArrayLikeObject } from '../predicate/isArrayLikeObject.ts'; | ||
|
||
/** | ||
* Gathers elements in the same position in an internal array | ||
* from a grouped array of elements and returns them as a new array. | ||
* | ||
* @template T - The type of elements in the nested array. | ||
* @param {T[][] | ArrayLike<ArrayLike<T>> | null | undefined} array - The nested array to unzip. | ||
* @returns {T[][]} A new array of unzipped elements. | ||
* | ||
* @example | ||
* const zipped = [['a', true, 1],['b', false, 2]]; | ||
* const result = unzip(zipped); | ||
* // result will be [['a', 'b'], [true, false], [1, 2]] | ||
*/ | ||
export function unzip<T>(array: T[][] | ArrayLike<ArrayLike<T>> | null | undefined): T[][] { | ||
if (!isArrayLikeObject(array) || !array.length) { | ||
return []; | ||
} | ||
if (Array.isArray(array)) { | ||
return unzipToolkit(array); | ||
} | ||
return unzipToolkit(Array.from(array, value => Array.from(value))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters