@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.
Files changed (260) hide show
  1. package/README.md +17 -0
  2. package/package.json +49 -0
  3. package/src/array/chunk.spec.ts +29 -0
  4. package/src/array/chunk.ts +35 -0
  5. package/src/array/filter.spec.ts +20 -0
  6. package/src/array/filter.ts +24 -0
  7. package/src/array/flatten.spec.ts +33 -0
  8. package/src/array/flatten.ts +51 -0
  9. package/src/array/index.ts +14 -0
  10. package/src/array/range.spec.ts +29 -0
  11. package/src/array/range.ts +31 -0
  12. package/src/array/shuffle.spec.ts +39 -0
  13. package/src/array/shuffle.ts +27 -0
  14. package/src/array/unzip.spec.ts +14 -0
  15. package/src/array/unzip.ts +18 -0
  16. package/src/array/zip.spec.ts +26 -0
  17. package/src/array/zip.ts +17 -0
  18. package/src/assert.spec.ts +40 -0
  19. package/src/assert.ts +50 -0
  20. package/src/bi/consts.ts +112 -0
  21. package/src/bi/helpers.ts +23 -0
  22. package/src/bi/index.ts +16 -0
  23. package/src/bi/max.spec.ts +32 -0
  24. package/src/bi/min.spec.ts +26 -0
  25. package/src/bi/min.ts +16 -0
  26. package/src/bi/sqrt.spec.ts +59 -0
  27. package/src/bi/sqrt.ts +43 -0
  28. package/src/bi/toBigInt.spec.ts +69 -0
  29. package/src/bi/toBigInt.ts +33 -0
  30. package/src/bi/toHex.spec.ts +56 -0
  31. package/src/bi/toHex.ts +16 -0
  32. package/src/bi/toU8a.spec.ts +99 -0
  33. package/src/bi/toU8a.ts +72 -0
  34. package/src/bn/bn.ts +6 -0
  35. package/src/bn/consts.spec.ts +38 -0
  36. package/src/bn/consts.ts +112 -0
  37. package/src/bn/fromHex.spec.ts +15 -0
  38. package/src/bn/fromHex.ts +4 -0
  39. package/src/bn/index.ts +18 -0
  40. package/src/bn/max.spec.ts +32 -0
  41. package/src/bn/min.spec.ts +26 -0
  42. package/src/bn/min.ts +36 -0
  43. package/src/bn/sqrt.spec.ts +37 -0
  44. package/src/bn/sqrt.ts +50 -0
  45. package/src/bn/toBn.spec.ts +53 -0
  46. package/src/bn/toBn.ts +43 -0
  47. package/src/bn/toHex.spec.ts +50 -0
  48. package/src/bn/toHex.ts +27 -0
  49. package/src/bn/toU8a.spec.ts +60 -0
  50. package/src/bn/toU8a.ts +45 -0
  51. package/src/buffer/index.ts +8 -0
  52. package/src/buffer/toU8a.spec.ts +20 -0
  53. package/src/buffer/toU8a.ts +20 -0
  54. package/src/bundle.ts +35 -0
  55. package/src/compact/addLength.spec.ts +14 -0
  56. package/src/compact/addLength.ts +24 -0
  57. package/src/compact/defaults.ts +7 -0
  58. package/src/compact/fromU8a.spec.ts +84 -0
  59. package/src/compact/fromU8a.ts +109 -0
  60. package/src/compact/index.ts +28 -0
  61. package/src/compact/stripLength.spec.ts +17 -0
  62. package/src/compact/stripLength.ts +26 -0
  63. package/src/compact/toU8a.spec.ts +59 -0
  64. package/src/compact/toU8a.ts +53 -0
  65. package/src/compact/types.ts +4 -0
  66. package/src/detectPackage.spec.ts +110 -0
  67. package/src/detectPackage.ts +138 -0
  68. package/src/extractTime.spec.ts +21 -0
  69. package/src/extractTime.ts +71 -0
  70. package/src/float/index.ts +4 -0
  71. package/src/float/toU8a.spec.ts +52 -0
  72. package/src/float/toU8a.ts +30 -0
  73. package/src/format/formatBalance.spec.ts +296 -0
  74. package/src/format/formatBalance.ts +177 -0
  75. package/src/format/formatDate.spec.ts +17 -0
  76. package/src/format/formatDate.ts +22 -0
  77. package/src/format/formatDecimal.spec.ts +28 -0
  78. package/src/format/formatDecimal.ts +22 -0
  79. package/src/format/formatElapsed.spec.ts +44 -0
  80. package/src/format/formatElapsed.ts +35 -0
  81. package/src/format/formatNumber.spec.ts +60 -0
  82. package/src/format/formatNumber.ts +29 -0
  83. package/src/format/getSeparator.spec.ts +53 -0
  84. package/src/format/getSeparator.ts +15 -0
  85. package/src/format/index.ts +9 -0
  86. package/src/format/si.ts +52 -0
  87. package/src/has.spec.ts +12 -0
  88. package/src/has.ts +38 -0
  89. package/src/hex/addPrefix.spec.ts +32 -0
  90. package/src/hex/addPrefix.ts +26 -0
  91. package/src/hex/fixLength.spec.ts +38 -0
  92. package/src/hex/fixLength.ts +36 -0
  93. package/src/hex/hasPrefix.spec.ts +32 -0
  94. package/src/hex/hasPrefix.ts +24 -0
  95. package/src/hex/index.ts +16 -0
  96. package/src/hex/stripPrefix.spec.ts +44 -0
  97. package/src/hex/stripPrefix.ts +30 -0
  98. package/src/hex/toBigInt.spec.ts +74 -0
  99. package/src/hex/toBigInt.ts +19 -0
  100. package/src/hex/toBn.spec.ts +74 -0
  101. package/src/hex/toBn.ts +40 -0
  102. package/src/hex/toNumber.spec.ts +20 -0
  103. package/src/hex/toNumber.ts +24 -0
  104. package/src/hex/toString.spec.ts +20 -0
  105. package/src/hex/toString.ts +25 -0
  106. package/src/hex/toU8a.spec.ts +76 -0
  107. package/src/hex/toU8a.ts +68 -0
  108. package/src/hex/toU8aBuffer.spec.ts +38 -0
  109. package/src/hex/toU8aBuffer.ts +52 -0
  110. package/src/index.spec.ts +12 -0
  111. package/src/index.ts +6 -0
  112. package/src/is/array.spec.ts +20 -0
  113. package/src/is/array.ts +10 -0
  114. package/src/is/ascii.spec.ts +49 -0
  115. package/src/is/ascii.ts +52 -0
  116. package/src/is/bigInt.spec.ts +26 -0
  117. package/src/is/bigInt.ts +20 -0
  118. package/src/is/bn.spec.ts +21 -0
  119. package/src/is/bn.ts +23 -0
  120. package/src/is/boolean.spec.ts +26 -0
  121. package/src/is/boolean.ts +20 -0
  122. package/src/is/buffer.spec.ts +26 -0
  123. package/src/is/buffer.ts +28 -0
  124. package/src/is/childClass.spec.ts +36 -0
  125. package/src/is/childClass.ts +29 -0
  126. package/src/is/class.spec.ts +37 -0
  127. package/src/is/class.ts +12 -0
  128. package/src/is/codec.spec.ts +52 -0
  129. package/src/is/codec.ts +26 -0
  130. package/src/is/compact.spec.ts +27 -0
  131. package/src/is/compact.ts +19 -0
  132. package/src/is/error.spec.ts +20 -0
  133. package/src/is/error.ts +23 -0
  134. package/src/is/function.spec.ts +20 -0
  135. package/src/is/function.ts +23 -0
  136. package/src/is/helpers.ts +23 -0
  137. package/src/is/hex.spec.ts +77 -0
  138. package/src/is/hex.ts +36 -0
  139. package/src/is/index.ts +36 -0
  140. package/src/is/instanceOf.spec.ts +48 -0
  141. package/src/is/instanceOf.ts +24 -0
  142. package/src/is/ip.spec.ts +50 -0
  143. package/src/is/ip.ts +56 -0
  144. package/src/is/jsonObject.spec.ts +51 -0
  145. package/src/is/jsonObject.ts +45 -0
  146. package/src/is/null.spec.ts +20 -0
  147. package/src/is/null.ts +20 -0
  148. package/src/is/number.spec.ts +20 -0
  149. package/src/is/number.ts +20 -0
  150. package/src/is/object.spec.ts +32 -0
  151. package/src/is/object.ts +24 -0
  152. package/src/is/observable.spec.ts +26 -0
  153. package/src/is/observable.ts +22 -0
  154. package/src/is/promise.spec.ts +32 -0
  155. package/src/is/promise.ts +6 -0
  156. package/src/is/riscv.spec.ts +36 -0
  157. package/src/is/riscv.ts +27 -0
  158. package/src/is/string.spec.ts +32 -0
  159. package/src/is/string.ts +22 -0
  160. package/src/is/testChain.spec.ts +25 -0
  161. package/src/is/testChain.ts +12 -0
  162. package/src/is/toBigInt.spec.ts +22 -0
  163. package/src/is/toBigInt.ts +8 -0
  164. package/src/is/toBn.spec.ts +25 -0
  165. package/src/is/toBn.ts +8 -0
  166. package/src/is/u8a.spec.ts +42 -0
  167. package/src/is/u8a.ts +25 -0
  168. package/src/is/undefined.spec.ts +23 -0
  169. package/src/is/undefined.ts +21 -0
  170. package/src/is/utf8.spec.ts +42 -0
  171. package/src/is/utf8.ts +207 -0
  172. package/src/is/wasm.spec.ts +20 -0
  173. package/src/is/wasm.ts +17 -0
  174. package/src/lazy.spec.ts +71 -0
  175. package/src/lazy.ts +63 -0
  176. package/src/logger.spec.ts +188 -0
  177. package/src/logger.ts +160 -0
  178. package/src/memoize.spec.ts +79 -0
  179. package/src/memoize.ts +49 -0
  180. package/src/mod.ts +4 -0
  181. package/src/nextTick.ts +26 -0
  182. package/src/noop.ts +16 -0
  183. package/src/number/index.ts +9 -0
  184. package/src/number/toHex.spec.ts +44 -0
  185. package/src/number/toHex.ts +27 -0
  186. package/src/number/toU8a.spec.ts +50 -0
  187. package/src/number/toU8a.ts +25 -0
  188. package/src/object/clear.spec.ts +14 -0
  189. package/src/object/clear.ts +16 -0
  190. package/src/object/copy.spec.ts +17 -0
  191. package/src/object/copy.ts +12 -0
  192. package/src/object/entries.spec.ts +20 -0
  193. package/src/object/entries.ts +12 -0
  194. package/src/object/index.ts +10 -0
  195. package/src/object/keys.spec.ts +19 -0
  196. package/src/object/keys.ts +10 -0
  197. package/src/object/property.spec.ts +149 -0
  198. package/src/object/property.ts +39 -0
  199. package/src/object/spread.spec.ts +36 -0
  200. package/src/object/spread.ts +38 -0
  201. package/src/object/values.spec.ts +19 -0
  202. package/src/object/values.ts +10 -0
  203. package/src/packageDetect.ts +13 -0
  204. package/src/packageInfo.ts +6 -0
  205. package/src/promisify.spec.ts +50 -0
  206. package/src/promisify.ts +30 -0
  207. package/src/string/camelCase.spec.ts +119 -0
  208. package/src/string/camelCase.ts +79 -0
  209. package/src/string/index.ts +12 -0
  210. package/src/string/lowerFirst.spec.ts +41 -0
  211. package/src/string/lowerFirst.ts +46 -0
  212. package/src/string/pascalCase.spec.ts +26 -0
  213. package/src/string/shorten.spec.ts +38 -0
  214. package/src/string/shorten.ts +24 -0
  215. package/src/string/toHex.spec.ts +26 -0
  216. package/src/string/toHex.ts +27 -0
  217. package/src/string/toU8a.spec.ts +31 -0
  218. package/src/string/toU8a.ts +28 -0
  219. package/src/string/upperFirst.spec.ts +41 -0
  220. package/src/stringify.spec.ts +33 -0
  221. package/src/stringify.ts +20 -0
  222. package/src/test/index.ts +4 -0
  223. package/src/test/performance.ts +92 -0
  224. package/src/types.ts +100 -0
  225. package/src/u8a/cmp.spec.ts +62 -0
  226. package/src/u8a/cmp.ts +50 -0
  227. package/src/u8a/concat.spec.ts +42 -0
  228. package/src/u8a/concat.ts +60 -0
  229. package/src/u8a/concatBuffer.ts +34 -0
  230. package/src/u8a/empty.spec.ts +46 -0
  231. package/src/u8a/empty.ts +22 -0
  232. package/src/u8a/eq.spec.ts +113 -0
  233. package/src/u8a/eq.ts +46 -0
  234. package/src/u8a/fixLength.spec.ts +34 -0
  235. package/src/u8a/fixLength.ts +34 -0
  236. package/src/u8a/index.ts +22 -0
  237. package/src/u8a/sorted.spec.ts +20 -0
  238. package/src/u8a/sorted.ts +22 -0
  239. package/src/u8a/toBigInt.spec.ts +106 -0
  240. package/src/u8a/toBigInt.ts +100 -0
  241. package/src/u8a/toBn.spec.ts +106 -0
  242. package/src/u8a/toBn.ts +101 -0
  243. package/src/u8a/toBuffer.spec.ts +20 -0
  244. package/src/u8a/toBuffer.ts +28 -0
  245. package/src/u8a/toFloat.spec.ts +86 -0
  246. package/src/u8a/toFloat.ts +26 -0
  247. package/src/u8a/toHex.spec.ts +94 -0
  248. package/src/u8a/toHex.ts +68 -0
  249. package/src/u8a/toHexBuffer.spec.ts +63 -0
  250. package/src/u8a/toHexBuffer.ts +30 -0
  251. package/src/u8a/toNumber.spec.ts +47 -0
  252. package/src/u8a/toNumber.ts +80 -0
  253. package/src/u8a/toString.spec.ts +36 -0
  254. package/src/u8a/toString.ts +26 -0
  255. package/src/u8a/toU8a.spec.ts +63 -0
  256. package/src/u8a/toU8a.ts +46 -0
  257. package/src/u8a/wrap.spec.ts +139 -0
  258. package/src/u8a/wrap.ts +65 -0
  259. package/tsconfig.build.json +19 -0
  260. package/tsconfig.spec.json +17 -0
