Skip to content

Commit

Permalink
feat: _zip
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Jan 2, 2024
1 parent 5a00bc4 commit 8ac200d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/array/array.util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
_takeWhile,
_uniq,
_uniqBy,
_zip,
} from './array.util'
import { _range } from './range'

Expand Down Expand Up @@ -357,3 +358,15 @@ test('_maxBy, _minBy', () => {
expect(_minByOrUndefined([{ age: 18 }, { age: 30 }], u => u.age)).toEqual({ age: 18 })
expect(_minBy([{ age: 18 }, { age: 30 }], u => u.age)).toEqual({ age: 18 })
})

test('_zip', () => {
const a1 = [1, 2, 3]
const a2 = [2, 3, 4]
expect(_zip(a1, a2)).toEqual([
[1, 2],
[2, 3],
[3, 4],
])

expect(_zip(a1, a2).map(([a, b]) => a * b)).toEqual([2, 6, 12])
})
11 changes: 11 additions & 0 deletions src/array/array.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,14 @@ export function _minByOrUndefined<T>(

return minItem
}

export function _zip<T1, T2>(array1: T1[], array2: T2[]): [T1, T2][] {
const len = Math.min(array1.length, array2.length)
const res: [T1, T2][] = []

for (let i = 0; i < len; i++) {
res.push([array1[i]!, array2[i]!])
}

return res
}

0 comments on commit 8ac200d

Please sign in to comment.