@w3ux/utils 1.0.1 → 1.1.1-beta.0

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/base.cjs CHANGED
@@ -33,6 +33,7 @@ __export(base_exports, {
33
33
  removeHexPrefix: () => removeHexPrefix,
34
34
  rmCommas: () => rmCommas,
35
35
  shuffle: () => shuffle,
36
+ stringToU8a: () => stringToU8a,
36
37
  withTimeout: () => withTimeout
37
38
  });
38
39
  module.exports = __toCommonJS(base_exports);
@@ -156,6 +157,13 @@ var isSuperset = (set, subset) => {
156
157
  };
157
158
  var maxBigInt = (...values) => values.reduce((max, current) => current > max ? current : max);
158
159
  var minBigInt = (...values) => values.reduce((min, current) => current < min ? current : min);
160
+ var stringToU8a = (input) => {
161
+ const u8a = new Uint8Array(input.length);
162
+ for (let i = 0; i < input.length; i++) {
163
+ u8a[i] = input.charCodeAt(i);
164
+ }
165
+ return u8a;
166
+ };
159
167
  // Annotate the CommonJS export names for ESM import in node:
160
168
  0 && (module.exports = {
161
169
  appendOr,
@@ -172,6 +180,7 @@ var minBigInt = (...values) => values.reduce((min, current) => current < min ? c
172
180
  removeHexPrefix,
173
181
  rmCommas,
174
182
  shuffle,
183
+ stringToU8a,
175
184
  withTimeout
176
185
  });
177
186
  /* @license Copyright 2024 w3ux authors & contributors
package/base.d.cts CHANGED
@@ -100,5 +100,13 @@ declare const maxBigInt: (...values: bigint[]) => bigint;
100
100
  * minBigInt(10n, 50n, 30n, 100n, 20n); // 10n
101
101
  */
102
102
  declare const minBigInt: (...values: bigint[]) => bigint;
103
+ /**
104
+ * Converts a string to a Uint8Array.
105
+ *
106
+ * @function stringToU8a
107
+ * @param {string} input - The string to convert.
108
+ * @returns {Uint8Array} The resulting Uint8Array.
109
+ */
110
+ declare const stringToU8a: (input: string) => Uint8Array;
103
111
 
104
- export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, maxBigInt, minBigInt, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, shuffle, withTimeout };
112
+ export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, maxBigInt, minBigInt, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, shuffle, stringToU8a, withTimeout };
package/base.d.ts CHANGED
@@ -100,5 +100,13 @@ declare const maxBigInt: (...values: bigint[]) => bigint;
100
100
  * minBigInt(10n, 50n, 30n, 100n, 20n); // 10n
101
101
  */
102
102
  declare const minBigInt: (...values: bigint[]) => bigint;
103
+ /**
104
+ * Converts a string to a Uint8Array.
105
+ *
106
+ * @function stringToU8a
107
+ * @param {string} input - The string to convert.
108
+ * @returns {Uint8Array} The resulting Uint8Array.
109
+ */
110
+ declare const stringToU8a: (input: string) => Uint8Array;
103
111
 
104
- export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, maxBigInt, minBigInt, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, shuffle, withTimeout };
112
+ export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, maxBigInt, minBigInt, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, shuffle, stringToU8a, withTimeout };
package/base.js CHANGED
@@ -119,6 +119,13 @@ var isSuperset = (set, subset) => {
119
119
  };
120
120
  var maxBigInt = (...values) => values.reduce((max, current) => current > max ? current : max);
121
121
  var minBigInt = (...values) => values.reduce((min, current) => current < min ? current : min);
