@w3ux/utils 1.0.0 → 1.1.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/index.cjs CHANGED
@@ -342,8 +342,10 @@ function u8aUnwrapBytes(bytes) {
342
342
  var import_substrate_bindings2 = require("@polkadot-api/substrate-bindings");
343
343
  var planckToUnit = (val, units) => {
344
344
  try {
345
- units = Math.max(units, 0);
346
- const bigIntVal = typeof val === "bigint" ? val : BigInt(typeof val === "number" ? Math.floor(val).toString() : val);
345
+ units = Math.max(Math.round(units), 0);
346
+ const bigIntVal = typeof val === "bigint" ? val : BigInt(
347
+ typeof val === "number" ? Math.floor(val).toString() : rmCommas(val)
348
+ );
347
349
  const divisor = units === 0 ? 1n : BigInt(10) ** BigInt(units);
348
350
  const integerPart = bigIntVal / divisor;
349
351
  const fractionalPart = bigIntVal % divisor;
@@ -355,13 +357,18 @@ var planckToUnit = (val, units) => {
355
357
  };
356
358
  var unitToPlanck = (val, units) => {
357
359
  try {
358
- const strVal = (typeof val === "string" ? val : val.toString()) || "0";
360
+ units = Math.max(Math.round(units), 0);
361
+ const strVal = (typeof val === "string" ? rmCommas(val) : val.toString()) || "0";
359
362
  const [integerPart, fractionalPart = ""] = strVal.split(".");
360
363
  let bigIntValue = BigInt(integerPart) * BigInt(10) ** BigInt(units);
361
364
  if (fractionalPart) {
362
- const fractionalScale = BigInt(10) ** BigInt(units - fractionalPart.length);
363
- const fractionalValue = BigInt(fractionalPart.padEnd(units, "0"));
364
- bigIntValue += fractionalValue / fractionalScale;
365
+ let fractionalValue;
366
+ if (fractionalPart.length > units) {
367
+ fractionalValue = BigInt(fractionalPart.slice(0, units));
368
+ } else {
369
+ fractionalValue = BigInt(fractionalPart.padEnd(units, "0"));
370
+ }
371
+ bigIntValue += fractionalValue;
365
372
  }
366
373
  return bigIntValue;
367
374
  } catch (e) {
package/index.js CHANGED
@@ -270,8 +270,10 @@ function u8aUnwrapBytes(bytes) {
270
270
  import { AccountId as AccountId2 } from "@polkadot-api/substrate-bindings";
271
271
  var planckToUnit = (val, units) => {
272
272
  try {
273
- units = Math.max(units, 0);
274
- const bigIntVal = typeof val === "bigint" ? val : BigInt(typeof val === "number" ? Math.floor(val).toString() : val);
273
+ units = Math.max(Math.round(units), 0);
274
+ const bigIntVal = typeof val === "bigint" ? val : BigInt(
275
+ typeof val === "number" ? Math.floor(val).toString() : rmCommas(val)
276
+ );
275
277
  const divisor = units === 0 ? 1n : BigInt(10) ** BigInt(units);
276
278
  const integerPart = bigIntVal / divisor;
277
279
  const fractionalPart = bigIntVal % divisor;
@@ -283,13 +285,18 @@ var planckToUnit = (val, units) => {
283
285
  };
284
286
  var unitToPlanck = (val, units) => {
285
287
  try {
286
- const strVal = (typeof val === "string" ? val : val.toString()) || "0";
288
+ units = Math.max(Math.round(units), 0);
289
+ const strVal = (typeof val === "string" ? rmCommas(val) : val.toString()) || "0";
287
290
  const [integerPart, fractionalPart = ""] = strVal.split(".");
288
291
  let bigIntValue = BigInt(integerPart) * BigInt(10) ** BigInt(units);
289
292
  if (fractionalPart) {
290
- const fractionalScale = BigInt(10) ** BigInt(units - fractionalPart.length);
291
- const fractionalValue = BigInt(fractionalPart.padEnd(units, "0"));
292
- bigIntValue += fractionalValue / fractionalScale;
293
+ let fractionalValue;
294
+ if (fractionalPart.length > units) {
295
+ fractionalValue = BigInt(fractionalPart.slice(0, units));
296
+ } else {
297
+ fractionalValue = BigInt(fractionalPart.padEnd(units, "0"));
298
+ }
299
+ bigIntValue += fractionalValue;
293
300
  }
294
301
  return bigIntValue;
295
302
  } catch (e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@w3ux/utils",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "license": "GPL-3.0-only",
5
5
  "dependencies": {
6
6
  "@polkadot-api/substrate-bindings": "^0.9.3"
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,13 +250,18 @@ 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 fractionalScale = BigInt(10) ** BigInt(units - fractionalPart.length);
255
- const fractionalValue = BigInt(fractionalPart.padEnd(units, "0"));
256
- bigIntValue += fractionalValue / fractionalScale;
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
+ }
264
+ bigIntValue += fractionalValue;
257
265
  }
258
266
  return bigIntValue;
259
267
  } catch (e) {
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,13 +193,18 @@ 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 fractionalScale = BigInt(10) ** BigInt(units - fractionalPart.length);
198
- const fractionalValue = BigInt(fractionalPart.padEnd(units, "0"));
199
- bigIntValue += fractionalValue / fractionalScale;
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
+ }
207
+ bigIntValue += fractionalValue;
200
208
  }
201
209
  return bigIntValue;
202
210
  } catch (e) {