Add arrayUnzip (#1598)
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
# CHANGELOG
|
||||
|
||||
## master
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `arrayUnzip` as reverse of `arrayZip`
|
||||
|
||||
|
||||
## 10.0.2 Jul 10, 2022
|
||||
|
||||
Changes:
|
||||
|
||||
+2
-2
@@ -34,8 +34,8 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.18.6",
|
||||
"@polkadot/dev": "^0.67.78",
|
||||
"@types/jest": "^28.1.5"
|
||||
"@polkadot/dev": "^0.67.80",
|
||||
"@types/jest": "^28.1.6"
|
||||
},
|
||||
"resolutions": {
|
||||
"typescript": "^4.7.4"
|
||||
|
||||
@@ -10,4 +10,5 @@ export { arrayFilter } from './filter';
|
||||
export { arrayFlatten } from './flatten';
|
||||
export { arrayRange } from './range';
|
||||
export { arrayShuffle } from './shuffle';
|
||||
export { arrayUnzip } from './unzip';
|
||||
export { arrayZip } from './zip';
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* @name arrayShuffle
|
||||
* @description Shuffles the input array (unlike sort, this is not done in-place)
|
||||
*/
|
||||
export function arrayShuffle <T> (input: T[]): T[] {
|
||||
export function arrayShuffle <T> (input: readonly T[]): T[] {
|
||||
const result = input.slice();
|
||||
let curr = result.length;
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { arrayUnzip } from '.';
|
||||
|
||||
describe('arrayUnzip', (): void => {
|
||||
it('unzips entries', (): void => {
|
||||
expect(
|
||||
arrayUnzip([['a', 1], ['b', 2], ['c', 3]])
|
||||
).toEqual([['a', 'b', 'c'], [1, 2, 3]]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name arrayUnzip
|
||||
* @description Splits a single [K, V][] into [K[], V[]]
|
||||
*/
|
||||
export function arrayUnzip <K, V> (entries: readonly [K, V][]): [K[], V[]] {
|
||||
const keys = new Array<K>(entries.length);
|
||||
const values = new Array<V>(entries.length);
|
||||
|
||||
for (let i = 0; i < entries.length; i++) {
|
||||
[keys[i], values[i]] = entries[i];
|
||||
}
|
||||
|
||||
return [keys, values];
|
||||
}
|
||||
@@ -12,11 +12,11 @@ export { packageInfo } from './packageInfo';
|
||||
const importFetch = import('node-fetch').catch(() => null);
|
||||
|
||||
// keep track of the resolved import value
|
||||
let resolvedFetch: typeof fetch | null = null;
|
||||
let modFn: typeof fetch | null = null;
|
||||
|
||||
async function nodeFetch (...args: Parameters<typeof fetch>): Promise<Response> {
|
||||
if (resolvedFetch) {
|
||||
return resolvedFetch(...args);
|
||||
if (modFn) {
|
||||
return modFn(...args);
|
||||
}
|
||||
|
||||
const mod = await importFetch;
|
||||
@@ -25,9 +25,9 @@ async function nodeFetch (...args: Parameters<typeof fetch>): Promise<Response>
|
||||
throw new Error('Unable to import node-fetch in this environment');
|
||||
}
|
||||
|
||||
resolvedFetch = mod.default as unknown as typeof fetch;
|
||||
modFn = mod.default as unknown as typeof fetch;
|
||||
|
||||
return resolvedFetch(...args);
|
||||
return modFn(...args);
|
||||
}
|
||||
|
||||
export const fetch = extractGlobal('fetch', nodeFetch);
|
||||
|
||||
@@ -2045,9 +2045,9 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@polkadot/dev@npm:^0.67.78":
|
||||
version: 0.67.78
|
||||
resolution: "@polkadot/dev@npm:0.67.78"
|
||||
"@polkadot/dev@npm:^0.67.80":
|
||||
version: 0.67.80
|
||||
resolution: "@polkadot/dev@npm:0.67.80"
|
||||
dependencies:
|
||||
"@babel/cli": ^7.18.6
|
||||
"@babel/core": ^7.18.6
|
||||
@@ -2109,7 +2109,7 @@ __metadata:
|
||||
mkdirp: ^1.0.4
|
||||
prettier: ^2.7.1
|
||||
rimraf: ^3.0.2
|
||||
rollup: ^2.76.0
|
||||
rollup: ^2.77.0
|
||||
rollup-plugin-cleanup: ^3.2.1
|
||||
typescript: ^4.7.4
|
||||
yargs: ^17.5.1
|
||||
@@ -2136,7 +2136,7 @@ __metadata:
|
||||
polkadot-exec-rollup: scripts/polkadot-exec-rollup.mjs
|
||||
polkadot-exec-tsc: scripts/polkadot-exec-tsc.mjs
|
||||
polkadot-exec-webpack: scripts/polkadot-exec-webpack.mjs
|
||||
checksum: 0e9f7e93fd15303cf289713d33542d83211fc37eb2439c8b7483cd64cbeaf2a2c215f42453f61bf915761cf239310a6a357045babfbc5513dbeb7fa91be61bcb
|
||||
checksum: 54fad512768ed57457b0238e78e9d54bc3e71ce5bc04a4b9a9e233aba8417fa01cf9acc0c0b46873c51a68aa533cc74162a0b5f041309ce8043df8891bc091b7
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@@ -2641,13 +2641,13 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/jest@npm:^28.1.5":
|
||||
version: 28.1.5
|
||||
resolution: "@types/jest@npm:28.1.5"
|
||||
"@types/jest@npm:^28.1.6":
|
||||
version: 28.1.6
|
||||
resolution: "@types/jest@npm:28.1.6"
|
||||
dependencies:
|
||||
jest-matcher-utils: ^28.0.0
|
||||
pretty-format: ^28.0.0
|
||||
checksum: 994bfc25a5e767ec1506a2a7d94e60a7a5645b2e4e8444c56ae902f3a3e27ae54e821e41c1b4e7c8d7f5022bf5abfdd0460ead223ba6f2d0a194e5b2bed1ad76
|
||||
checksum: f2ba5fbefc8f44d1c16ee19d8d2811bca75754a2846e222287f2788d96062801c568215e6b81eb532a48e8cb2a7282729da1d4f6fb496831da8269c5abaad4c5
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@@ -9428,9 +9428,9 @@ resolve@^2.0.0-next.3:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"rollup@npm:^2.76.0":
|
||||
version: 2.76.0
|
||||
resolution: "rollup@npm:2.76.0"
|
||||
"rollup@npm:^2.77.0":
|
||||
version: 2.77.0
|
||||
resolution: "rollup@npm:2.77.0"
|
||||
dependencies:
|
||||
fsevents: ~2.3.2
|
||||
dependenciesMeta:
|
||||
@@ -9438,7 +9438,7 @@ resolve@^2.0.0-next.3:
|
||||
optional: true
|
||||
bin:
|
||||
rollup: dist/bin/rollup
|
||||
checksum: 58293e1c63c11d4afcfcf619601d5c5136dd3d0c9d3bd6a0b6141fede32027edc1eb53873bbb9a9c1e95e86c67f6ad66185720031b6eadf325972174d1d8fbcb
|
||||
checksum: 74ccc98429709984a89af636250833e7502d87f1d9c6d96ebfe4b52030ebf94b9f6b84b8ab476670329a61d54b681d35eecdc601bac5b5396b099b1ea69970ef
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@@ -9447,8 +9447,8 @@ resolve@^2.0.0-next.3:
|
||||
resolution: "root-workspace-0b6124@workspace:."
|
||||
dependencies:
|
||||
"@babel/core": ^7.18.6
|
||||
"@polkadot/dev": ^0.67.78
|
||||
"@types/jest": ^28.1.5
|
||||
"@polkadot/dev": ^0.67.80
|
||||
"@types/jest": ^28.1.6
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
|
||||
Reference in New Issue
Block a user