@pezkuwi/util 14.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -0
- package/package.json +49 -0
- package/src/array/chunk.spec.ts +29 -0
- package/src/array/chunk.ts +35 -0
- package/src/array/filter.spec.ts +20 -0
- package/src/array/filter.ts +24 -0
- package/src/array/flatten.spec.ts +33 -0
- package/src/array/flatten.ts +51 -0
- package/src/array/index.ts +14 -0
- package/src/array/range.spec.ts +29 -0
- package/src/array/range.ts +31 -0
- package/src/array/shuffle.spec.ts +39 -0
- package/src/array/shuffle.ts +27 -0
- package/src/array/unzip.spec.ts +14 -0
- package/src/array/unzip.ts +18 -0
- package/src/array/zip.spec.ts +26 -0
- package/src/array/zip.ts +17 -0
- package/src/assert.spec.ts +40 -0
- package/src/assert.ts +50 -0
- package/src/bi/consts.ts +112 -0
- package/src/bi/helpers.ts +23 -0
- package/src/bi/index.ts +16 -0
- package/src/bi/max.spec.ts +32 -0
- package/src/bi/min.spec.ts +26 -0
- package/src/bi/min.ts +16 -0
- package/src/bi/sqrt.spec.ts +59 -0
- package/src/bi/sqrt.ts +43 -0
- package/src/bi/toBigInt.spec.ts +69 -0
- package/src/bi/toBigInt.ts +33 -0
- package/src/bi/toHex.spec.ts +56 -0
- package/src/bi/toHex.ts +16 -0
- package/src/bi/toU8a.spec.ts +99 -0
- package/src/bi/toU8a.ts +72 -0
- package/src/bn/bn.ts +6 -0
- package/src/bn/consts.spec.ts +38 -0
- package/src/bn/consts.ts +112 -0
- package/src/bn/fromHex.spec.ts +15 -0
- package/src/bn/fromHex.ts +4 -0
- package/src/bn/index.ts +18 -0
- package/src/bn/max.spec.ts +32 -0
- package/src/bn/min.spec.ts +26 -0
- package/src/bn/min.ts +36 -0
- package/src/bn/sqrt.spec.ts +37 -0
- package/src/bn/sqrt.ts +50 -0
- package/src/bn/toBn.spec.ts +53 -0
- package/src/bn/toBn.ts +43 -0
- package/src/bn/toHex.spec.ts +50 -0
- package/src/bn/toHex.ts +27 -0
- package/src/bn/toU8a.spec.ts +60 -0
- package/src/bn/toU8a.ts +45 -0
- package/src/buffer/index.ts +8 -0
- package/src/buffer/toU8a.spec.ts +20 -0
- package/src/buffer/toU8a.ts +20 -0
- package/src/bundle.ts +35 -0
- package/src/compact/addLength.spec.ts +14 -0
- package/src/compact/addLength.ts +24 -0
- package/src/compact/defaults.ts +7 -0
- package/src/compact/fromU8a.spec.ts +84 -0
- package/src/compact/fromU8a.ts +109 -0
- package/src/compact/index.ts +28 -0
- package/src/compact/stripLength.spec.ts +17 -0
- package/src/compact/stripLength.ts +26 -0
- package/src/compact/toU8a.spec.ts +59 -0
- package/src/compact/toU8a.ts +53 -0
- package/src/compact/types.ts +4 -0
- package/src/detectPackage.spec.ts +110 -0
- package/src/detectPackage.ts +138 -0
- package/src/extractTime.spec.ts +21 -0
- package/src/extractTime.ts +71 -0
- package/src/float/index.ts +4 -0
- package/src/float/toU8a.spec.ts +52 -0
- package/src/float/toU8a.ts +30 -0
- package/src/format/formatBalance.spec.ts +296 -0
- package/src/format/formatBalance.ts +177 -0
- package/src/format/formatDate.spec.ts +17 -0
- package/src/format/formatDate.ts +22 -0
- package/src/format/formatDecimal.spec.ts +28 -0
- package/src/format/formatDecimal.ts +22 -0
- package/src/format/formatElapsed.spec.ts +44 -0
- package/src/format/formatElapsed.ts +35 -0
- package/src/format/formatNumber.spec.ts +60 -0
- package/src/format/formatNumber.ts +29 -0
- package/src/format/getSeparator.spec.ts +53 -0
- package/src/format/getSeparator.ts +15 -0
- package/src/format/index.ts +9 -0
- package/src/format/si.ts +52 -0
- package/src/has.spec.ts +12 -0
- package/src/has.ts +38 -0
- package/src/hex/addPrefix.spec.ts +32 -0
- package/src/hex/addPrefix.ts +26 -0
- package/src/hex/fixLength.spec.ts +38 -0
- package/src/hex/fixLength.ts +36 -0
- package/src/hex/hasPrefix.spec.ts +32 -0
- package/src/hex/hasPrefix.ts +24 -0
- package/src/hex/index.ts +16 -0
- package/src/hex/stripPrefix.spec.ts +44 -0
- package/src/hex/stripPrefix.ts +30 -0
- package/src/hex/toBigInt.spec.ts +74 -0
- package/src/hex/toBigInt.ts +19 -0
- package/src/hex/toBn.spec.ts +74 -0
- package/src/hex/toBn.ts +40 -0
- package/src/hex/toNumber.spec.ts +20 -0
- package/src/hex/toNumber.ts +24 -0
- package/src/hex/toString.spec.ts +20 -0
- package/src/hex/toString.ts +25 -0
- package/src/hex/toU8a.spec.ts +76 -0
- package/src/hex/toU8a.ts +68 -0
- package/src/hex/toU8aBuffer.spec.ts +38 -0
- package/src/hex/toU8aBuffer.ts +52 -0
- package/src/index.spec.ts +12 -0
- package/src/index.ts +6 -0
- package/src/is/array.spec.ts +20 -0
- package/src/is/array.ts +10 -0
- package/src/is/ascii.spec.ts +49 -0
- package/src/is/ascii.ts +52 -0
- package/src/is/bigInt.spec.ts +26 -0
- package/src/is/bigInt.ts +20 -0
- package/src/is/bn.spec.ts +21 -0
- package/src/is/bn.ts +23 -0
- package/src/is/boolean.spec.ts +26 -0
- package/src/is/boolean.ts +20 -0
- package/src/is/buffer.spec.ts +26 -0
- package/src/is/buffer.ts +28 -0
- package/src/is/childClass.spec.ts +36 -0
- package/src/is/childClass.ts +29 -0
- package/src/is/class.spec.ts +37 -0
- package/src/is/class.ts +12 -0
- package/src/is/codec.spec.ts +52 -0
- package/src/is/codec.ts +26 -0
- package/src/is/compact.spec.ts +27 -0
- package/src/is/compact.ts +19 -0
- package/src/is/error.spec.ts +20 -0
- package/src/is/error.ts +23 -0
- package/src/is/function.spec.ts +20 -0
- package/src/is/function.ts +23 -0
- package/src/is/helpers.ts +23 -0
- package/src/is/hex.spec.ts +77 -0
- package/src/is/hex.ts +36 -0
- package/src/is/index.ts +36 -0
- package/src/is/instanceOf.spec.ts +48 -0
- package/src/is/instanceOf.ts +24 -0
- package/src/is/ip.spec.ts +50 -0
- package/src/is/ip.ts +56 -0
- package/src/is/jsonObject.spec.ts +51 -0
- package/src/is/jsonObject.ts +45 -0
- package/src/is/null.spec.ts +20 -0
- package/src/is/null.ts +20 -0
- package/src/is/number.spec.ts +20 -0
- package/src/is/number.ts +20 -0
- package/src/is/object.spec.ts +32 -0
- package/src/is/object.ts +24 -0
- package/src/is/observable.spec.ts +26 -0
- package/src/is/observable.ts +22 -0
- package/src/is/promise.spec.ts +32 -0
- package/src/is/promise.ts +6 -0
- package/src/is/riscv.spec.ts +36 -0
- package/src/is/riscv.ts +27 -0
- package/src/is/string.spec.ts +32 -0
- package/src/is/string.ts +22 -0
- package/src/is/testChain.spec.ts +25 -0
- package/src/is/testChain.ts +12 -0
- package/src/is/toBigInt.spec.ts +22 -0
- package/src/is/toBigInt.ts +8 -0
- package/src/is/toBn.spec.ts +25 -0
- package/src/is/toBn.ts +8 -0
- package/src/is/u8a.spec.ts +42 -0
- package/src/is/u8a.ts +25 -0
- package/src/is/undefined.spec.ts +23 -0
- package/src/is/undefined.ts +21 -0
- package/src/is/utf8.spec.ts +42 -0
- package/src/is/utf8.ts +207 -0
- package/src/is/wasm.spec.ts +20 -0
- package/src/is/wasm.ts +17 -0
- package/src/lazy.spec.ts +71 -0
- package/src/lazy.ts +63 -0
- package/src/logger.spec.ts +188 -0
- package/src/logger.ts +160 -0
- package/src/memoize.spec.ts +79 -0
- package/src/memoize.ts +49 -0
- package/src/mod.ts +4 -0
- package/src/nextTick.ts +26 -0
- package/src/noop.ts +16 -0
- package/src/number/index.ts +9 -0
- package/src/number/toHex.spec.ts +44 -0
- package/src/number/toHex.ts +27 -0
- package/src/number/toU8a.spec.ts +50 -0
- package/src/number/toU8a.ts +25 -0
- package/src/object/clear.spec.ts +14 -0
- package/src/object/clear.ts +16 -0
- package/src/object/copy.spec.ts +17 -0
- package/src/object/copy.ts +12 -0
- package/src/object/entries.spec.ts +20 -0
- package/src/object/entries.ts +12 -0
- package/src/object/index.ts +10 -0
- package/src/object/keys.spec.ts +19 -0
- package/src/object/keys.ts +10 -0
- package/src/object/property.spec.ts +149 -0
- package/src/object/property.ts +39 -0
- package/src/object/spread.spec.ts +36 -0
- package/src/object/spread.ts +38 -0
- package/src/object/values.spec.ts +19 -0
- package/src/object/values.ts +10 -0
- package/src/packageDetect.ts +13 -0
- package/src/packageInfo.ts +6 -0
- package/src/promisify.spec.ts +50 -0
- package/src/promisify.ts +30 -0
- package/src/string/camelCase.spec.ts +119 -0
- package/src/string/camelCase.ts +79 -0
- package/src/string/index.ts +12 -0
- package/src/string/lowerFirst.spec.ts +41 -0
- package/src/string/lowerFirst.ts +46 -0
- package/src/string/pascalCase.spec.ts +26 -0
- package/src/string/shorten.spec.ts +38 -0
- package/src/string/shorten.ts +24 -0
- package/src/string/toHex.spec.ts +26 -0
- package/src/string/toHex.ts +27 -0
- package/src/string/toU8a.spec.ts +31 -0
- package/src/string/toU8a.ts +28 -0
- package/src/string/upperFirst.spec.ts +41 -0
- package/src/stringify.spec.ts +33 -0
- package/src/stringify.ts +20 -0
- package/src/test/index.ts +4 -0
- package/src/test/performance.ts +92 -0
- package/src/types.ts +100 -0
- package/src/u8a/cmp.spec.ts +62 -0
- package/src/u8a/cmp.ts +50 -0
- package/src/u8a/concat.spec.ts +42 -0
- package/src/u8a/concat.ts +60 -0
- package/src/u8a/concatBuffer.ts +34 -0
- package/src/u8a/empty.spec.ts +46 -0
- package/src/u8a/empty.ts +22 -0
- package/src/u8a/eq.spec.ts +113 -0
- package/src/u8a/eq.ts +46 -0
- package/src/u8a/fixLength.spec.ts +34 -0
- package/src/u8a/fixLength.ts +34 -0
- package/src/u8a/index.ts +22 -0
- package/src/u8a/sorted.spec.ts +20 -0
- package/src/u8a/sorted.ts +22 -0
- package/src/u8a/toBigInt.spec.ts +106 -0
- package/src/u8a/toBigInt.ts +100 -0
- package/src/u8a/toBn.spec.ts +106 -0
- package/src/u8a/toBn.ts +101 -0
- package/src/u8a/toBuffer.spec.ts +20 -0
- package/src/u8a/toBuffer.ts +28 -0
- package/src/u8a/toFloat.spec.ts +86 -0
- package/src/u8a/toFloat.ts +26 -0
- package/src/u8a/toHex.spec.ts +94 -0
- package/src/u8a/toHex.ts +68 -0
- package/src/u8a/toHexBuffer.spec.ts +63 -0
- package/src/u8a/toHexBuffer.ts +30 -0
- package/src/u8a/toNumber.spec.ts +47 -0
- package/src/u8a/toNumber.ts +80 -0
- package/src/u8a/toString.spec.ts +36 -0
- package/src/u8a/toString.ts +26 -0
- package/src/u8a/toU8a.spec.ts +63 -0
- package/src/u8a/toU8a.ts +46 -0
- package/src/u8a/wrap.spec.ts +139 -0
- package/src/u8a/wrap.ts +65 -0
- package/tsconfig.build.json +19 -0
- package/tsconfig.spec.json +17 -0
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# @pezkuwi/util
|
|
2
|
+
|
|
3
|
+
Various useful utility functions that are used across all projects in the [@pezkuwi](https://pezkuwi.js.org) namespace. It provides utility functions with additional safety checks, allowing not only for consistent coding, but also reducing the general boilerplate.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
Installation -
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
yarn add @pezkuwi/util
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Functions can be imported directly from the package, e.g.
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { isHex } from '@pezkuwi/util';
|
|
17
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": "Jaco Greeff <jacogr@gmail.com>",
|
|
3
|
+
"bugs": "https://github.com/pezkuwichain/pezkuwi-common/issues",
|
|
4
|
+
"description": "A collection of useful utilities for @pezkuwi",
|
|
5
|
+
"engines": {
|
|
6
|
+
"node": ">=18"
|
|
7
|
+
},
|
|
8
|
+
"homepage": "https://github.com/pezkuwichain/pezkuwi-common/tree/master/packages/util#readme",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"name": "@pezkuwi/util",
|
|
11
|
+
"repository": {
|
|
12
|
+
"directory": "packages/util",
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/pezkuwichain/pezkuwi-common.git"
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": [
|
|
17
|
+
"./packageDetect.js",
|
|
18
|
+
"./packageDetect.cjs"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"version": "14.0.1",
|
|
22
|
+
"main": "index.js",
|
|
23
|
+
"exports": {
|
|
24
|
+
"./hex/toU8a": {
|
|
25
|
+
"node": {
|
|
26
|
+
"require": "./cjs/hex/toU8aBuffer.js",
|
|
27
|
+
"default": "./hex/toU8aBuffer.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"./u8a/toHex": {
|
|
31
|
+
"node": {
|
|
32
|
+
"require": "./cjs/u8a/toHexBuffer.js",
|
|
33
|
+
"default": "./u8a/toHexBuffer.js"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@pezkuwi/x-bigint": "14.0.1",
|
|
39
|
+
"@pezkuwi/x-global": "14.0.1",
|
|
40
|
+
"@pezkuwi/x-textdecoder": "14.0.1",
|
|
41
|
+
"@pezkuwi/x-textencoder": "14.0.1",
|
|
42
|
+
"@types/bn.js": "^5.1.6",
|
|
43
|
+
"bn.js": "^5.2.1",
|
|
44
|
+
"tslib": "^2.8.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@pezkuwi/x-randomvalues": "14.0.1"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { arrayChunk, arrayRange } from '../index.js';
|
|
7
|
+
import { perf } from '../test/index.js';
|
|
8
|
+
|
|
9
|
+
describe('arrayChunk', (): void => {
|
|
10
|
+
it('chunks with exact', (): void => {
|
|
11
|
+
expect(
|
|
12
|
+
arrayChunk([1, 2, 3, 4, 5, 6, 7, 8], 8)
|
|
13
|
+
).toEqual([[1, 2, 3, 4, 5, 6, 7, 8]]);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('chunks with unequal', (): void => {
|
|
17
|
+
expect(
|
|
18
|
+
arrayChunk([1, 2, 3, 4, 5, 6, 7], 3)
|
|
19
|
+
).toEqual([[1, 2, 3], [4, 5, 6], [7]]);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('chunks with non-empty results', (): void => {
|
|
23
|
+
expect(
|
|
24
|
+
arrayChunk([[1, 2], [3, 4], [5, 6], [7, 8]], 2)
|
|
25
|
+
).toEqual([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
perf('arrayChunk', 200_000, [[arrayRange(500), 50]], arrayChunk);
|
|
29
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @name arrayChunk
|
|
6
|
+
* @summary Split T[] into T[][] based on the defind size
|
|
7
|
+
* @description
|
|
8
|
+
* Returns a set ao arrays based on the chunksize
|
|
9
|
+
* @example
|
|
10
|
+
* <BR>
|
|
11
|
+
*
|
|
12
|
+
* ```javascript
|
|
13
|
+
* import { arrayChunk } from '@pezkuwi/util';
|
|
14
|
+
*
|
|
15
|
+
* arrayChunk([1, 2, 3, 4, 5]); // [[1, 2], [3, 4], [5]]
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export function arrayChunk <T> (array: T[], chunkSize: number): T[][] {
|
|
19
|
+
const outputSize = Math.ceil(array.length / chunkSize);
|
|
20
|
+
|
|
21
|
+
// shortcut for the single-split case
|
|
22
|
+
if (outputSize === 1) {
|
|
23
|
+
return [array];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const output = Array<T[]>(outputSize);
|
|
27
|
+
|
|
28
|
+
for (let i = 0; i < outputSize; i++) {
|
|
29
|
+
const offset = i * chunkSize;
|
|
30
|
+
|
|
31
|
+
output[i] = array.slice(offset, offset + chunkSize);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return output;
|
|
35
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { arrayFilter } from './index.js';
|
|
7
|
+
|
|
8
|
+
describe('filterArray', (): void => {
|
|
9
|
+
it('filters arrays, removing undefined', (): void => {
|
|
10
|
+
expect(
|
|
11
|
+
arrayFilter([0, '', null, false, undefined, NaN])
|
|
12
|
+
).toEqual([0, '', null, false, NaN]);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('filters arrays, removing undefined & null (allowNull = false)', (): void => {
|
|
16
|
+
expect(
|
|
17
|
+
arrayFilter([0, '', null, false, undefined, NaN], false)
|
|
18
|
+
).toEqual([0, '', false, NaN]);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @name arrayFilter
|
|
6
|
+
* @summary Filters undefined and (optionally) null values from an array
|
|
7
|
+
* @description
|
|
8
|
+
* Returns a new array with all `undefined` values removed. Optionally, when `allowNulls = false`, it removes the `null` values as well
|
|
9
|
+
* @example
|
|
10
|
+
* <BR>
|
|
11
|
+
*
|
|
12
|
+
* ```javascript
|
|
13
|
+
* import { arrayFilter } from '@pezkuwi/util';
|
|
14
|
+
*
|
|
15
|
+
* arrayFilter([0, void 0, true, null, false, '']); // [0, true, null, false, '']
|
|
16
|
+
* arrayFilter([0, void 0, true, null, false, ''], false); // [0, true, false, '']
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export function arrayFilter <T = unknown> (array: readonly (T | null | undefined)[], allowNulls = true): T[] {
|
|
20
|
+
return array.filter((v): v is T =>
|
|
21
|
+
v !== undefined &&
|
|
22
|
+
(allowNulls || v !== null)
|
|
23
|
+
);
|
|
24
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { perf } from '../test/index.js';
|
|
7
|
+
import { arrayFlatten } from './index.js';
|
|
8
|
+
|
|
9
|
+
const PERF_ONE = [[1, 2, 3, 4, 5]];
|
|
10
|
+
const PERF_MUL = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
|
|
11
|
+
|
|
12
|
+
describe('arrayFlatten', (): void => {
|
|
13
|
+
it('flattens arrays', (): void => {
|
|
14
|
+
expect(
|
|
15
|
+
arrayFlatten([[0], [1, 2, 3], [4, 5], [6], [], [7, 8]])
|
|
16
|
+
).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8]);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('flattens a single entry', (): void => {
|
|
20
|
+
expect(
|
|
21
|
+
arrayFlatten([[1, 2, 3, 4, 5]])
|
|
22
|
+
).toEqual([1, 2, 3, 4, 5]);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('flattens an empty', (): void => {
|
|
26
|
+
expect(
|
|
27
|
+
arrayFlatten([])
|
|
28
|
+
).toEqual([]);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
perf('arrayFlatten (single)', 10_000_000, [[PERF_ONE]], arrayFlatten);
|
|
32
|
+
perf('arrayFlatten (multi)', 700_000, [[PERF_MUL]], arrayFlatten);
|
|
33
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// This is supposed to be a faster concat...
|
|
5
|
+
// https://dev.to/uilicious/javascript-array-push-is-945x-faster-than-array-concat-1oki
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @name arrayFlatten
|
|
9
|
+
* @summary Merge T[][] into T[]
|
|
10
|
+
* @description
|
|
11
|
+
* Returns a new array with all arrays merged into one
|
|
12
|
+
* @example
|
|
13
|
+
* <BR>
|
|
14
|
+
*
|
|
15
|
+
* ```javascript
|
|
16
|
+
* import { arrayFlatten } from '@pezkuwi/util';
|
|
17
|
+
*
|
|
18
|
+
* arrayFlatten([[1, 2], [3, 4], [5]]); // [1, 2, 3, 4, 5]
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function arrayFlatten <T> (arrays: readonly T[][]): T[] {
|
|
22
|
+
const num = arrays.length;
|
|
23
|
+
|
|
24
|
+
// shortcuts for the empty & single-entry case
|
|
25
|
+
if (num === 0) {
|
|
26
|
+
return [];
|
|
27
|
+
} else if (num === 1) {
|
|
28
|
+
return arrays[0];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// pre-allocate based on the combined size
|
|
32
|
+
let size = 0;
|
|
33
|
+
|
|
34
|
+
for (let i = 0; i < num; i++) {
|
|
35
|
+
size += arrays[i].length;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const output = new Array<T>(size);
|
|
39
|
+
let i = -1;
|
|
40
|
+
|
|
41
|
+
for (let j = 0; j < num; j++) {
|
|
42
|
+
const a = arrays[j];
|
|
43
|
+
|
|
44
|
+
// instead of pushing, we just set the entries
|
|
45
|
+
for (let e = 0, count = a.length; e < count; e++) {
|
|
46
|
+
output[++i] = a[e];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return output;
|
|
51
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @summary Utility methods that operates on arrays
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export { arrayChunk } from './chunk.js';
|
|
9
|
+
export { arrayFilter } from './filter.js';
|
|
10
|
+
export { arrayFlatten } from './flatten.js';
|
|
11
|
+
export { arrayRange } from './range.js';
|
|
12
|
+
export { arrayShuffle } from './shuffle.js';
|
|
13
|
+
export { arrayUnzip } from './unzip.js';
|
|
14
|
+
export { arrayZip } from './zip.js';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { perf } from '../test/index.js';
|
|
7
|
+
import { arrayRange } from './index.js';
|
|
8
|
+
|
|
9
|
+
describe('arrayRange', (): void => {
|
|
10
|
+
it('does not allow 0 values', (): void => {
|
|
11
|
+
expect(
|
|
12
|
+
() => arrayRange(0)
|
|
13
|
+
).toThrow(/Expected non-zero, positive number as a range size/);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('creates a range of the specified length', (): void => {
|
|
17
|
+
expect(
|
|
18
|
+
arrayRange(10)
|
|
19
|
+
).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('creates a range of the specified length, with offset', (): void => {
|
|
23
|
+
expect(
|
|
24
|
+
arrayRange(7, 3)
|
|
25
|
+
).toEqual([3, 4, 5, 6, 7, 8, 9]);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
perf('arrayRange (100 entries)', 1_000_000, [[100]], arrayRange);
|
|
29
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @name arrayRange
|
|
6
|
+
* @summary Returns a range of numbers ith the size and the specified offset
|
|
7
|
+
* @description
|
|
8
|
+
* 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.
|
|
9
|
+
* @example
|
|
10
|
+
* <BR>
|
|
11
|
+
*
|
|
12
|
+
* ```javascript
|
|
13
|
+
* import { arrayRange } from '@pezkuwi/util';
|
|
14
|
+
*
|
|
15
|
+
* arrayRange(5); // [0, 1, 2, 3, 4]
|
|
16
|
+
* arrayRange(3, 5); // [5, 6, 7]
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export function arrayRange (size: number, startAt = 0): number[] {
|
|
20
|
+
if (size <= 0) {
|
|
21
|
+
throw new Error('Expected non-zero, positive number as a range size');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const result = new Array<number>(size);
|
|
25
|
+
|
|
26
|
+
for (let i = 0; i < size; i++) {
|
|
27
|
+
result[i] = i + startAt;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { stringify } from '../stringify.js';
|
|
7
|
+
import { perf } from '../test/index.js';
|
|
8
|
+
import { arrayRange, arrayShuffle } from './index.js';
|
|
9
|
+
|
|
10
|
+
const ptest = arrayRange(16284);
|
|
11
|
+
|
|
12
|
+
describe('arrayShuffle', (): void => {
|
|
13
|
+
it('returns an empty array as-is', (): void => {
|
|
14
|
+
expect(
|
|
15
|
+
arrayShuffle([])
|
|
16
|
+
).toEqual([]);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('returns a single array as-is', (): void => {
|
|
20
|
+
expect(
|
|
21
|
+
arrayShuffle([100])
|
|
22
|
+
).toEqual([100]);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('shuffles an array', (): void => {
|
|
26
|
+
const inp = arrayRange(100);
|
|
27
|
+
const out = arrayShuffle(inp);
|
|
28
|
+
|
|
29
|
+
expect(inp).toHaveLength(out.length);
|
|
30
|
+
expect(
|
|
31
|
+
inp.filter((v) => !out.includes(v))
|
|
32
|
+
).toEqual([]);
|
|
33
|
+
expect(
|
|
34
|
+
stringify(inp)
|
|
35
|
+
).not.toEqual(stringify(out));
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
perf('arrayShuffle', 1000, [[ptest]], arrayShuffle);
|
|
39
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @name arrayShuffle
|
|
6
|
+
* @description Shuffles the input array (unlike sort, this is not done in-place)
|
|
7
|
+
*/
|
|
8
|
+
export function arrayShuffle <T> (input: readonly T[]): T[] {
|
|
9
|
+
const result = input.slice();
|
|
10
|
+
let curr = result.length;
|
|
11
|
+
|
|
12
|
+
// noop for the single entry
|
|
13
|
+
if (curr === 1) {
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
while (curr !== 0) {
|
|
18
|
+
// ~~ is more performant than Math.floor
|
|
19
|
+
const rand = ~~(Math.random() * curr);
|
|
20
|
+
|
|
21
|
+
curr--;
|
|
22
|
+
|
|
23
|
+
[result[curr], result[rand]] = [result[rand], result[curr]];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { arrayUnzip } from './index.js';
|
|
7
|
+
|
|
8
|
+
describe('arrayUnzip', (): void => {
|
|
9
|
+
it('unzips entries', (): void => {
|
|
10
|
+
expect(
|
|
11
|
+
arrayUnzip([['a', 1], ['b', 2], ['c', 3]])
|
|
12
|
+
).toEqual([['a', 'b', 'c'], [1, 2, 3]]);
|
|
13
|
+
});
|
|
14
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @name arrayUnzip
|
|
6
|
+
* @description Splits a single [K, V][] into [K[], V[]]
|
|
7
|
+
*/
|
|
8
|
+
export function arrayUnzip <K, V> (entries: readonly [K, V][]): [K[], V[]] {
|
|
9
|
+
const count = entries.length;
|
|
10
|
+
const keys = new Array<K>(count);
|
|
11
|
+
const values = new Array<V>(count);
|
|
12
|
+
|
|
13
|
+
for (let i = 0; i < count; i++) {
|
|
14
|
+
[keys[i], values[i]] = entries[i];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return [keys, values];
|
|
18
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { arrayZip } from './index.js';
|
|
7
|
+
|
|
8
|
+
describe('arrayZip', (): void => {
|
|
9
|
+
it('zips a simple one', (): void => {
|
|
10
|
+
expect(
|
|
11
|
+
arrayZip(['a', 'b', 'c'], [1, 2, 3])
|
|
12
|
+
).toEqual([['a', 1], ['b', 2], ['c', 3]]);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('zips where values > keys', (): void => {
|
|
16
|
+
expect(
|
|
17
|
+
arrayZip(['a', 'b', 'c'], [1, 2, 3, 4])
|
|
18
|
+
).toEqual([['a', 1], ['b', 2], ['c', 3]]);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('zips where values < keys', (): void => {
|
|
22
|
+
expect(
|
|
23
|
+
arrayZip(['a', 'b', 'c'], [1, 2])
|
|
24
|
+
).toEqual([['a', 1], ['b', 2], ['c', undefined]]);
|
|
25
|
+
});
|
|
26
|
+
});
|
package/src/array/zip.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @name arrayZip
|
|
6
|
+
* @description Combines 2 distinct key/value arrays into a single [K, V] array
|
|
7
|
+
*/
|
|
8
|
+
export function arrayZip <K, V> (keys: readonly K[], values: readonly V[]): [K, V][] {
|
|
9
|
+
const count = keys.length;
|
|
10
|
+
const result = new Array<[K, V]>(count);
|
|
11
|
+
|
|
12
|
+
for (let i = 0; i < count; i++) {
|
|
13
|
+
result[i] = [keys[i], values[i]];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { assert, assertReturn } from './index.js';
|
|
7
|
+
|
|
8
|
+
describe('assert', (): void => {
|
|
9
|
+
it('should not throw an error when test is true', (): void => {
|
|
10
|
+
assert(true, 'nothing should be thrown');
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should throw an error when test is not true', (): void => {
|
|
14
|
+
expect(
|
|
15
|
+
() => assert(false, 'error thrown')
|
|
16
|
+
).toThrow(/error thrown/);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should throw an error when message: () => string', (): void => {
|
|
20
|
+
expect(
|
|
21
|
+
() => assert(false, (): string => 'message from function')
|
|
22
|
+
).toThrow(/message from function/);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe('assertReturn', (): void => {
|
|
27
|
+
it('should not throw an error when result is true', (): void => {
|
|
28
|
+
expect(assertReturn(true, 'nothing should be thrown')).toEqual(true);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should not throw an error when result is false', (): void => {
|
|
32
|
+
expect(assertReturn(false, 'nothing should be thrown')).toEqual(false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should throw an error when result is undefined', (): void => {
|
|
36
|
+
expect(
|
|
37
|
+
() => assertReturn(undefined, 'something thrown')
|
|
38
|
+
).toThrow(/something thrown/);
|
|
39
|
+
});
|
|
40
|
+
});
|
package/src/assert.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { isFunction } from './is/function.js';
|
|
5
|
+
|
|
6
|
+
type MessageFn = () => string;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @name assert
|
|
10
|
+
* @summary Checks for a valid test, if not Error is thrown.
|
|
11
|
+
* @description
|
|
12
|
+
* Checks that `test` is a truthy value. If value is falsy (`null`, `undefined`, `false`, ...), it throws an Error with the supplied `message`. When `test` passes, `true` is returned.
|
|
13
|
+
* @example
|
|
14
|
+
* <BR>
|
|
15
|
+
*
|
|
16
|
+
* ```javascript
|
|
17
|
+
* const { assert } from '@pezkuwi/util';
|
|
18
|
+
*
|
|
19
|
+
* assert(true, 'True should be true'); // passes
|
|
20
|
+
* assert(false, 'False should not be true'); // Error thrown
|
|
21
|
+
* assert(false, () => 'message'); // Error with 'message'
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function assert (condition: unknown, message: string | MessageFn): asserts condition {
|
|
25
|
+
if (!condition) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
isFunction(message)
|
|
28
|
+
? message()
|
|
29
|
+
: message
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @name assertReturn
|
|
36
|
+
* @description Returns when the value is not undefined/null, otherwise throws assertion error
|
|
37
|
+
*/
|
|
38
|
+
export function assertReturn <T> (value: T | undefined | null, message: string | MessageFn): T {
|
|
39
|
+
assert(value !== undefined && value !== null, message);
|
|
40
|
+
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @name assertUnreachable
|
|
46
|
+
* @description An assertion helper that ensures all codepaths are followed
|
|
47
|
+
*/
|
|
48
|
+
export function assertUnreachable (x: never): never {
|
|
49
|
+
throw new Error(`This codepath should be unreachable. Unhandled input: ${x as unknown as string}`);
|
|
50
|
+
}
|