arrayZip (#1150)
* arrayZip * Update packages/util/src/array/zip.spec.ts * Update packages/util/src/array/zip.ts
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -10,3 +10,4 @@ export { arrayFilter } from './filter';
|
||||
export { arrayFlatten } from './flatten';
|
||||
export { arrayRange } from './range';
|
||||
export { arrayShuffle } from './shuffle';
|
||||
export { arrayZip } from './zip';
|
||||
|
||||
@@ -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]]);
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user