mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-23 01:28:01 +00:00
Initial rebrand: @polkadot -> @pezkuwi (14 packages)
- Package namespace: @polkadot/* -> @pezkuwi/* - Repository: polkadot-js/common -> pezkuwichain/pezkuwi-common - Author: Pezkuwi Team <team@pezkuwichain.io> Core packages: - @pezkuwi/util (utilities) - @pezkuwi/util-crypto (crypto primitives) - @pezkuwi/keyring (account management) - @pezkuwi/networks (chain metadata) - @pezkuwi/hw-ledger (Ledger hardware wallet) - @pezkuwi/x-* (10 polyfill packages) Total: 14 packages Upstream: polkadot-js/common v14.0.1
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { arrayChunk, arrayRange } from '../index.js';
|
||||
import { perf } from '../test/index.js';
|
||||
|
||||
describe('arrayChunk', (): void => {
|
||||
it('chunks with exact', (): void => {
|
||||
expect(
|
||||
arrayChunk([1, 2, 3, 4, 5, 6, 7, 8], 8)
|
||||
).toEqual([[1, 2, 3, 4, 5, 6, 7, 8]]);
|
||||
});
|
||||
|
||||
it('chunks with unequal', (): void => {
|
||||
expect(
|
||||
arrayChunk([1, 2, 3, 4, 5, 6, 7], 3)
|
||||
).toEqual([[1, 2, 3], [4, 5, 6], [7]]);
|
||||
});
|
||||
|
||||
it('chunks with non-empty results', (): void => {
|
||||
expect(
|
||||
arrayChunk([[1, 2], [3, 4], [5, 6], [7, 8]], 2)
|
||||
).toEqual([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
|
||||
});
|
||||
|
||||
perf('arrayChunk', 200_000, [[arrayRange(500), 50]], arrayChunk);
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name arrayChunk
|
||||
* @summary Split T[] into T[][] based on the defind size
|
||||
* @description
|
||||
* Returns a set ao arrays based on the chunksize
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { arrayChunk } from '@pezkuwi/util';
|
||||
*
|
||||
* arrayChunk([1, 2, 3, 4, 5]); // [[1, 2], [3, 4], [5]]
|
||||
* ```
|
||||
*/
|
||||
export function arrayChunk <T> (array: T[], chunkSize: number): T[][] {
|
||||
const outputSize = Math.ceil(array.length / chunkSize);
|
||||
|
||||
// shortcut for the single-split case
|
||||
if (outputSize === 1) {
|
||||
return [array];
|
||||
}
|
||||
|
||||
const output = Array<T[]>(outputSize);
|
||||
|
||||
for (let i = 0; i < outputSize; i++) {
|
||||
const offset = i * chunkSize;
|
||||
|
||||
output[i] = array.slice(offset, offset + chunkSize);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { arrayFilter } from './index.js';
|
||||
|
||||
describe('filterArray', (): void => {
|
||||
it('filters arrays, removing undefined', (): void => {
|
||||
expect(
|
||||
arrayFilter([0, '', null, false, undefined, NaN])
|
||||
).toEqual([0, '', null, false, NaN]);
|
||||
});
|
||||
|
||||
it('filters arrays, removing undefined & null (allowNull = false)', (): void => {
|
||||
expect(
|
||||
arrayFilter([0, '', null, false, undefined, NaN], false)
|
||||
).toEqual([0, '', false, NaN]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name arrayFilter
|
||||
* @summary Filters undefined and (optionally) null values from an array
|
||||
* @description
|
||||
* Returns a new array with all `undefined` values removed. Optionally, when `allowNulls = false`, it removes the `null` values as well
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { arrayFilter } from '@pezkuwi/util';
|
||||
*
|
||||
* arrayFilter([0, void 0, true, null, false, '']); // [0, true, null, false, '']
|
||||
* arrayFilter([0, void 0, true, null, false, ''], false); // [0, true, false, '']
|
||||
* ```
|
||||
*/
|
||||
export function arrayFilter <T = unknown> (array: readonly (T | null | undefined)[], allowNulls = true): T[] {
|
||||
return array.filter((v): v is T =>
|
||||
v !== undefined &&
|
||||
(allowNulls || v !== null)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { perf } from '../test/index.js';
|
||||
import { arrayFlatten } from './index.js';
|
||||
|
||||
const PERF_ONE = [[1, 2, 3, 4, 5]];
|
||||
const PERF_MUL = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
|
||||
|
||||
describe('arrayFlatten', (): void => {
|
||||
it('flattens arrays', (): void => {
|
||||
expect(
|
||||
arrayFlatten([[0], [1, 2, 3], [4, 5], [6], [], [7, 8]])
|
||||
).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8]);
|
||||
});
|
||||
|
||||
it('flattens a single entry', (): void => {
|
||||
expect(
|
||||
arrayFlatten([[1, 2, 3, 4, 5]])
|
||||
).toEqual([1, 2, 3, 4, 5]);
|
||||
});
|
||||
|
||||
it('flattens an empty', (): void => {
|
||||
expect(
|
||||
arrayFlatten([])
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
perf('arrayFlatten (single)', 10_000_000, [[PERF_ONE]], arrayFlatten);
|
||||
perf('arrayFlatten (multi)', 700_000, [[PERF_MUL]], arrayFlatten);
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// This is supposed to be a faster concat...
|
||||
// https://dev.to/uilicious/javascript-array-push-is-945x-faster-than-array-concat-1oki
|
||||
|
||||
/**
|
||||
* @name arrayFlatten
|
||||
* @summary Merge T[][] into T[]
|
||||
* @description
|
||||
* Returns a new array with all arrays merged into one
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { arrayFlatten } from '@pezkuwi/util';
|
||||
*
|
||||
* arrayFlatten([[1, 2], [3, 4], [5]]); // [1, 2, 3, 4, 5]
|
||||
* ```
|
||||
*/
|
||||
export function arrayFlatten <T> (arrays: readonly T[][]): T[] {
|
||||
const num = arrays.length;
|
||||
|
||||
// shortcuts for the empty & single-entry case
|
||||
if (num === 0) {
|
||||
return [];
|
||||
} else if (num === 1) {
|
||||
return arrays[0];
|
||||
}
|
||||
|
||||
// pre-allocate based on the combined size
|
||||
let size = 0;
|
||||
|
||||
for (let i = 0; i < num; i++) {
|
||||
size += arrays[i].length;
|
||||
}
|
||||
|
||||
const output = new Array<T>(size);
|
||||
let i = -1;
|
||||
|
||||
for (let j = 0; j < num; j++) {
|
||||
const a = arrays[j];
|
||||
|
||||
// instead of pushing, we just set the entries
|
||||
for (let e = 0, count = a.length; e < count; e++) {
|
||||
output[++i] = a[e];
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @summary Utility methods that operates on arrays
|
||||
*/
|
||||
|
||||
export { arrayChunk } from './chunk.js';
|
||||
export { arrayFilter } from './filter.js';
|
||||
export { arrayFlatten } from './flatten.js';
|
||||
export { arrayRange } from './range.js';
|
||||
export { arrayShuffle } from './shuffle.js';
|
||||
export { arrayUnzip } from './unzip.js';
|
||||
export { arrayZip } from './zip.js';
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { perf } from '../test/index.js';
|
||||
import { arrayRange } from './index.js';
|
||||
|
||||
describe('arrayRange', (): void => {
|
||||
it('does not allow 0 values', (): void => {
|
||||
expect(
|
||||
() => arrayRange(0)
|
||||
).toThrow(/Expected non-zero, positive number as a range size/);
|
||||
});
|
||||
|
||||
it('creates a range of the specified length', (): void => {
|
||||
expect(
|
||||
arrayRange(10)
|
||||
).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
});
|
||||
|
||||
it('creates a range of the specified length, with offset', (): void => {
|
||||
expect(
|
||||
arrayRange(7, 3)
|
||||
).toEqual([3, 4, 5, 6, 7, 8, 9]);
|
||||
});
|
||||
|
||||
perf('arrayRange (100 entries)', 1_000_000, [[100]], arrayRange);
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name arrayRange
|
||||
* @summary Returns a range of numbers ith the size and the specified offset
|
||||
* @description
|
||||
* Returns a new array of numbers with the specific size. Optionally, when `startAt`, is provided, it generates the range to start at a specific value.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { arrayRange } from '@pezkuwi/util';
|
||||
*
|
||||
* arrayRange(5); // [0, 1, 2, 3, 4]
|
||||
* arrayRange(3, 5); // [5, 6, 7]
|
||||
* ```
|
||||
*/
|
||||
export function arrayRange (size: number, startAt = 0): number[] {
|
||||
if (size <= 0) {
|
||||
throw new Error('Expected non-zero, positive number as a range size');
|
||||
}
|
||||
|
||||
const result = new Array<number>(size);
|
||||
|
||||
for (let i = 0; i < size; i++) {
|
||||
result[i] = i + startAt;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { stringify } from '../stringify.js';
|
||||
import { perf } from '../test/index.js';
|
||||
import { arrayRange, arrayShuffle } from './index.js';
|
||||
|
||||
const ptest = arrayRange(16284);
|
||||
|
||||
describe('arrayShuffle', (): void => {
|
||||
it('returns an empty array as-is', (): void => {
|
||||
expect(
|
||||
arrayShuffle([])
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it('returns a single array as-is', (): void => {
|
||||
expect(
|
||||
arrayShuffle([100])
|
||||
).toEqual([100]);
|
||||
});
|
||||
|
||||
it('shuffles an array', (): void => {
|
||||
const inp = arrayRange(100);
|
||||
const out = arrayShuffle(inp);
|
||||
|
||||
expect(inp).toHaveLength(out.length);
|
||||
expect(
|
||||
inp.filter((v) => !out.includes(v))
|
||||
).toEqual([]);
|
||||
expect(
|
||||
stringify(inp)
|
||||
).not.toEqual(stringify(out));
|
||||
});
|
||||
|
||||
perf('arrayShuffle', 1000, [[ptest]], arrayShuffle);
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name arrayShuffle
|
||||
* @description Shuffles the input array (unlike sort, this is not done in-place)
|
||||
*/
|
||||
export function arrayShuffle <T> (input: readonly T[]): T[] {
|
||||
const result = input.slice();
|
||||
let curr = result.length;
|
||||
|
||||
// noop for the single entry
|
||||
if (curr === 1) {
|
||||
return result;
|
||||
}
|
||||
|
||||
while (curr !== 0) {
|
||||
// ~~ is more performant than Math.floor
|
||||
const rand = ~~(Math.random() * curr);
|
||||
|
||||
curr--;
|
||||
|
||||
[result[curr], result[rand]] = [result[rand], result[curr]];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { arrayUnzip } from './index.js';
|
||||
|
||||
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,18 @@
|
||||
// Copyright 2017-2025 @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 count = entries.length;
|
||||
const keys = new Array<K>(count);
|
||||
const values = new Array<V>(count);
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
[keys[i], values[i]] = entries[i];
|
||||
}
|
||||
|
||||
return [keys, values];
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { arrayZip } from './index.js';
|
||||
|
||||
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,17 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name arrayZip
|
||||
* @description Combines 2 distinct key/value arrays into a single [K, V] array
|
||||
*/
|
||||
export function arrayZip <K, V> (keys: readonly K[], values: readonly V[]): [K, V][] {
|
||||
const count = keys.length;
|
||||
const result = new Array<[K, V]>(count);
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
result[i] = [keys[i], values[i]];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user