@@ -0,0 +1,112 @@
1
+ // Copyright 2017-2025 @polkadot/util authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { BigInt } from '@pezkuwi/x-bigint';
5
+
6
+ /**
7
+ * @name _0n
8
+ * @summary BigInt constant for 0.
9
+ */
10
+ export const _0n = /*#__PURE__*/ BigInt(0);
11
+
12
+ /**
13
+ * @name _1n
14
+ * @summary BigInt constant for 1.
15
+ */
16
+ export const _1n = /*#__PURE__*/ BigInt(1);
17
+
18
+ /**
19
+ * @name _2n
20
+ * @summary BigInt constant for 2.
21
+ */
22
+ export const _2n = /*#__PURE__*/ BigInt(2);
23
+
24
+ /**
25
+ * @name _3n
26
+ * @summary BigInt constant for 3.
27
+ */
28
+ export const _3n = /*#__PURE__*/ BigInt(3);
29
+
30
+ /**
31
+ * @name _4n
32
+ * @summary BigInt constant for 4.
33
+ */
34
+ export const _4n = /*#__PURE__*/ BigInt(4);
35
+
36
+ /**
37
+ * @name _5n
38
+ * @summary BigInt constant for 5.
39
+ */
40
+ export const _5n = /*#__PURE__*/ BigInt(5);
41
+
42
+ /**
43
+ * @name _6n
44
+ * @summary BigInt constant for 6.
45
+ */
46
+ export const _6n = /*#__PURE__*/ BigInt(6);
47
+
48
+ /**
49
+ * @name _7n
50
+ * @summary BigInt constant for 7.
51
+ */
52
+ export const _7n = /*#__PURE__*/ BigInt(7);
53
+
54
+ /**
55
+ * @name _8n
56
+ * @summary BigInt constant for 8.
57
+ */
58
+ export const _8n = /*#__PURE__*/ BigInt(8);
59
+
60
+ /**
61
+ * @name _9n
62
+ * @summary BigInt constant for 9.
63
+ */
64
+ export const _9n = /*#__PURE__*/ BigInt(9);
65
+
66
+ /**
67
+ * @name _10n
68
+ * @summary BigInt constant for 10.
69
+ */
70
+ export const _10n = /*#__PURE__*/ BigInt(10);
71
+
72
+ /**
73
+ * @name _100n
74
+ * @summary BigInt constant for 100.
75
+ */
76
+ export const _100n = /*#__PURE__*/ BigInt(100);
77
+
78
+ /**
79
+ * @name _1000n
80
+ * @summary BigInt constant for 1000.
81
+ */
82
+ export const _1000n = /*#__PURE__*/ BigInt(1_000);
83
+
84
+ /**
85
+ * @name _1Mn
86
+ * @summary BigInt constant for 1,000,000 (million).
87
+ */
88
+ export const _1Mn = /*#__PURE__*/ BigInt(1_000_000);
89
+
90
+ /**
91
+ * @name _1Bn
92
+ * @summary BigInt constant for 1,000,000,000 (billion).
93
+ */
94
+ export const _1Bn = /*#__PURE__*/ BigInt(1_000_000_000);
95
+
96
+ /**
97
+ * @name _1Qn
98
+ * @summary BigInt constant for 1,000,000,000,000,000,000 (quitillion).
99
+ */
100
+ export const _1Qn = _1Bn * _1Bn;
101
+
102
+ /**
103
+ * @name _2pow53n
104
+ * @summary BigInt constant for MAX_SAFE_INTEGER
105
+ */
106
+ export const _2pow53n = /*#__PURE__*/ BigInt(Number.MAX_SAFE_INTEGER);
107
+
108
+ /**
109
+ * @name _sqrt2pow53n
110
+ * @summary BigInt constant for Math.sqrt(MAX_SAFE_INTEGER)
111
+ */
112
+ export const _sqrt2pow53n = /*#__PURE__*/ BigInt(94906265);
@@ -0,0 +1,23 @@
1
+ // Copyright 2017-2025 @polkadot/util authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /** @internal */
5
+ export function createCmp <T> (cmp: (a: T, b: T) => boolean): (...items: T[]) => T {
6
+ return (...items: T[]): T => {
7
+ const count = items.length;
8
+
9
+ if (count === 0) {
10
+ throw new Error('Must provide one or more arguments');
11
+ }
12
+
13
+ let result = items[0];
14
+
15
+ for (let i = 1; i < count; i++) {
16
+ if (cmp(items[i], result)) {
17
+ result = items[i];
18
+ }
19
+ }
20
+
21
+ return result;
22
+ };
23
+ }
@@ -0,0 +1,16 @@
1
+ // Copyright 2017-2025 @polkadot/util authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /**
5
+ * @summary Utility methods to convert to and from `bigint` objects
6
+ */
7
+
8
+ // all named
9
+ export { nMax, nMin } from './min.js';
10
+ export { nSqrt } from './sqrt.js';
11
+ export { nToBigInt } from './toBigInt.js';
12
+ export { nToHex } from './toHex.js';
13
+ export { nToU8a } from './toU8a.js';
14
+
15
+ // all starred
16
+ export * from './consts.js';
@@ -0,0 +1,32 @@
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 { nMax } from './index.js';
7
+
8
+ describe('nMax', (): void => {
9
+ it('finds maximum (sorted)', (): void => {
10
+ expect(
11
+ nMax(1n, 2n, 3n)
12
+ ).toEqual(3n);
13
+ });
14
+
15
+ it('finds maximum (unsorted)', (): void => {
16
+ expect(
17
+ nMax(2n, 3n, 1n)
18
+ ).toEqual(3n);
19
+ });
20
+
21
+ it('returns a single item', (): void => {
22
+ expect(
23
+ nMax(1n)
24
+ ).toEqual(1n);
25
+ });
26
+
27
+ it('fails when no items are available', (): void => {
28
+ expect(
29
+ () => nMax()
30
+ ).toThrow(/Must provide one or more arguments/);
31
+ });
32
+ });
@@ -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 { nMin } from './index.js';
7
+
8
+ describe('nMin', (): void => {
9
+ it('finds BN minimum', (): void => {
10
+ expect(
11
+ nMin(2n, 1n, 3n)
12
+ ).toEqual(1n);
13
+ });
14
+
15
+ it('returns a single item', (): void => {
16
+ expect(
17
+ nMin(1n)
18
+ ).toEqual(1n);
19
+ });
20
+
21
+ it('fails when no items are available', (): void => {
22
+ expect(
23
+ () => nMin()
24
+ ).toThrow(/Must provide one or more arguments/);
25
+ });
26
+ });
package/src/bi/min.ts ADDED
@@ -0,0 +1,16 @@
1
+ // Copyright 2017-2025 @polkadot/util authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { createCmp } from './helpers.js';
5
+
6
+ /**
7
+ * @name nMax
8
+ * @summary Finds and returns the highest value in an array of bigint.
9
+ */
10
+ export const nMax = /*#__PURE__*/ createCmp<bigint>((a, b) => a > b);
11
+
12
+ /**
13
+ * @name nMin
14
+ * @summary Finds and returns the lowest value in an array of bigint.
15
+ */
16
+ export const nMin = /*#__PURE__*/ createCmp<bigint>((a, b) => a < b);
@@ -0,0 +1,59 @@
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 { BN } from '../bn/index.js';
7
+ import { _sqrt2pow53n, nSqrt } from './index.js';
8
+
9
+ // eslint-disable-next-line jest/no-export
10
+ export const TESTS: [value: string | number | BN | bigint, expected: string | number][] = [
11
+ [0, 0],
12
+ [1, 1],
13
+ [4, 2],
14
+ [256 * 256, 256],
15
+ [Number.MAX_SAFE_INTEGER, 94906265],
16
+ [new BN(Number.MAX_SAFE_INTEGER).iaddn(256), 94906265],
17
+ [12345678n * 12345679n, 12345678],
18
+ // test cases below from https://github.com/Aisse-258/bigint-isqrt/blob/f5254b9750841959022461c1353437a07a08f501/test/sqrt-test.js
19
+ [
20
+ '54866395443885995655625',
21
+ '234235768925'
22
+ ],
23
+ [
24
+ '82120471531550314555681345949499512621827274120673745141541602816614526075010755373654280259022317599142038423759320355177481886719814621305828811322920076213800348341464996337890625',
25
+ '9062034624274524065844376014975805577107171799890766992670739972241112960081909332275390625'
26
+ ],
27
+ [
28
+ '2068290204957779940494571454815902632050207752627007664813877111611972735314294907791993008455381265730624848057015476004143868408297250386512042711005577373327434315310235583583151773358353534031397385969275475421659036494173765903706963544628565027679444680637920371210258368358706248446698622704138943704561842220533563976838506992543814273403403362954987589637283890281226038798503294069403370486523697009383229578343444337394508531937972775639593763364402389410142290440075517598007930025487176173228232153685095699851764630884360483051570513804913157899314303269924424908606245851172031453725690942288677622973423417902662711606364188470325930406288190754356367655606430407552275285889968860367977288564364154999338284288438279464648663835830140826171534470717958048887869721791862539529444820484648908879364324577951008050851801647916673254009490596379549192296404841454780884358889449367135382779230574781333008561195244620152128505091883408484094800737142399855337799331037607433934520934135669364954550499058123797911762228642423567268540444910163468496805547450495819213412636594319148417857753712526066272727725420886898680432958409892237191284583904066400736417812258327303739879128916456683685232170600928808798462776174857486589598776587550843543720784829419478239563366491666155716208452087857080883906200829769222868347286071365156305396955420561718593130305703074908891135467193627721682044757187980688062367562451981939375400543212890625',
29
+ '45478458691536369397924851088318871329614555665525810477032568598360481742967756704524034986984134271202405190906906195123751965012732122548540879928668891416468847696038707003466943811913626146981446753434347552196868717612248915137033581428310504778371173112125038060138252719589122657221314190626986951627417768579641354255445988772645883238873672997347648890137913979299598785628581961589739331483895853398160785485300758458294999124984266041998145956341386549681176624661615465976970855592467853824812945820721933491609237311130118125761849911376495970746282571994312288380577879004620824004760109618074670810797544446334767103935754106103208789391060008291027361311679876439695844637933763721093782805837690830230712890625'
30
+ ]
31
+ ];
32
+
33
+ describe('nSqrt', (): void => {
34
+ it('fails on < 0 roots', (): void => {
35
+ expect(
36
+ () => nSqrt(-1n)
37
+ ).toThrow(/negative numbers is not supported/);
38
+ });
39
+
40
+ it('has the correct constant for sqrt(Number.MAX_SAFE_INTEGER)', (): void => {
41
+ expect(
42
+ BigInt(
43
+ ~~Math.sqrt(
44
+ Number.MAX_SAFE_INTEGER
45
+ )
46
+ ) === _sqrt2pow53n
47
+ ).toEqual(true);
48
+ });
49
+
50
+ describe('conversion tests', (): void => {
51
+ TESTS.forEach(([value, expected], i): void => {
52
+ it(`#${i}: calcs ${expected}`, (): void => {
53
+ expect(
54
+ nSqrt(value) === BigInt(expected)
55
+ ).toEqual(true);
56
+ });
57
+ });
58
+ });
59
+ });
package/src/bi/sqrt.ts ADDED
@@ -0,0 +1,43 @@
1
+ // Copyright 2017-2025 @polkadot/util authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { BN } from '../bn/index.js';
5
+ import type { ToBigInt, ToBn } from '../types.js';
6
+
7
+ import { BigInt } from '@pezkuwi/x-bigint';
8
+
9
+ import { _0n, _1n, _2pow53n, _sqrt2pow53n } from './consts.js';
10
+ import { nToBigInt } from './toBigInt.js';
11
+
12
+ /**
13
+ * @name nSqrt
14
+ * @summary Calculates the integer square root of a bigint
15
+ */
16
+ export function nSqrt <ExtToBn extends ToBn | ToBigInt> (value: ExtToBn | BN | bigint | string | number | null): bigint {
17
+ const n = nToBigInt(value);
18
+
19
+ if (n < _0n) {
20
+ throw new Error('square root of negative numbers is not supported');
21
+ }
22
+
23
+ // https://stackoverflow.com/questions/53683995/javascript-big-integer-square-root/
24
+ // shortcut <= 2^53 - 1 to use the JS utils
25
+ if (n <= _2pow53n) {
26
+ // ~~ is more performant that Math.floor
27
+ return BigInt(~~Math.sqrt(Number(n)));
28
+ }
29
+
30
+ // Use sqrt(MAX_SAFE_INTEGER) as starting point. since we already know the
31
+ // output will be larger than this, we expect this to be a safe start
32
+ let x0 = _sqrt2pow53n;
33
+
34
+ while (true) {
35
+ const x1 = ((n / x0) + x0) >> _1n;
36
+
37
+ if (x0 === x1 || (x0 === (x1 - _1n))) {
38
+ return x0;
39
+ }
40
+
41
+ x0 = x1;
42
+ }
43
+ }
@@ -0,0 +1,69 @@
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 { BN } from '../bn/index.js';
7
+ import { nToBigInt } from './index.js';
8
+
9
+ describe('nToBigInt', (): void => {
10
+ it('converts null values to 0x00', (): void => {
11
+ expect(
12
+ nToBigInt(null)
13
+ ).toEqual(0n);
14
+ });
15
+
16
+ it('converts 0x values to 0x00', (): void => {
17
+ expect(
18
+ nToBigInt('0x')
19
+ ).toEqual(0n);
20
+ });
21
+
22
+ it('converts BN values to bigint', (): void => {
23
+ expect(
24
+ nToBigInt(new BN(128))
25
+ ).toEqual(128n);
26
+ });
27
+
28
+ it('converts BigInt values to bigint', (): void => {
29
+ expect(
30
+ nToBigInt(128821n)
31
+ ).toEqual(128821n);
32
+ });
33
+
34
+ it('converts number values to bigint', (): void => {
35
+ expect(
36
+ nToBigInt(128)
37
+ ).toEqual(128n);
38
+ });
39
+
40
+ it('converts string to bigint', (): void => {
41
+ expect(
42
+ nToBigInt('123')
43
+ ).toEqual(123n);
44
+ });
45
+
46
+ it('converts hex to bigint', (): void => {
47
+ expect(
48
+ nToBigInt('0x0123')
49
+ ).toEqual(0x123n);
50
+ });
51
+
52
+ it('converts Compact to bigint (via toBn)', (): void => {
53
+ expect(
54
+ nToBigInt({
55
+ something: 'test',
56
+ toBn: () => new BN(1234)
57
+ })
58
+ ).toEqual(1234n);
59
+ });
60
+
61
+ it('converts Compact to bigint (via toBigInt)', (): void => {
62
+ expect(
63
+ nToBigInt({
64
+ something: 'test',
65
+ toBigInt: () => 1234n
66
+ })
67
+ ).toEqual(1234n);
68
+ });
69
+ });
@@ -0,0 +1,33 @@
1
+ // Copyright 2017-2025 @polkadot/util authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { BN } from '../bn/bn.js';
5
+ import type { ToBigInt, ToBn } from '../types.js';
6
+
7
+ import { BigInt } from '@pezkuwi/x-bigint';
8
+
9
+ import { hexToBigInt } from '../hex/toBigInt.js';
10
+ import { isBn } from '../is/bn.js';
11
+ import { isHex } from '../is/hex.js';
12
+ import { isToBigInt } from '../is/toBigInt.js';
13
+ import { isToBn } from '../is/toBn.js';
14
+
15
+ /**
16
+ * @name nToBigInt
17
+ * @summary Creates a bigInt value from a BN, bigint, string (base 10 or hex) or number input.
18
+ */
19
+ export function nToBigInt <ExtToBn extends ToBigInt | ToBn> (value?: ExtToBn | BN | bigint | string | number | null): bigint {
20
+ return typeof value === 'bigint'
21
+ ? value
22
+ : !value
23
+ ? BigInt(0)
24
+ : isHex(value)
25
+ ? hexToBigInt(value.toString())
26
+ : isBn(value)
27
+ ? BigInt(value.toString())
28
+ : isToBigInt(value)
29
+ ? value.toBigInt()
30
+ : isToBn(value)
31
+ ? BigInt(value.toBn().toString())
32
+ : BigInt(value);
33
+ }
@@ -0,0 +1,56 @@
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 { nToHex } from './index.js';
7
+
8
+ describe('nToHex', (): void => {
9
+ it('converts null values to 0x00', (): void => {
10
+ expect(
11
+ nToHex(null)
12
+ ).toBe('0x00');
13
+ });
14
+
15
+ it('converts null values to 0x00000000 (with bitLength)', (): void => {
16
+ expect(
17
+ nToHex(null, { bitLength: 32 })
18
+ ).toBe('0x00000000');
19
+ });
20
+
21
+ it('converts values to a prefixed hex representation', (): void => {
22
+ expect(
23
+ nToHex(128)
24
+ ).toBe('0x80');
25
+ });
26
+
27
+ it('converts values to a prefixed hex representation (bitLength)', (): void => {
28
+ expect(
29
+ nToHex(128n, { bitLength: 16 })
30
+ ).toBe('0x0080');
31
+ });
32
+
33
+ it('converts values to a prefixed hex representation (bitLength + le)', (): void => {
34
+ expect(
35
+ nToHex(128, { bitLength: 16, isLe: true })
36
+ ).toBe('0x8000');
37
+ });
38
+
39
+ it('converts values to a prefixed hex representation (LE)', (): void => {
40
+ expect(
41
+ nToHex(128, { bitLength: 16, isLe: true })
42
+ ).toBe('0x8000');
43
+ });
44
+
45
+ it('handles negative numbers', (): void => {
46
+ expect(
47
+ nToHex(-1234, { isNegative: true })
48
+ ).toBe('0xfb2e');
49
+ });
50
+
51
+ it('handles negative numbers (with bitLength)', (): void => {
52
+ expect(
53
+ nToHex(-1234, { bitLength: 32, isNegative: true })
54
+ ).toBe('0xfffffb2e');
55
+ });
56
+ });
@@ -0,0 +1,16 @@
1
+ // Copyright 2017-2025 @polkadot/util authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { BN } from '../bn/bn.js';
5
+ import type { HexString, NumberOptions, ToBigInt, ToBn } from '../types.js';
6
+
7
+ import { u8aToHex } from '../u8a/index.js';
8
+ import { nToU8a } from './toU8a.js';
9
+
10
+ /**
11
+ * @name nToHex
12
+ * @summary Creates a hex value from a bigint object.
13
+ */
14
+ export function nToHex <ExtToBn extends ToBn | ToBigInt> (value?: ExtToBn | BN | bigint | number | null, { bitLength = -1, isLe = false, isNegative = false }: NumberOptions = {}): HexString {
15
+ return u8aToHex(nToU8a(value || 0, { bitLength, isLe, isNegative }));
16
+ }
@@ -0,0 +1,99 @@
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 { arrayRange } from '../array/index.js';
7
+ import { perf } from '../test/index.js';
8
+ import { nToU8a } from './index.js';
9
+
10
+ // eslint-disable-next-line jest/no-export
11
+ export const TESTS: [isLe: boolean, isNegative: boolean, numarr: number[], strval: string][] = [
12
+ // LE, positive numbers
13
+ [true, false, [0x12], '18'],
14
+ [true, false, [0x12, 0x34], '13330'],
15
+ [true, false, [0x12, 0x34, 0x56], '5649426'],
16
+ [true, false, [0x12, 0x34, 0x56, 0x78], '2018915346'],
17
+ [true, false, [0x12, 0x34, 0x56, 0x78, 0x9a], '663443878930'],
18
+ [true, false, [0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc], '207371629900818'],
19
+ [true, false, [0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78], '159954953172672629770948536149615195154'],
20
+ // LE, positive numbers (w/ signed flag)
21
+ [true, true, [12], '12'],
22
+ [true, true, [210, 4], '1234'],
23
+ [true, true, [64, 226, 1], '123456'],
24
+ [true, true, [21, 205, 91, 7], '123456789'],
25
+ [true, true, [203, 36, 104, 12, 8], '34567890123'],
26
+ [true, true, [255, 159, 114, 78, 24, 9], '9999999999999'],
27
+ // LE, negative numbers
28
+ [true, true, [244], '-12'],
29
+ [true, true, [46, 251], '-1234'],
30
+ [true, true, [192, 29, 254], '-123456'],
31
+ [true, true, [255, 255, 255, 255], '-1'],
32
+ [true, true, [254, 255, 255, 255], '-2'],
33
+ [true, true, [235, 50, 164, 248], '-123456789'],
34
+ [true, true, [0, 0, 0, 128], '-2147483648'],
35
+ [true, true, [0, 0, 0, 240], '-268435456'],
36
+ [true, true, [65, 86, 129, 173, 254], '-5678999999'],
37
+ [true, true, [1, 96, 141, 177, 231, 246], '-9999999999999'],
38
+ [true, true, [1, 0, 156, 88, 76, 73, 31, 242], '-999999999999999999'],
39
+ // BE
40
+ [false, false, [0x12], '18'],
41
+ [false, false, [0x12, 0x34], '4660'],
42
+ [false, false, [0x12, 0x34, 0x56], '1193046'],
43
+ [false, false, [0x12, 0x34, 0x56, 0x78], '305419896'],
44
+ [false, true, [0xf2, 0x34, 0x56, 0x78], '-231451016'],
45
+ [false, false, [0x12, 0x34, 0x56, 0x78, 0x9a], '78187493530'],
46
+ [false, false, [0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc], '20015998343868'],
47
+ [false, false, [0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78], '24197857161011715162171839636988778104']
48
+ ];
49
+
50
+ const ptest = arrayRange(65536).map((v) => [v]);
51
+
52
+ describe('nToU8a', (): void => {
53
+ describe('conversion tests', (): void => {
54
+ TESTS.forEach(([isLe, isNegative, numarr, strval], i): void => {
55
+ const bitLength = numarr.length * 8;
56
+
57
+ it(`#${i}: converts from ${strval} (bitLength=${bitLength}, isLe=${isLe}, isNegative=${isNegative})`, (): void => {
58
+ expect(
59
+ nToU8a(
60
+ BigInt(strval),
61
+ { bitLength, isLe, isNegative }
62
+ )
63
+ ).toEqual(new Uint8Array(numarr));
64
+ });
65
+ });
66
+ });
67
+
68
+ it('converts null values to 0x00', (): void => {
69
+ expect(
70
+ nToU8a(null)
71
+ ).toEqual(new Uint8Array(1));
72
+ });
73
+
74
+ it('converts null values to 0x00000000 (bitLength)', (): void => {
75
+ expect(
76
+ nToU8a(null, { bitLength: 32 })
77
+ ).toEqual(new Uint8Array([0, 0, 0, 0]));
78
+ });
79
+
80
+ it('converts values to a prefixed hex representation', (): void => {
81
+ expect(
82
+ nToU8a(0x123456n, { isLe: false })
83
+ ).toEqual(new Uint8Array([0x12, 0x34, 0x56]));
84
+ });
85
+
86
+ it('converts values to a prefixed hex representation (bitLength)', (): void => {
87
+ expect(
88
+ nToU8a(0x123456n, { bitLength: 32, isLe: false })
89
+ ).toEqual(new Uint8Array([0x00, 0x12, 0x34, 0x56]));
90
+ });
91
+
92
+ it('converts using little endian (as set)', (): void => {
93
+ expect(
94
+ nToU8a(0x123456n, { bitLength: 32, isLe: true })
95
+ ).toEqual(new Uint8Array([0x56, 0x34, 0x12, 0x00]));
96
+ });
97
+
98
+ perf('nToU8a', 250000, ptest, nToU8a);
99
+ });
@@ -0,0 +1,72 @@
1
+ // Copyright 2017-2025 @polkadot/util authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { BN } from '../bn/bn.js';
5
+ import type { NumberOptions, ToBigInt, ToBn } from '../types.js';
6
+
7
+ import { BigInt } from '@pezkuwi/x-bigint';
8
+
9
+ import { _0n, _1n } from './consts.js';
10
+ import { nToBigInt } from './toBigInt.js';
11
+
12
+ const DIV = BigInt(256);
13
+ const NEG_MASK = BigInt(0xff);
14
+
15
+ function toU8a (value: bigint, isLe: boolean, isNegative: boolean): Uint8Array {
16
+ const arr: number[] = [];
17
+ const withSigned = isNegative && (value < _0n);
18
+
19
+ if (withSigned) {
20
+ value = (value + _1n) * -_1n;
21
+ }
22
+
23
+ while (value !== _0n) {
24
+ const mod = value % DIV;
25
+ const val = Number(
26
+ withSigned
27
+ ? mod ^ NEG_MASK
28
+ : mod
29
+ );
30
+
31
+ if (isLe) {
32
+ arr.push(val);
33
+ } else {
34
+ arr.unshift(val);
35
+ }
36
+
37
+ value = (value - mod) / DIV;
38
+ }
39
+
40
+ return Uint8Array.from(arr);
41
+ }
42
+
43
+ /**
44
+ * @name nToU8a
45
+ * @summary Creates a Uint8Array object from a bigint.
46
+ */
47
+ export function nToU8a <ExtToBn extends ToBn | ToBigInt> (value?: ExtToBn | BN | bigint | number | null, { bitLength = -1, isLe = true, isNegative = false }: NumberOptions = {}): Uint8Array {
48
+ const valueBi = nToBigInt(value);
49
+
50
+ if (valueBi === _0n) {
51
+ return bitLength === -1
52
+ ? new Uint8Array(1)
53
+ : new Uint8Array(Math.ceil((bitLength || 0) / 8));
54
+ }
55
+
56
+ const u8a = toU8a(valueBi, isLe, isNegative);
57
+
58
+ if (bitLength === -1) {
59
+ return u8a;
60
+ }
61
+
62
+ const byteLength = Math.ceil((bitLength || 0) / 8);
63
+ const output = new Uint8Array(byteLength);
64
+
65
+ if (isNegative) {
66
+ output.fill(0xff);
67
+ }
68
+
69
+ output.set(u8a, isLe ? 0 : byteLength - u8a.length);
70
+
71
+ return output;
72
+ }
package/src/bn/bn.ts ADDED
@@ -0,0 +1,6 @@
1
+ // Copyright 2017-2025 @polkadot/util authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import BN from 'bn.js';
5
+
6
+ export { BN };