122
+ var stringToU8a = (input) => {
123
+ const u8a = new Uint8Array(input.length);
124
+ for (let i = 0; i < input.length; i++) {
125
+ u8a[i] = input.charCodeAt(i);
126
+ }
127
+ return u8a;
128
+ };
122
129
  export {
123
130
  appendOr,
124
131
  appendOrEmpty,
@@ -134,6 +141,7 @@ export {
134
141
  removeHexPrefix,
135
142
  rmCommas,
136
143
  shuffle,
144
+ stringToU8a,
137
145
  withTimeout
138
146
  };
139
147
  /* @license Copyright 2024 w3ux authors & contributors
package/index.cjs CHANGED
@@ -62,6 +62,7 @@ __export(src_exports, {
62
62
  shuffle: () => shuffle,
63
63
  snakeToCamel: () => snakeToCamel,
64
64
  sortWithNull: () => sortWithNull,
65
+ stringToU8a: () => stringToU8a,
65
66
  unescape: () => unescape,
66
67
  unimplemented: () => unimplemented,
67
68
  unitToPlanck: () => unitToPlanck,
@@ -191,6 +192,13 @@ var isSuperset = (set, subset) => {
191
192
  };
192
193
  var maxBigInt = (...values) => values.reduce((max, current) => current > max ? current : max);
193
194
  var minBigInt = (...values) => values.reduce((min, current) => current < min ? current : min);
195
+ var stringToU8a = (input) => {
196
+ const u8a = new Uint8Array(input.length);
197
+ for (let i = 0; i < input.length; i++) {
198
+ u8a[i] = input.charCodeAt(i);
199
+ }
200
+ return u8a;
201
+ };
194
202
 
195
203
  // ../../node_modules/@polkadot/x-textdecoder/node.js
196
204
  var import_node_util = __toESM(require("util"), 1);
@@ -286,13 +294,13 @@ function isU8a(value) {
286
294
 
287
295
  // ../../node_modules/@polkadot/util/string/toU8a.js
288
296
  var encoder = new TextEncoder();
289
- function stringToU8a(value) {
297
+ function stringToU8a2(value) {
290
298
  return value ? encoder.encode(value.toString()) : new Uint8Array();
291
299
  }
292
300
 
293
301
  // ../../node_modules/@polkadot/util/u8a/toU8a.js
294
302
  function u8aToU8a(value) {
295
- return isU8a(value) ? isBuffer(value) ? new Uint8Array(value) : value : isHex(value) ? hexToU8a(value) : Array.isArray(value) ? new Uint8Array(value) : stringToU8a(value);
303
+ return isU8a(value) ? isBuffer(value) ? new Uint8Array(value) : value : isHex(value) ? hexToU8a(value) : Array.isArray(value) ? new Uint8Array(value) : stringToU8a2(value);
296
304
  }
297
305
 
298
306
  // ../../node_modules/@polkadot/util/u8a/eq.js
@@ -342,8 +350,10 @@ function u8aUnwrapBytes(bytes) {
342
350
  var import_substrate_bindings2 = require("@polkadot-api/substrate-bindings");
343
351
  var planckToUnit = (val, units) => {
344
352
  try {
345
- units = Math.max(units, 0);
346
- const bigIntVal = typeof val === "bigint" ? val : BigInt(typeof val === "number" ? Math.floor(val).toString() : val);
353
+ units = Math.max(Math.round(units), 0);
354
+ const bigIntVal = typeof val === "bigint" ? val : BigInt(
355
+ typeof val === "number" ? Math.floor(val).toString() : rmCommas(val)
356
+ );
347
357
  const divisor = units === 0 ? 1n : BigInt(10) ** BigInt(units);
348
358
  const integerPart = bigIntVal / divisor;
349
359
  const fractionalPart = bigIntVal % divisor;
@@ -355,12 +365,17 @@ var planckToUnit = (val, units) => {
355
365
  };
356
366
  var unitToPlanck = (val, units) => {
357
367
  try {
358
- const strVal = (typeof val === "string" ? val : val.toString()) || "0";
368
+ units = Math.max(Math.round(units), 0);
369
+ const strVal = (typeof val === "string" ? rmCommas(val) : val.toString()) || "0";
359
370
  const [integerPart, fractionalPart = ""] = strVal.split(".");
360
371
  let bigIntValue = BigInt(integerPart) * BigInt(10) ** BigInt(units);
361
372
  if (fractionalPart) {
362
- const paddedFractional = fractionalPart.padEnd(units, "0");
363
- const fractionalValue = BigInt(paddedFractional);
373
+ let fractionalValue;
374
+ if (fractionalPart.length > units) {
375
+ fractionalValue = BigInt(fractionalPart.slice(0, units));
376
+ } else {
377
+ fractionalValue = BigInt(fractionalPart.padEnd(units, "0"));
378
+ }
364
379
  bigIntValue += fractionalValue;
365
380
  }
366
381
  return bigIntValue;
@@ -576,6 +591,7 @@ var mergeDeep = (target, ...sources) => {
576
591
  shuffle,
577
592
  snakeToCamel,
578
593
  sortWithNull,
594
+ stringToU8a,
579
595
  unescape,
580
596
  unimplemented,
581
597
  unitToPlanck,
package/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, maxBigInt, minBigInt, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, shuffle, withTimeout } from './base.cjs';
1
+ export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, maxBigInt, minBigInt, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, shuffle, stringToU8a, withTimeout } from './base.cjs';
2
2
  export { addedTo, applyWidthAsPadding, capitalizeFirstLetter, determinePoolDisplay, extractUrlValue, inChrome, isValidAddress, isValidHttpUrl, localStorageOrDefault, makeCancelable, matchedProperties, mergeDeep, planckToUnit, remToUnit, removeVarFromUrlHash, removedFrom, setStateWithRef, snakeToCamel, sortWithNull, unescape, unimplemented, unitToPlanck, varToUrlHash } from './unit.cjs';
3
3
  import '@w3ux/types';
4
4
  import 'react';
package/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, maxBigInt, minBigInt, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, shuffle, withTimeout } from './base.js';
1
+ export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, maxBigInt, minBigInt, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, shuffle, stringToU8a, withTimeout } from './base.js';
2
2
  export { addedTo, applyWidthAsPadding, capitalizeFirstLetter, determinePoolDisplay, extractUrlValue, inChrome, isValidAddress, isValidHttpUrl, localStorageOrDefault, makeCancelable, matchedProperties, mergeDeep, planckToUnit, remToUnit, removeVarFromUrlHash, removedFrom, setStateWithRef, snakeToCamel, sortWithNull, unescape, unimplemented, unitToPlanck, varToUrlHash } from './unit.js';
3
3
  import '@w3ux/types';
4
4
  import 'react';
package/index.js CHANGED
@@ -119,6 +119,13 @@ var isSuperset = (set, subset) => {
119
119
  };
120
120
  var maxBigInt = (...values) => values.reduce((max, current) => current > max ? current : max);
121
121
  var minBigInt = (...values) => values.reduce((min, current) => current < min ? current : min);
122
+ var stringToU8a = (input) => {
123
+ const u8a = new Uint8Array(input.length);
124
+ for (let i = 0; i < input.length; i++) {
125
+ u8a[i] = input.charCodeAt(i);
126
+ }
127
+ return u8a;
128
+ };
122
129
 
123
130
  // ../../node_modules/@polkadot/x-textdecoder/node.js
124
131
  import util from "node:util";
@@ -214,13 +221,13 @@ function isU8a(value) {
214
221
 
215
222
  // ../../node_modules/@polkadot/util/string/toU8a.js
216
223
  var encoder = new TextEncoder();
217
- function stringToU8a(value) {
224
+ function stringToU8a2(value) {
218
225
  return value ? encoder.encode(value.toString()) : new Uint8Array();
219
226
  }
220
227
 
221
228
  // ../../node_modules/@polkadot/util/u8a/toU8a.js
222
229
  function u8aToU8a(value) {
223
- return isU8a(value) ? isBuffer(value) ? new Uint8Array(value) : value : isHex(value) ? hexToU8a(value) : Array.isArray(value) ? new Uint8Array(value) : stringToU8a(value);
230
+ return isU8a(value) ? isBuffer(value) ? new Uint8Array(value) : value : isHex(value) ? hexToU8a(value) : Array.isArray(value) ? new Uint8Array(value) : stringToU8a2(value);
224
231
  }
225
232
 
226
233
  // ../../node_modules/@polkadot/util/u8a/eq.js
@@ -270,8 +277,10 @@ function u8aUnwrapBytes(bytes) {
270
277
  import { AccountId as AccountId2 } from "@polkadot-api/substrate-bindings";
271
278
  var planckToUnit = (val, units) => {
272
279
  try {
273
- units = Math.max(units, 0);
274
- const bigIntVal = typeof val === "bigint" ? val : BigInt(typeof val === "number" ? Math.floor(val).toString() : val);
280
+ units = Math.max(Math.round(units), 0);
281
+ const bigIntVal = typeof val === "bigint" ? val : BigInt(
282
+ typeof val === "number" ? Math.floor(val).toString() : rmCommas(val)
283
+ );
275
284
  const divisor = units === 0 ? 1n : BigInt(10) ** BigInt(units);
276
285
  const integerPart = bigIntVal / divisor;
277
286
  const fractionalPart = bigIntVal % divisor;
@@ -283,12 +292,17 @@ var planckToUnit = (val, units) => {
283
292
  };
284
293
  var unitToPlanck = (val, units) => {
285
294
  try {
286
- const strVal = (typeof val === "string" ? val : val.toString()) || "0";
295
+ units = Math.max(Math.round(units), 0);
296
+ const strVal = (typeof val === "string" ? rmCommas(val) : val.toString()) || "0";
287
297
  const [integerPart, fractionalPart = ""] = strVal.split(".");
288
298
  let bigIntValue = BigInt(integerPart) * BigInt(10) ** BigInt(units);
289
299
  if (fractionalPart) {
290
- const paddedFractional = fractionalPart.padEnd(units, "0");
291
- const fractionalValue = BigInt(paddedFractional);
300
+ let fractionalValue;
301
+ if (fractionalPart.length > units) {
302
+ fractionalValue = BigInt(fractionalPart.slice(0, units));
303
+ } else {
304
+ fractionalValue = BigInt(fractionalPart.padEnd(units, "0"));
305
+ }
292
306
  bigIntValue += fractionalValue;
293
307
  }
294
308
  return bigIntValue;
@@ -503,6 +517,7 @@ export {
503
517
  shuffle,
504
518
  snakeToCamel,
505
519
  sortWithNull,
520
+ stringToU8a,
506
521
  unescape,
507
522
  unimplemented,
508
523
  unitToPlanck,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@w3ux/utils",
3
- "version": "1.0.1",
3
+ "version": "1.1.1-beta.0",
4
4
  "license": "GPL-3.0-only",
5
5
  "dependencies": {
6
6
  "@polkadot-api/substrate-bindings": "^0.9.3"
@@ -11,14 +11,6 @@
11
11
  ".": {
12
12
  "import": "./index.js",
13
13
  "require": "./index.cjs"
14
- },
15
- "./util": {
16
- "import": "./util/index.js",
17
- "require": "./util/index.cjs"
18
- },
19
- "./util-crypto": {
20
- "import": "./util-crypto/index.js",
21
- "require": "./util-crypto/index.cjs"
22
14
  }
23
15
  }
24
16
  }
package/unit.cjs CHANGED
@@ -229,13 +229,16 @@ var ellipsisFn = (str, amount = 6, position = "center") => {
229
229
  return "..." + str.slice(amount);
230
230
  }
231
231
  };
232
+ var rmCommas = (val) => val.replace(/,/g, "");
232
233
 
233
234
  // src/unit.ts
234
235
  var import_substrate_bindings2 = require("@polkadot-api/substrate-bindings");
235
236
  var planckToUnit = (val, units) => {
236
237
  try {
237
- units = Math.max(units, 0);
238
- const bigIntVal = typeof val === "bigint" ? val : BigInt(typeof val === "number" ? Math.floor(val).toString() : val);
238
+ units = Math.max(Math.round(units), 0);
239
+ const bigIntVal = typeof val === "bigint" ? val : BigInt(
240
+ typeof val === "number" ? Math.floor(val).toString() : rmCommas(val)
241
+ );
239
242
  const divisor = units === 0 ? 1n : BigInt(10) ** BigInt(units);
240
243
  const integerPart = bigIntVal / divisor;
241
244
  const fractionalPart = bigIntVal % divisor;
@@ -247,12 +250,17 @@ var planckToUnit = (val, units) => {
247
250
  };
248
251
  var unitToPlanck = (val, units) => {
249
252
  try {
250
- const strVal = (typeof val === "string" ? val : val.toString()) || "0";
253
+ units = Math.max(Math.round(units), 0);
254
+ const strVal = (typeof val === "string" ? rmCommas(val) : val.toString()) || "0";
251
255
  const [integerPart, fractionalPart = ""] = strVal.split(".");
252
256
  let bigIntValue = BigInt(integerPart) * BigInt(10) ** BigInt(units);
253
257
  if (fractionalPart) {
254
- const paddedFractional = fractionalPart.padEnd(units, "0");
255
- const fractionalValue = BigInt(paddedFractional);
258
+ let fractionalValue;
259
+ if (fractionalPart.length > units) {
260
+ fractionalValue = BigInt(fractionalPart.slice(0, units));
261
+ } else {
262
+ fractionalValue = BigInt(fractionalPart.padEnd(units, "0"));
263
+ }
256
264
  bigIntValue += fractionalValue;
257
265
  }
258
266
  return bigIntValue;
package/unit.js CHANGED
@@ -172,13 +172,16 @@ var ellipsisFn = (str, amount = 6, position = "center") => {
172
172
  return "..." + str.slice(amount);
173
173
  }
174
174
  };
175
+ var rmCommas = (val) => val.replace(/,/g, "");
175
176
 
176
177
  // src/unit.ts
177
178
  import { AccountId as AccountId2 } from "@polkadot-api/substrate-bindings";
178
179
  var planckToUnit = (val, units) => {
179
180
  try {
180
- units = Math.max(units, 0);
181
- const bigIntVal = typeof val === "bigint" ? val : BigInt(typeof val === "number" ? Math.floor(val).toString() : val);
181
+ units = Math.max(Math.round(units), 0);
182
+ const bigIntVal = typeof val === "bigint" ? val : BigInt(
183
+ typeof val === "number" ? Math.floor(val).toString() : rmCommas(val)
184
+ );
182
185
  const divisor = units === 0 ? 1n : BigInt(10) ** BigInt(units);
183
186
  const integerPart = bigIntVal / divisor;
184
187
  const fractionalPart = bigIntVal % divisor;
@@ -190,12 +193,17 @@ var planckToUnit = (val, units) => {
190
193
  };
191
194
  var unitToPlanck = (val, units) => {
192
195
  try {
193
- const strVal = (typeof val === "string" ? val : val.toString()) || "0";
196
+ units = Math.max(Math.round(units), 0);
197
+ const strVal = (typeof val === "string" ? rmCommas(val) : val.toString()) || "0";
194
198
  const [integerPart, fractionalPart = ""] = strVal.split(".");
195
199
  let bigIntValue = BigInt(integerPart) * BigInt(10) ** BigInt(units);
196
200
  if (fractionalPart) {
197
- const paddedFractional = fractionalPart.padEnd(units, "0");
198
- const fractionalValue = BigInt(paddedFractional);
201
+ let fractionalValue;
202
+ if (fractionalPart.length > units) {
203
+ fractionalValue = BigInt(fractionalPart.slice(0, units));
204
+ } else {
205
+ fractionalValue = BigInt(fractionalPart.padEnd(units, "0"));
206
+ }
199
207
  bigIntValue += fractionalValue;
200
208
  }
201
209
  return bigIntValue;