Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add unzip function #64

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions benchmarks/array/unzip.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as _ from 'radashi'
import { bench } from 'vitest'

describe('unzip', () => {
bench('with non-empty arrays', () => {
_.unzip([
['a', 1, true],
['b', 2, false],
])
})
})
22 changes: 22 additions & 0 deletions docs/array/unzip.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: unzip
description: Group array elements by their index position across the input arrays
---

## Basic usage

Creates an array of ungrouped elements, where each resulting array contains all elements at a specific index from the input arrays. The first array contains all first elements, the second array contains all second elements, and so on.

```ts
import * as _ from 'radashi'

_.unzip([
['a', 1, true],
['b', 2, false],
])
// => [
// ['a', 'b'],
// [1, 2],
// [true, false],
// ]
```
25 changes: 25 additions & 0 deletions src/array/unzip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Creates an array of ungrouped elements, where each resulting array
* contains all elements at a specific index from the input arrays.
* The first array contains all first elements, the second array
* contains all second elements, and so on.
*
* ```ts
* unzip([['a', 1, true], ['b', 2, false]])
* // [['a', 'b'], [1, 2], [true, false]]
* ```
*/
export function unzip<T>(arrays: readonly (readonly T[])[]): T[][] {
if (!arrays || !arrays.length) {
return []
}
const out = new Array(
arrays.reduce((max, arr) => Math.max(max, arr.length), 0),
)
let index = 0
const get = (array: T[]) => array[index]
for (; index < out.length; index++) {
out[index] = Array.from(arrays as { length: number }, get)
}
return out
}
9 changes: 3 additions & 6 deletions src/array/zip.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { unzip } from 'radashi'

/**
* Creates an array of grouped elements, the first of which contains
* the first elements of the given arrays, the second of which
Expand Down Expand Up @@ -31,10 +33,5 @@ export function zip<T1, T2>(
array2: readonly T2[],
): [T1, T2][]
export function zip<T>(...arrays: (readonly T[])[]): T[][] {
if (!arrays || !arrays.length) {
return []
}
return new Array(Math.max(...arrays.map(({ length }) => length)))
.fill([])
.map((_, idx) => arrays.map(array => array[idx]))
return unzip(arrays)
}
1 change: 1 addition & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export * from './array/sort.ts'
export * from './array/sum.ts'
export * from './array/toggle.ts'
export * from './array/unique.ts'
export * from './array/unzip.ts'
export * from './array/zip.ts'
export * from './array/zipToObject.ts'

Expand Down
15 changes: 15 additions & 0 deletions tests/array/unzip.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as _ from 'radashi'

describe('unzip', () => {
test('unzips an array correctly', () => {
const result = _.unzip([
['a', 1, true],
['b', 2, false],
])
expect(result).toEqual([
['a', 'b'],
[1, 2],
[true, false],
])
})
})