* arrayZip

* Update packages/util/src/array/zip.spec.ts

* Update packages/util/src/array/zip.ts
This commit is contained in:
Jaco
2021-10-30 18:45:20 +03:00
committed by GitHub
parent 1158551b9e
commit 134b8c3b9f
4 changed files with 38 additions and 0 deletions
+1
View File
@@ -8,6 +8,7 @@ Contributed:
Changes:
- Add `arrayZip` to combine a key + value array into entries-like
- Assert `chainCode` inputs to `derive*`, protecting against misuse
- Adjust testnet detection in `@polkadot/networks`
- Bump Ledger libraries to latest versions
+1
View File
@@ -10,3 +10,4 @@ export { arrayFilter } from './filter';
export { arrayFlatten } from './flatten';
export { arrayRange } from './range';
export { arrayShuffle } from './shuffle';
export { arrayZip } from './zip';
+24
View File
@@ -0,0 +1,24 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { arrayZip } from '.';
describe('arrayZip', (): void => {
it('zips a simple one', (): void => {
expect(
arrayZip(['a', 'b', 'c'], [1, 2, 3])
).toEqual([['a', 1], ['b', 2], ['c', 3]]);
});
it('zips where values > keys', (): void => {
expect(
arrayZip(['a', 'b', 'c'], [1, 2, 3, 4])
).toEqual([['a', 1], ['b', 2], ['c', 3]]);
});
it('zips where values < keys', (): void => {
expect(
arrayZip(['a', 'b', 'c'], [1, 2])
).toEqual([['a', 1], ['b', 2], ['c', undefined]]);
});
});
+12
View File
@@ -0,0 +1,12 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
export function arrayZip <K, V> (keys: K[], values: V[]): [K, V][] {
const result = new Array<[K, V]>(keys.length);
for (let i = 0; i < keys.length; i++) {
result[i] = [keys[i], values[i]];
}
return result;
}