@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,38 @@
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, BN_HUNDRED, BN_ONE, BN_TEN, BN_THOUSAND, BN_ZERO } from './index.js';
7
+
8
+ describe('consts', (): void => {
9
+ it('BN_ZERO equals 0', (): void => {
10
+ expect(
11
+ BN_ZERO
12
+ ).toEqual(new BN(0));
13
+ });
14
+
15
+ it('BN_ONE equals 1', (): void => {
16
+ expect(
17
+ BN_ONE
18
+ ).toEqual(new BN(1));
19
+ });
20
+
21
+ it('BN_TEN equals 10', (): void => {
22
+ expect(
23
+ BN_TEN
24
+ ).toEqual(new BN(10));
25
+ });
26
+
27
+ it('BN_HUNDRED equals 100', (): void => {
28
+ expect(
29
+ BN_HUNDRED
30
+ ).toEqual(new BN(100));
31
+ });
32
+
33
+ it('BN_THOUSAND equals 1000', (): void => {
34
+ expect(
35
+ BN_THOUSAND
36
+ ).toEqual(new BN(1000));
37
+ });
38
+ });
@@ -0,0 +1,112 @@
1
+ // Copyright 2017-2025 @polkadot/util authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { BN } from './bn.js';
5
+
6
+ /**
7
+ * @name BN_ZERO
8
+ * @summary BN constant for 0.
9
+ */
10
+ export const BN_ZERO: BN = /*#__PURE__*/ new BN(0);
11
+
12
+ /**
13
+ * @name BN_ONE
14
+ * @summary BN constant for 1.
15
+ */
16
+ export const BN_ONE: BN = /*#__PURE__*/ new BN(1);
17
+
18
+ /**
19
+ * @name BN_TWO
20
+ * @summary BN constant for 2.
21
+ */
22
+ export const BN_TWO: BN = /*#__PURE__*/ new BN(2);
23
+
24
+ /**
25
+ * @name BN_THREE
26
+ * @summary BN constant for 3.
27
+ */
28
+ export const BN_THREE: BN = /*#__PURE__*/ new BN(3);
29
+
30
+ /**
31
+ * @name BN_FOUR
32
+ * @summary BN constant for 4.
33
+ */
34
+ export const BN_FOUR: BN = /*#__PURE__*/ new BN(4);
35
+
36
+ /**
37
+ * @name BN_FIVE
38
+ * @summary BN constant for 5.
39
+ */
40
+ export const BN_FIVE: BN = /*#__PURE__*/ new BN(5);
41
+
42
+ /**
43
+ * @name BN_SIX
44
+ * @summary BN constant for 6.
45
+ */
46
+ export const BN_SIX: BN = /*#__PURE__*/ new BN(6);
47
+
48
+ /**
49
+ * @name BN_SEVEN
50
+ * @summary BN constant for 7.
51
+ */
52
+ export const BN_SEVEN: BN = /*#__PURE__*/ new BN(7);
53
+
54
+ /**
55
+ * @name BN_EIGHT
56
+ * @summary BN constant for 8.
57
+ */
58
+ export const BN_EIGHT: BN = /*#__PURE__*/ new BN(8);
59
+
60
+ /**
61
+ * @name BN_NINE
62
+ * @summary BN constant for 9.
63
+ */
64
+ export const BN_NINE: BN = /*#__PURE__*/ new BN(9);
65
+
66
+ /**
67
+ * @name BN_TEN
68
+ * @summary BN constant for 10.
69
+ */
70
+ export const BN_TEN: BN = /*#__PURE__*/ new BN(10);
71
+
72
+ /**
73
+ * @name BN_HUNDRED
74
+ * @summary BN constant for 100.
75
+ */
76
+ export const BN_HUNDRED: BN = /*#__PURE__*/ new BN(100);
77
+
78
+ /**
79
+ * @name BN_THOUSAND
80
+ * @summary BN constant for 1,000.
81
+ */
82
+ export const BN_THOUSAND: BN = /*#__PURE__*/ new BN(1_000);
83
+
84
+ /**
85
+ * @name BN_MILLION
86
+ * @summary BN constant for 1,000,000.
87
+ */
88
+ export const BN_MILLION: BN = /*#__PURE__*/ new BN(1_000_000);
89
+
90
+ /**
91
+ * @name BN_BILLION
92
+ * @summary BN constant for 1,000,000,000.
93
+ */
94
+ export const BN_BILLION: BN = /*#__PURE__*/ new BN(1_000_000_000);
95
+
96
+ /**
97
+ * @name BN_QUINTILL
98
+ * @summary BN constant for 1,000,000,000,000,000,000.
99
+ */
100
+ export const BN_QUINTILL: BN = BN_BILLION.mul(BN_BILLION);
101
+
102
+ /**
103
+ * @name BN_MAX_INTEGER
104
+ * @summary BN constant for MAX_SAFE_INTEGER
105
+ */
106
+ export const BN_MAX_INTEGER: BN = /*#__PURE__*/ new BN(Number.MAX_SAFE_INTEGER);
107
+
108
+ /**
109
+ * @name BN_SQRT_MAX_INTEGER
110
+ * @summary BN constant for Math.sqrt(MAX_SAFE_INTEGER)
111
+ */
112
+ export const BN_SQRT_MAX_INTEGER: BN = /*#__PURE__*/ new BN(94906265);
@@ -0,0 +1,15 @@
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 { isFunction } from '../is/function.js';
7
+ import { bnFromHex } from './index.js';
8
+
9
+ describe('bnFromHex', (): void => {
10
+ it('exists as a function', (): void => {
11
+ expect(
12
+ isFunction(bnFromHex)
13
+ ).toEqual(true);
14
+ });
15
+ });
@@ -0,0 +1,4 @@
1
+ // Copyright 2017-2025 @polkadot/util authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { hexToBn as bnFromHex } from '../hex/toBn.js';
@@ -0,0 +1,18 @@
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 `BN` objects
6
+ */
7
+
8
+ // all named
9
+ export { BN } from './bn.js';
10
+ export { bnFromHex } from './fromHex.js';
11
+ export { bnMax, bnMin } from './min.js';
12
+ export { bnSqrt } from './sqrt.js';
13
+ export { bnToBn } from './toBn.js';
14
+ export { bnToHex } from './toHex.js';
15
+ export { bnToU8a } from './toU8a.js';
16
+
17
+ // all starred
18
+ 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 { BN, bnMax } from './index.js';
7
+
8
+ describe('bnMax', (): void => {
9
+ it('finds BN maximum (sorted)', (): void => {
10
+ expect(
11
+ bnMax(new BN(1), new BN(2), new BN(3))
12
+ ).toEqual(new BN(3));
13
+ });
14
+
15
+ it('finds BN maximum (unsorted)', (): void => {
16
+ expect(
17
+ bnMax(new BN(2), new BN(3), new BN(1))
18
+ ).toEqual(new BN(3));
19
+ });
20
+
21
+ it('returns a single item', (): void => {
22
+ expect(
23
+ bnMax(new BN(1))
24
+ ).toEqual(new BN(1));
25
+ });
26
+
27
+ it('fails when no items are available', (): void => {
28
+ expect(
29
+ () => bnMax()
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 { BN, bnMin } from './index.js';
7
+
8
+ describe('bnMin', (): void => {
9
+ it('finds BN minimum', (): void => {
10
+ expect(
11
+ bnMin(new BN(2), new BN(1), new BN(3))
12
+ ).toEqual(new BN(1));
13
+ });
14
+
15
+ it('returns a single item', (): void => {
16
+ expect(
17
+ bnMin(new BN(1))
18
+ ).toEqual(new BN(1));
19
+ });
20
+
21
+ it('fails when no items are available', (): void => {
22
+ expect(
23
+ () => bnMin()
24
+ ).toThrow(/Must provide one or more arguments/);
25
+ });
26
+ });
package/src/bn/min.ts ADDED
@@ -0,0 +1,36 @@
1
+ // Copyright 2017-2025 @polkadot/util authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { BN } from './bn.js';
5
+
6
+ import { createCmp } from '../bi/helpers.js';
7
+
8
+ /**
9
+ * @name bnMax
10
+ * @summary Finds and returns the highest value in an array of BNs.
11
+ * @example
12
+ * <BR>
13
+ *
14
+ * ```javascript
15
+ * import BN from 'bn.js';
16
+ * import { bnMax } from '@pezkuwi/util';
17
+ *
18
+ * bnMax([new BN(1), new BN(3), new BN(2)]).toString(); // => '3'
19
+ * ```
20
+ */
21
+ export const bnMax = /*#__PURE__*/ createCmp<BN>((a, b) => a.gt(b));
22
+
23
+ /**
24
+ * @name bnMin
25
+ * @summary Finds and returns the smallest value in an array of BNs.
26
+ * @example
27
+ * <BR>
28
+ *
29
+ * ```javascript
30
+ * import BN from 'bn.js';
31
+ * import { bnMin } from '@pezkuwi/util';
32
+ *
33
+ * bnMin([new BN(1), new BN(3), new BN(2)]).toString(); // => '1'
34
+ * ```
35
+ */
36
+ export const bnMin = /*#__PURE__*/ createCmp<BN>((a, b) => a.lt(b));
@@ -0,0 +1,37 @@
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 { TESTS } from '../bi/sqrt.spec.js';
7
+ import { BN, BN_SQRT_MAX_INTEGER, bnSqrt } from './index.js';
8
+
9
+ describe('bnSqrt', (): void => {
10
+ it('fails on < 0 roots', (): void => {
11
+ expect(
12
+ () => bnSqrt(new BN(-1))
13
+ ).toThrow(/negative numbers is not supported/);
14
+ });
15
+
16
+ it('has the correct constant for sqrt(Number.MAX_SAFE_INTEGER)', (): void => {
17
+ expect(
18
+ BN_SQRT_MAX_INTEGER.eq(
19
+ new BN(
20
+ ~~Math.sqrt(
21
+ Number.MAX_SAFE_INTEGER
22
+ )
23
+ )
24
+ )
25
+ ).toEqual(true);
26
+ });
27
+
28
+ describe('conversion tests', (): void => {
29
+ TESTS.forEach(([value, expected], i): void => {
30
+ it(`#${i}: calcs ${expected}`, (): void => {
31
+ expect(
32
+ bnSqrt(value).eq(new BN(expected))
33
+ ).toEqual(true);
34
+ });
35
+ });
36
+ });
37
+ });
package/src/bn/sqrt.ts ADDED
@@ -0,0 +1,50 @@
1
+ // Copyright 2017-2025 @polkadot/util authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { ToBn } from '../types.js';
5
+
6
+ import { BN } from './bn.js';
7
+ import { BN_MAX_INTEGER, BN_ONE, BN_SQRT_MAX_INTEGER } from './consts.js';
8
+ import { bnToBn } from './toBn.js';
9
+
10
+ /**
11
+ * @name bnSqrt
12
+ * @summary Calculates the integer square root of a BN
13
+ * @example
14
+ * <BR>
15
+ *
16
+ * ```javascript
17
+ * import BN from 'bn.js';
18
+ * import { bnSqrt } from '@pezkuwi/util';
19
+ *
20
+ * bnSqrt(new BN(16)).toString(); // => '4'
21
+ * ```
22
+ */
23
+ export function bnSqrt <ExtToBn extends ToBn> (value: ExtToBn | BN | bigint | string | number | null): BN {
24
+ const n = bnToBn(value);
25
+
26
+ if (n.isNeg()) {
27
+ throw new Error('square root of negative numbers is not supported');
28
+ }
29
+
30
+ // https://stackoverflow.com/questions/53683995/javascript-big-integer-square-root/
31
+ // shortcut <= 2^53 - 1 to use the JS utils
32
+ if (n.lte(BN_MAX_INTEGER)) {
33
+ // ~~ More performant version of Math.floor
34
+ return new BN(~~Math.sqrt(n.toNumber()));
35
+ }
36
+
37
+ // Use sqrt(MAX_SAFE_INTEGER) as starting point. since we already know the
38
+ // output will be larger than this, we expect this to be a safe start
39
+ let x0 = BN_SQRT_MAX_INTEGER.clone();
40
+
41
+ while (true) {
42
+ const x1 = n.div(x0).iadd(x0).ishrn(1);
43
+
44
+ if (x0.eq(x1) || x0.eq(x1.sub(BN_ONE))) {
45
+ return x0;
46
+ }
47
+
48
+ x0 = x1;
49
+ }
50
+ }
@@ -0,0 +1,53 @@
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, bnToBn } from './index.js';
7
+
8
+ describe('bnToBn', (): void => {
9
+ it('converts null values to 0x00', (): void => {
10
+ expect(
11
+ bnToBn(null).toNumber()
12
+ ).toEqual(0);
13
+ });
14
+
15
+ it('converts BN values to BN', (): void => {
16
+ expect(
17
+ bnToBn(new BN(128)).toNumber()
18
+ ).toEqual(128);
19
+ });
20
+
21
+ it('converts BigInt values to BN', (): void => {
22
+ expect(
23
+ bnToBn(128821n).toNumber()
24
+ ).toEqual(128821);
25
+ });
26
+
27
+ it('converts number values to BN', (): void => {
28
+ expect(
29
+ bnToBn(128).toNumber()
30
+ ).toEqual(128);
31
+ });
32
+
33
+ it('converts string to BN', (): void => {
34
+ expect(
35
+ bnToBn('123').toNumber()
36
+ ).toEqual(123);
37
+ });
38
+
39
+ it('converts hex to BN', (): void => {
40
+ expect(
41
+ bnToBn('0x0123').toNumber()
42
+ ).toEqual(0x123);
43
+ });
44
+
45
+ it('converts Compact to BN', (): void => {
46
+ expect(
47
+ bnToBn({
48
+ something: 'test',
49
+ toBn: (): BN => new BN(1234)
50
+ }).toNumber()
51
+ ).toEqual(1234);
52
+ });
53
+ });
package/src/bn/toBn.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 { ToBigInt, ToBn } from '../types.js';
5
+
6
+ import { hexToBn } from '../hex/toBn.js';
7
+ import { isBigInt } from '../is/bigInt.js';
8
+ import { isHex } from '../is/hex.js';
9
+ import { isToBigInt } from '../is/toBigInt.js';
10
+ import { isToBn } from '../is/toBn.js';
11
+ import { BN } from './bn.js';
12
+
13
+ /**
14
+ * @name bnToBn
15
+ * @summary Creates a BN value from a BN, bigint, string (base 10 or hex) or number input.
16
+ * @description
17
+ * `null` inputs returns a `0x0` result, BN values returns the value, numbers returns a BN representation.
18
+ * @example
19
+ * <BR>
20
+ *
21
+ * ```javascript
22
+ * import BN from 'bn.js';
23
+ * import { bnToBn } from '@pezkuwi/util';
24
+ *
25
+ * bnToBn(0x1234); // => BN(0x1234)
26
+ * bnToBn(new BN(0x1234)); // => BN(0x1234)
27
+ * ```
28
+ */
29
+ export function bnToBn <ExtToBn extends ToBigInt | ToBn> (value?: ExtToBn | BN | bigint | string | number | null): BN {
30
+ return value
31
+ ? BN.isBN(value)
32
+ ? value
33
+ : isHex(value)
34
+ ? hexToBn(value.toString())
35
+ : isBigInt(value)
36
+ ? new BN(value.toString())
37
+ : isToBn(value)
38
+ ? value.toBn()
39
+ : isToBigInt(value)
40
+ ? new BN(value.toBigInt().toString())
41
+ : new BN(value)
42
+ : new BN(0);
43
+ }
@@ -0,0 +1,50 @@
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, bnToHex } from './index.js';
7
+
8
+ describe('bnToHex', (): void => {
9
+ it('converts null values to 0x00', (): void => {
10
+ expect(
11
+ bnToHex(null)
12
+ ).toBe('0x00');
13
+ });
14
+
15
+ it('converts null values to 0x00000000 (with bitLength)', (): void => {
16
+ expect(
17
+ bnToHex(null, { bitLength: 32 })
18
+ ).toBe('0x00000000');
19
+ });
20
+
21
+ it('converts BN values to a prefixed hex representation', (): void => {
22
+ expect(
23
+ bnToHex(new BN(128))
24
+ ).toBe('0x80');
25
+ });
26
+
27
+ it('converts BN values to a prefixed hex representation (bitLength)', (): void => {
28
+ expect(
29
+ bnToHex(new BN(128), { bitLength: 16 })
30
+ ).toBe('0x0080');
31
+ });
32
+
33
+ it('converts BN values to a prefixed hex representation (LE)', (): void => {
34
+ expect(
35
+ bnToHex(new BN(128), { bitLength: 16, isLe: true })
36
+ ).toBe('0x8000');
37
+ });
38
+
39
+ it('handles negative numbers', (): void => {
40
+ expect(
41
+ bnToHex(new BN(-1234), { isNegative: true })
42
+ ).toBe('0xfb2e');
43
+ });
44
+
45
+ it('handles negative numbers (with bitLength)', (): void => {
46
+ expect(
47
+ bnToHex(new BN(-1234), { bitLength: 32, isNegative: true })
48
+ ).toBe('0xfffffb2e');
49
+ });
50
+ });
@@ -0,0 +1,27 @@
1
+ // Copyright 2017-2025 @polkadot/util authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { HexString, NumberOptions, ToBn } from '../types.js';
5
+ import type { BN } from './bn.js';
6
+
7
+ import { u8aToHex } from '../u8a/index.js';
8
+ import { bnToU8a } from './toU8a.js';
9
+
10
+ /**
11
+ * @name bnToHex
12
+ * @summary Creates a hex value from a BN.js bignumber object.
13
+ * @description
14
+ * `null` inputs returns a `0x` result, BN values return the actual value as a `0x` prefixed hex value. Anything that is not a BN object throws an error. With `bitLength` set, it fixes the number to the specified length.
15
+ * @example
16
+ * <BR>
17
+ *
18
+ * ```javascript
19
+ * import BN from 'bn.js';
20
+ * import { bnToHex } from '@pezkuwi/util';
21
+ *
22
+ * bnToHex(new BN(0x123456)); // => '0x123456'
23
+ * ```
24
+ */
25
+ export function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, { bitLength = -1, isLe = false, isNegative = false }: NumberOptions = {}): HexString {
26
+ return u8aToHex(bnToU8a(value, { bitLength, isLe, isNegative }));
27
+ }
@@ -0,0 +1,60 @@
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 { TESTS } from '../bi/toU8a.spec.js';
8
+ import { perf } from '../test/index.js';
9
+ import { BN, bnToU8a } from './index.js';
10
+
11
+ const ptest = arrayRange(65536).map((v) => [v]);
12
+
13
+ describe('bnToU8a', (): void => {
14
+ it('converts null values to 0x00', (): void => {
15
+ expect(
16
+ bnToU8a(null)
17
+ ).toEqual(new Uint8Array(1));
18
+ });
19
+
20
+ it('converts null values to 0x00000000 (bitLength)', (): void => {
21
+ expect(
22
+ bnToU8a(null, { bitLength: 32 })
23
+ ).toEqual(new Uint8Array([0, 0, 0, 0]));
24
+ });
25
+
26
+ it('converts BN values to a prefixed hex representation', (): void => {
27
+ expect(
28
+ bnToU8a(new BN(0x123456), { isLe: false })
29
+ ).toEqual(new Uint8Array([0x12, 0x34, 0x56]));
30
+ });
31
+
32
+ it('converts BN values to a prefixed hex representation (bitLength)', (): void => {
33
+ expect(
34
+ bnToU8a(new BN(0x123456), { bitLength: 32, isLe: false })
35
+ ).toEqual(new Uint8Array([0x00, 0x12, 0x34, 0x56]));
36
+ });
37
+
38
+ it('converts using little endian (as set)', (): void => {
39
+ expect(
40
+ bnToU8a(new BN(0x123456), { bitLength: 32, isLe: true })
41
+ ).toEqual(new Uint8Array([0x56, 0x34, 0x12, 0x00]));
42
+ });
43
+
44
+ describe('conversion tests', (): void => {
45
+ TESTS.forEach(([isLe, isNegative, numarr, strval], i): void => {
46
+ const bitLength = numarr.length * 8;
47
+
48
+ it(`#${i}: converts from ${strval} (bitLength=${bitLength}, isLe=${isLe}, isNegative=${isNegative})`, (): void => {
49
+ expect(
50
+ bnToU8a(
51
+ new BN(strval),
52
+ { bitLength, isLe, isNegative }
53
+ )
54
+ ).toEqual(new Uint8Array(numarr));
55
+ });
56
+ });
57
+ });
58
+
59
+ perf('bnToU8a', 250000, ptest, bnToU8a);
60
+ });
@@ -0,0 +1,45 @@
1
+ // Copyright 2017-2025 @polkadot/util authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { NumberOptions, ToBn } from '../types.js';
5
+ import type { BN } from './bn.js';
6
+
7
+ import { bnToBn } from './toBn.js';
8
+
9
+ const DEFAULT_OPTS: NumberOptions = { bitLength: -1, isLe: true, isNegative: false };
10
+
11
+ /**
12
+ * @name bnToU8a
13
+ * @summary Creates a Uint8Array object from a BN.
14
+ * @description
15
+ * `null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `BN` input values return the actual bytes value converted to a `Uint8Array`. Optionally convert using little-endian format if `isLE` is set.
16
+ * @example
17
+ * <BR>
18
+ *
19
+ * ```javascript
20
+ * import { bnToU8a } from '@pezkuwi/util';
21
+ *
22
+ * bnToU8a(new BN(0x1234)); // => [0x12, 0x34]
23
+ * ```
24
+ */
25
+ export function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, { bitLength = -1, isLe = true, isNegative = false } = DEFAULT_OPTS): Uint8Array {
26
+ const valueBn = bnToBn(value);
27
+ const byteLength = bitLength === -1
28
+ ? Math.ceil(valueBn.bitLength() / 8)
29
+ : Math.ceil((bitLength || 0) / 8);
30
+
31
+ if (!value) {
32
+ return bitLength === -1
33
+ ? new Uint8Array(1)
34
+ : new Uint8Array(byteLength);
35
+ }
36
+
37
+ const output = new Uint8Array(byteLength);
38
+ const bn = isNegative
39
+ ? valueBn.toTwos(byteLength * 8)
40
+ : valueBn;
41
+
42
+ output.set(bn.toArray(isLe ? 'le' : 'be', byteLength), 0);
43
+
44
+ return output;
45
+ }
@@ -0,0 +1,8 @@
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 `Buffer` objects
6
+ */
7
+
8
+ export { bufferToU8a } from './toU8a.js';
@@ -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 { bufferToU8a } from './index.js';
7
+
8
+ describe('bufferToU8a', (): void => {
9
+ it('returns an empty buffer when null provided', (): void => {
10
+ expect(
11
+ bufferToU8a(null)
12
+ ).toEqual(new Uint8Array());
13
+ });
14
+
15
+ it('returns a Uint8Buffer with the correct values', (): void => {
16
+ expect(
17
+ bufferToU8a(Buffer.from([128, 0, 10]))
18
+ ).toEqual(new Uint8Array([128, 0, 10]));
19
+ });
20
+ });