@w3ux/utils 0.10.1-alpha.2 → 0.10.1-alpha.4
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 +24 -17
- package/index.d.cts +1 -2
- package/index.d.ts +1 -2
- package/index.js +24 -15
- package/package.json +2 -3
- package/unit.cjs +24 -20
- package/unit.d.cts +9 -23
- package/unit.d.ts +9 -23
- package/unit.js +24 -18
package/index.cjs
CHANGED
|
@@ -51,7 +51,6 @@ __export(src_exports, {
|
|
|
51
51
|
minDecimalPlaces: () => minDecimalPlaces,
|
|
52
52
|
pageFromUri: () => pageFromUri,
|
|
53
53
|
planckToUnit: () => planckToUnit,
|
|
54
|
-
planckToUnitLegacy: () => planckToUnitLegacy,
|
|
55
54
|
remToUnit: () => remToUnit,
|
|
56
55
|
removeHexPrefix: () => removeHexPrefix,
|
|
57
56
|
removeVarFromUrlHash: () => removeVarFromUrlHash,
|
|
@@ -61,7 +60,6 @@ __export(src_exports, {
|
|
|
61
60
|
shuffle: () => shuffle,
|
|
62
61
|
snakeToCamel: () => snakeToCamel,
|
|
63
62
|
sortWithNull: () => sortWithNull,
|
|
64
|
-
stringToBigNumber: () => stringToBigNumber,
|
|
65
63
|
transformToBaseUnit: () => transformToBaseUnit,
|
|
66
64
|
unescape: () => unescape,
|
|
67
65
|
unimplemented: () => unimplemented,
|
|
@@ -335,23 +333,35 @@ function u8aUnwrapBytes(bytes) {
|
|
|
335
333
|
}
|
|
336
334
|
|
|
337
335
|
// src/unit.ts
|
|
338
|
-
var import_bignumber = require("bignumber.js");
|
|
339
336
|
var import_substrate_bindings2 = require("@polkadot-api/substrate-bindings");
|
|
340
337
|
var remToUnit = (rem) => Number(rem.slice(0, rem.length - 3)) * parseFloat(getComputedStyle(document.documentElement).fontSize);
|
|
341
|
-
var planckToUnitLegacy = (val, units) => new import_bignumber.BigNumber(
|
|
342
|
-
val.dividedBy(new import_bignumber.BigNumber(10).exponentiatedBy(units)).toFixed(units)
|
|
343
|
-
);
|
|
344
338
|
var planckToUnit = (val, units) => {
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
339
|
+
try {
|
|
340
|
+
units = Math.max(units, 0);
|
|
341
|
+
const bigIntVal = typeof val === "bigint" ? val : BigInt(typeof val === "number" ? Math.floor(val).toString() : val);
|
|
342
|
+
const divisor = units === 0 ? 1n : BigInt(10) ** BigInt(units);
|
|
343
|
+
const integerPart = bigIntVal / divisor;
|
|
344
|
+
const fractionalPart = bigIntVal % divisor;
|
|
345
|
+
const fractionalStr = units > 0 ? `.${fractionalPart.toString().padStart(units, "0")}` : ``;
|
|
346
|
+
return `${integerPart}${fractionalStr}`;
|
|
347
|
+
} catch (e) {
|
|
348
|
+
return "0";
|
|
349
|
+
}
|
|
351
350
|
};
|
|
352
351
|
var unitToPlanck = (val, units) => {
|
|
353
|
-
|
|
354
|
-
|
|
352
|
+
try {
|
|
353
|
+
const strVal = (typeof val === "string" ? val : val.toString()) || "0";
|
|
354
|
+
const [integerPart, fractionalPart = ""] = strVal.split(".");
|
|
355
|
+
let bigIntValue = BigInt(integerPart) * BigInt(10) ** BigInt(units);
|
|
356
|
+
if (fractionalPart) {
|
|
357
|
+
const fractionalScale = BigInt(10) ** BigInt(units - fractionalPart.length);
|
|
358
|
+
const fractionalValue = BigInt(fractionalPart.padEnd(units, "0"));
|
|
359
|
+
bigIntValue += fractionalValue / fractionalScale;
|
|
360
|
+
}
|
|
361
|
+
return bigIntValue;
|
|
362
|
+
} catch (e) {
|
|
363
|
+
return BigInt(0);
|
|
364
|
+
}
|
|
355
365
|
};
|
|
356
366
|
var capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1);
|
|
357
367
|
var snakeToCamel = (str) => str.toLowerCase().replace(
|
|
@@ -545,7 +555,6 @@ var mergeDeep = (target, ...sources) => {
|
|
|
545
555
|
}
|
|
546
556
|
return mergeDeep(target, ...sources);
|
|
547
557
|
};
|
|
548
|
-
var stringToBigNumber = (value) => new import_bignumber.BigNumber(rmCommas(value));
|
|
549
558
|
// Annotate the CommonJS export names for ESM import in node:
|
|
550
559
|
0 && (module.exports = {
|
|
551
560
|
addedTo,
|
|
@@ -570,7 +579,6 @@ var stringToBigNumber = (value) => new import_bignumber.BigNumber(rmCommas(value
|
|
|
570
579
|
minDecimalPlaces,
|
|
571
580
|
pageFromUri,
|
|
572
581
|
planckToUnit,
|
|
573
|
-
planckToUnitLegacy,
|
|
574
582
|
remToUnit,
|
|
575
583
|
removeHexPrefix,
|
|
576
584
|
removeVarFromUrlHash,
|
|
@@ -580,7 +588,6 @@ var stringToBigNumber = (value) => new import_bignumber.BigNumber(rmCommas(value
|
|
|
580
588
|
shuffle,
|
|
581
589
|
snakeToCamel,
|
|
582
590
|
sortWithNull,
|
|
583
|
-
stringToBigNumber,
|
|
584
591
|
transformToBaseUnit,
|
|
585
592
|
unescape,
|
|
586
593
|
unimplemented,
|
package/index.d.cts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, shuffle, withTimeout } from './base.cjs';
|
|
2
|
-
export { addedTo, applyWidthAsPadding, capitalizeFirstLetter, determinePoolDisplay, extractUrlValue, inChrome, isValidAddress, isValidHttpUrl, localStorageOrDefault, makeCancelable, matchedProperties, mergeDeep, planckToUnit,
|
|
2
|
+
export { addedTo, applyWidthAsPadding, capitalizeFirstLetter, determinePoolDisplay, extractUrlValue, inChrome, isValidAddress, isValidHttpUrl, localStorageOrDefault, makeCancelable, matchedProperties, mergeDeep, planckToUnit, remToUnit, removeVarFromUrlHash, removedFrom, setStateWithRef, snakeToCamel, sortWithNull, transformToBaseUnit, unescape, unimplemented, unitToPlanck, varToUrlHash } from './unit.cjs';
|
|
3
3
|
import '@w3ux/types';
|
|
4
|
-
import 'bignumber.js';
|
|
5
4
|
import 'react';
|
|
6
5
|
import './types.cjs';
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, shuffle, withTimeout } from './base.js';
|
|
2
|
-
export { addedTo, applyWidthAsPadding, capitalizeFirstLetter, determinePoolDisplay, extractUrlValue, inChrome, isValidAddress, isValidHttpUrl, localStorageOrDefault, makeCancelable, matchedProperties, mergeDeep, planckToUnit,
|
|
2
|
+
export { addedTo, applyWidthAsPadding, capitalizeFirstLetter, determinePoolDisplay, extractUrlValue, inChrome, isValidAddress, isValidHttpUrl, localStorageOrDefault, makeCancelable, matchedProperties, mergeDeep, planckToUnit, remToUnit, removeVarFromUrlHash, removedFrom, setStateWithRef, snakeToCamel, sortWithNull, transformToBaseUnit, unescape, unimplemented, unitToPlanck, varToUrlHash } from './unit.js';
|
|
3
3
|
import '@w3ux/types';
|
|
4
|
-
import 'bignumber.js';
|
|
5
4
|
import 'react';
|
|
6
5
|
import './types.js';
|
package/index.js
CHANGED
|
@@ -262,23 +262,35 @@ function u8aUnwrapBytes(bytes) {
|
|
|
262
262
|
}
|
|
263
263
|
|
|
264
264
|
// src/unit.ts
|
|
265
|
-
import { BigNumber } from "bignumber.js";
|
|
266
265
|
import { AccountId as AccountId2 } from "@polkadot-api/substrate-bindings";
|
|
267
266
|
var remToUnit = (rem) => Number(rem.slice(0, rem.length - 3)) * parseFloat(getComputedStyle(document.documentElement).fontSize);
|
|
268
|
-
var planckToUnitLegacy = (val, units) => new BigNumber(
|
|
269
|
-
val.dividedBy(new BigNumber(10).exponentiatedBy(units)).toFixed(units)
|
|
270
|
-
);
|
|
271
267
|
var planckToUnit = (val, units) => {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
268
|
+
try {
|
|
269
|
+
units = Math.max(units, 0);
|
|
270
|
+
const bigIntVal = typeof val === "bigint" ? val : BigInt(typeof val === "number" ? Math.floor(val).toString() : val);
|
|
271
|
+
const divisor = units === 0 ? 1n : BigInt(10) ** BigInt(units);
|
|
272
|
+
const integerPart = bigIntVal / divisor;
|
|
273
|
+
const fractionalPart = bigIntVal % divisor;
|
|
274
|
+
const fractionalStr = units > 0 ? `.${fractionalPart.toString().padStart(units, "0")}` : ``;
|
|
275
|
+
return `${integerPart}${fractionalStr}`;
|
|
276
|
+
} catch (e) {
|
|
277
|
+
return "0";
|
|
278
|
+
}
|
|
278
279
|
};
|
|
279
280
|
var unitToPlanck = (val, units) => {
|
|
280
|
-
|
|
281
|
-
|
|
281
|
+
try {
|
|
282
|
+
const strVal = (typeof val === "string" ? val : val.toString()) || "0";
|
|
283
|
+
const [integerPart, fractionalPart = ""] = strVal.split(".");
|
|
284
|
+
let bigIntValue = BigInt(integerPart) * BigInt(10) ** BigInt(units);
|
|
285
|
+
if (fractionalPart) {
|
|
286
|
+
const fractionalScale = BigInt(10) ** BigInt(units - fractionalPart.length);
|
|
287
|
+
const fractionalValue = BigInt(fractionalPart.padEnd(units, "0"));
|
|
288
|
+
bigIntValue += fractionalValue / fractionalScale;
|
|
289
|
+
}
|
|
290
|
+
return bigIntValue;
|
|
291
|
+
} catch (e) {
|
|
292
|
+
return BigInt(0);
|
|
293
|
+
}
|
|
282
294
|
};
|
|
283
295
|
var capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1);
|
|
284
296
|
var snakeToCamel = (str) => str.toLowerCase().replace(
|
|
@@ -472,7 +484,6 @@ var mergeDeep = (target, ...sources) => {
|
|
|
472
484
|
}
|
|
473
485
|
return mergeDeep(target, ...sources);
|
|
474
486
|
};
|
|
475
|
-
var stringToBigNumber = (value) => new BigNumber(rmCommas(value));
|
|
476
487
|
export {
|
|
477
488
|
addedTo,
|
|
478
489
|
appendOr,
|
|
@@ -496,7 +507,6 @@ export {
|
|
|
496
507
|
minDecimalPlaces,
|
|
497
508
|
pageFromUri,
|
|
498
509
|
planckToUnit,
|
|
499
|
-
planckToUnitLegacy,
|
|
500
510
|
remToUnit,
|
|
501
511
|
removeHexPrefix,
|
|
502
512
|
removeVarFromUrlHash,
|
|
@@ -506,7 +516,6 @@ export {
|
|
|
506
516
|
shuffle,
|
|
507
517
|
snakeToCamel,
|
|
508
518
|
sortWithNull,
|
|
509
|
-
stringToBigNumber,
|
|
510
519
|
transformToBaseUnit,
|
|
511
520
|
unescape,
|
|
512
521
|
unimplemented,
|
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@w3ux/utils",
|
|
3
|
-
"version": "0.10.1-alpha.
|
|
3
|
+
"version": "0.10.1-alpha.4",
|
|
4
4
|
"license": "GPL-3.0-only",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"@polkadot-api/substrate-bindings": "^0.9.3"
|
|
7
|
-
"bignumber.js": "^9.1.1"
|
|
6
|
+
"@polkadot-api/substrate-bindings": "^0.9.3"
|
|
8
7
|
},
|
|
9
8
|
"main": "index.cjs",
|
|
10
9
|
"module": "index.js",
|
package/unit.cjs
CHANGED
|
@@ -42,14 +42,12 @@ __export(unit_exports, {
|
|
|
42
42
|
matchedProperties: () => matchedProperties,
|
|
43
43
|
mergeDeep: () => mergeDeep,
|
|
44
44
|
planckToUnit: () => planckToUnit,
|
|
45
|
-
planckToUnitLegacy: () => planckToUnitLegacy,
|
|
46
45
|
remToUnit: () => remToUnit,
|
|
47
46
|
removeVarFromUrlHash: () => removeVarFromUrlHash,
|
|
48
47
|
removedFrom: () => removedFrom,
|
|
49
48
|
setStateWithRef: () => setStateWithRef,
|
|
50
49
|
snakeToCamel: () => snakeToCamel,
|
|
51
50
|
sortWithNull: () => sortWithNull,
|
|
52
|
-
stringToBigNumber: () => stringToBigNumber,
|
|
53
51
|
transformToBaseUnit: () => transformToBaseUnit,
|
|
54
52
|
unescape: () => unescape,
|
|
55
53
|
unimplemented: () => unimplemented,
|
|
@@ -204,9 +202,6 @@ function u8aUnwrapBytes(bytes) {
|
|
|
204
202
|
return u8aIsWrapped(u8a, false) ? u8a.subarray(U8A_WRAP_PREFIX.length, u8a.length - U8A_WRAP_POSTFIX.length) : u8a;
|
|
205
203
|
}
|
|
206
204
|
|
|
207
|
-
// src/unit.ts
|
|
208
|
-
var import_bignumber = require("bignumber.js");
|
|
209
|
-
|
|
210
205
|
// src/base.ts
|
|
211
206
|
var import_substrate_bindings = require("@polkadot-api/substrate-bindings");
|
|
212
207
|
var ellipsisFn = (str, amount = 6, position = "center") => {
|
|
@@ -235,25 +230,37 @@ var ellipsisFn = (str, amount = 6, position = "center") => {
|
|
|
235
230
|
return "..." + str.slice(amount);
|
|
236
231
|
}
|
|
237
232
|
};
|
|
238
|
-
var rmCommas = (val) => val.replace(/,/g, "");
|
|
239
233
|
|
|
240
234
|
// src/unit.ts
|
|
241
235
|
var import_substrate_bindings2 = require("@polkadot-api/substrate-bindings");
|
|
242
236
|
var remToUnit = (rem) => Number(rem.slice(0, rem.length - 3)) * parseFloat(getComputedStyle(document.documentElement).fontSize);
|
|
243
|
-
var planckToUnitLegacy = (val, units) => new import_bignumber.BigNumber(
|
|
244
|
-
val.dividedBy(new import_bignumber.BigNumber(10).exponentiatedBy(units)).toFixed(units)
|
|
245
|
-
);
|
|
246
237
|
var planckToUnit = (val, units) => {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
238
|
+
try {
|
|
239
|
+
units = Math.max(units, 0);
|
|
240
|
+
const bigIntVal = typeof val === "bigint" ? val : BigInt(typeof val === "number" ? Math.floor(val).toString() : val);
|
|
241
|
+
const divisor = units === 0 ? 1n : BigInt(10) ** BigInt(units);
|
|
242
|
+
const integerPart = bigIntVal / divisor;
|
|
243
|
+
const fractionalPart = bigIntVal % divisor;
|
|
244
|
+
const fractionalStr = units > 0 ? `.${fractionalPart.toString().padStart(units, "0")}` : ``;
|
|
245
|
+
return `${integerPart}${fractionalStr}`;
|
|
246
|
+
} catch (e) {
|
|
247
|
+
return "0";
|
|
248
|
+
}
|
|
253
249
|
};
|
|
254
250
|
var unitToPlanck = (val, units) => {
|
|
255
|
-
|
|
256
|
-
|
|
251
|
+
try {
|
|
252
|
+
const strVal = (typeof val === "string" ? val : val.toString()) || "0";
|
|
253
|
+
const [integerPart, fractionalPart = ""] = strVal.split(".");
|
|
254
|
+
let bigIntValue = BigInt(integerPart) * BigInt(10) ** BigInt(units);
|
|
255
|
+
if (fractionalPart) {
|
|
256
|
+
const fractionalScale = BigInt(10) ** BigInt(units - fractionalPart.length);
|
|
257
|
+
const fractionalValue = BigInt(fractionalPart.padEnd(units, "0"));
|
|
258
|
+
bigIntValue += fractionalValue / fractionalScale;
|
|
259
|
+
}
|
|
260
|
+
return bigIntValue;
|
|
261
|
+
} catch (e) {
|
|
262
|
+
return BigInt(0);
|
|
263
|
+
}
|
|
257
264
|
};
|
|
258
265
|
var capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1);
|
|
259
266
|
var snakeToCamel = (str) => str.toLowerCase().replace(
|
|
@@ -447,7 +454,6 @@ var mergeDeep = (target, ...sources) => {
|
|
|
447
454
|
}
|
|
448
455
|
return mergeDeep(target, ...sources);
|
|
449
456
|
};
|
|
450
|
-
var stringToBigNumber = (value) => new import_bignumber.BigNumber(rmCommas(value));
|
|
451
457
|
// Annotate the CommonJS export names for ESM import in node:
|
|
452
458
|
0 && (module.exports = {
|
|
453
459
|
addedTo,
|
|
@@ -463,14 +469,12 @@ var stringToBigNumber = (value) => new import_bignumber.BigNumber(rmCommas(value
|
|
|
463
469
|
matchedProperties,
|
|
464
470
|
mergeDeep,
|
|
465
471
|
planckToUnit,
|
|
466
|
-
planckToUnitLegacy,
|
|
467
472
|
remToUnit,
|
|
468
473
|
removeVarFromUrlHash,
|
|
469
474
|
removedFrom,
|
|
470
475
|
setStateWithRef,
|
|
471
476
|
snakeToCamel,
|
|
472
477
|
sortWithNull,
|
|
473
|
-
stringToBigNumber,
|
|
474
478
|
transformToBaseUnit,
|
|
475
479
|
unescape,
|
|
476
480
|
unimplemented,
|
package/unit.d.cts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { BigNumber } from 'bignumber.js';
|
|
2
1
|
import { MutableRefObject, RefObject } from 'react';
|
|
3
2
|
import { AnyObject } from './types.cjs';
|
|
4
3
|
import { AnyJson } from '@w3ux/types';
|
|
@@ -8,30 +7,22 @@ import { AnyJson } from '@w3ux/types';
|
|
|
8
7
|
* @summary Converts a rem string to a number.
|
|
9
8
|
*/
|
|
10
9
|
declare const remToUnit: (rem: string) => number;
|
|
11
|
-
/**
|
|
12
|
-
* @name planckToUnitLegacy
|
|
13
|
-
* @summary convert planck to the token unit.
|
|
14
|
-
* @description
|
|
15
|
-
* Converts an on chain balance value in BigNumber planck to a decimal value in token unit. (1 token
|
|
16
|
-
* = 10^units planck).
|
|
17
|
-
*/
|
|
18
|
-
declare const planckToUnitLegacy: (val: BigNumber, units: number) => BigNumber;
|
|
19
10
|
/**
|
|
20
11
|
* @name planckToUnit
|
|
21
|
-
* @summary
|
|
12
|
+
* @summary Convert planck to the token unit.
|
|
22
13
|
* @description
|
|
23
|
-
* Converts an on-chain balance value from BigInt
|
|
24
|
-
* (1 token = 10^units planck).
|
|
14
|
+
* Converts an on-chain balance value from `number`, `BigInt`, or `string` (representing planck)
|
|
15
|
+
* to a decimal value in token units. (1 token = 10^units planck).
|
|
25
16
|
*/
|
|
26
|
-
declare const planckToUnit: (val: bigint | string, units: number) => string;
|
|
17
|
+
declare const planckToUnit: (val: number | bigint | string, units: number) => string;
|
|
27
18
|
/**
|
|
28
19
|
* @name unitToPlanck
|
|
29
|
-
* @summary Convert
|
|
20
|
+
* @summary Convert token unit to planck.
|
|
30
21
|
* @description
|
|
31
|
-
* Converts a
|
|
32
|
-
*
|
|
22
|
+
* Converts a token unit value (as `string`, `number`, or `BigInt`) to an integer value in planck.
|
|
23
|
+
* (1 token = 10^units planck).
|
|
33
24
|
*/
|
|
34
|
-
declare const unitToPlanck: (val: string, units: number) =>
|
|
25
|
+
declare const unitToPlanck: (val: string | number | bigint, units: number) => bigint;
|
|
35
26
|
/**
|
|
36
27
|
* @name capitalizeFirstLetter
|
|
37
28
|
* @summary Capitalize the first letter of a string.
|
|
@@ -161,10 +152,5 @@ declare const unimplemented: ({ ...props }: {
|
|
|
161
152
|
* @param ...sources
|
|
162
153
|
*/
|
|
163
154
|
declare const mergeDeep: (target: AnyObject, ...sources: AnyObject[]) => AnyObject;
|
|
164
|
-
/**
|
|
165
|
-
* @name stringToBigNumber
|
|
166
|
-
* @summary Converts a balance string into a `BigNumber`.
|
|
167
|
-
*/
|
|
168
|
-
declare const stringToBigNumber: (value: string) => BigNumber;
|
|
169
155
|
|
|
170
|
-
export { addedTo, applyWidthAsPadding, capitalizeFirstLetter, determinePoolDisplay, extractUrlValue, inChrome, isValidAddress, isValidHttpUrl, localStorageOrDefault, makeCancelable, matchedProperties, mergeDeep, planckToUnit,
|
|
156
|
+
export { addedTo, applyWidthAsPadding, capitalizeFirstLetter, determinePoolDisplay, extractUrlValue, inChrome, isValidAddress, isValidHttpUrl, localStorageOrDefault, makeCancelable, matchedProperties, mergeDeep, planckToUnit, remToUnit, removeVarFromUrlHash, removedFrom, setStateWithRef, snakeToCamel, sortWithNull, transformToBaseUnit, unescape, unimplemented, unitToPlanck, varToUrlHash };
|
package/unit.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { BigNumber } from 'bignumber.js';
|
|
2
1
|
import { MutableRefObject, RefObject } from 'react';
|
|
3
2
|
import { AnyObject } from './types.js';
|
|
4
3
|
import { AnyJson } from '@w3ux/types';
|
|
@@ -8,30 +7,22 @@ import { AnyJson } from '@w3ux/types';
|
|
|
8
7
|
* @summary Converts a rem string to a number.
|
|
9
8
|
*/
|
|
10
9
|
declare const remToUnit: (rem: string) => number;
|
|
11
|
-
/**
|
|
12
|
-
* @name planckToUnitLegacy
|
|
13
|
-
* @summary convert planck to the token unit.
|
|
14
|
-
* @description
|
|
15
|
-
* Converts an on chain balance value in BigNumber planck to a decimal value in token unit. (1 token
|
|
16
|
-
* = 10^units planck).
|
|
17
|
-
*/
|
|
18
|
-
declare const planckToUnitLegacy: (val: BigNumber, units: number) => BigNumber;
|
|
19
10
|
/**
|
|
20
11
|
* @name planckToUnit
|
|
21
|
-
* @summary
|
|
12
|
+
* @summary Convert planck to the token unit.
|
|
22
13
|
* @description
|
|
23
|
-
* Converts an on-chain balance value from BigInt
|
|
24
|
-
* (1 token = 10^units planck).
|
|
14
|
+
* Converts an on-chain balance value from `number`, `BigInt`, or `string` (representing planck)
|
|
15
|
+
* to a decimal value in token units. (1 token = 10^units planck).
|
|
25
16
|
*/
|
|
26
|
-
declare const planckToUnit: (val: bigint | string, units: number) => string;
|
|
17
|
+
declare const planckToUnit: (val: number | bigint | string, units: number) => string;
|
|
27
18
|
/**
|
|
28
19
|
* @name unitToPlanck
|
|
29
|
-
* @summary Convert
|
|
20
|
+
* @summary Convert token unit to planck.
|
|
30
21
|
* @description
|
|
31
|
-
* Converts a
|
|
32
|
-
*
|
|
22
|
+
* Converts a token unit value (as `string`, `number`, or `BigInt`) to an integer value in planck.
|
|
23
|
+
* (1 token = 10^units planck).
|
|
33
24
|
*/
|
|
34
|
-
declare const unitToPlanck: (val: string, units: number) =>
|
|
25
|
+
declare const unitToPlanck: (val: string | number | bigint, units: number) => bigint;
|
|
35
26
|
/**
|
|
36
27
|
* @name capitalizeFirstLetter
|
|
37
28
|
* @summary Capitalize the first letter of a string.
|
|
@@ -161,10 +152,5 @@ declare const unimplemented: ({ ...props }: {
|
|
|
161
152
|
* @param ...sources
|
|
162
153
|
*/
|
|
163
154
|
declare const mergeDeep: (target: AnyObject, ...sources: AnyObject[]) => AnyObject;
|
|
164
|
-
/**
|
|
165
|
-
* @name stringToBigNumber
|
|
166
|
-
* @summary Converts a balance string into a `BigNumber`.
|
|
167
|
-
*/
|
|
168
|
-
declare const stringToBigNumber: (value: string) => BigNumber;
|
|
169
155
|
|
|
170
|
-
export { addedTo, applyWidthAsPadding, capitalizeFirstLetter, determinePoolDisplay, extractUrlValue, inChrome, isValidAddress, isValidHttpUrl, localStorageOrDefault, makeCancelable, matchedProperties, mergeDeep, planckToUnit,
|
|
156
|
+
export { addedTo, applyWidthAsPadding, capitalizeFirstLetter, determinePoolDisplay, extractUrlValue, inChrome, isValidAddress, isValidHttpUrl, localStorageOrDefault, makeCancelable, matchedProperties, mergeDeep, planckToUnit, remToUnit, removeVarFromUrlHash, removedFrom, setStateWithRef, snakeToCamel, sortWithNull, transformToBaseUnit, unescape, unimplemented, unitToPlanck, varToUrlHash };
|
package/unit.js
CHANGED
|
@@ -144,9 +144,6 @@ function u8aUnwrapBytes(bytes) {
|
|
|
144
144
|
return u8aIsWrapped(u8a, false) ? u8a.subarray(U8A_WRAP_PREFIX.length, u8a.length - U8A_WRAP_POSTFIX.length) : u8a;
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
-
// src/unit.ts
|
|
148
|
-
import { BigNumber } from "bignumber.js";
|
|
149
|
-
|
|
150
147
|
// src/base.ts
|
|
151
148
|
import { AccountId } from "@polkadot-api/substrate-bindings";
|
|
152
149
|
var ellipsisFn = (str, amount = 6, position = "center") => {
|
|
@@ -175,25 +172,37 @@ var ellipsisFn = (str, amount = 6, position = "center") => {
|
|
|
175
172
|
return "..." + str.slice(amount);
|
|
176
173
|
}
|
|
177
174
|
};
|
|
178
|
-
var rmCommas = (val) => val.replace(/,/g, "");
|
|
179
175
|
|
|
180
176
|
// src/unit.ts
|
|
181
177
|
import { AccountId as AccountId2 } from "@polkadot-api/substrate-bindings";
|
|
182
178
|
var remToUnit = (rem) => Number(rem.slice(0, rem.length - 3)) * parseFloat(getComputedStyle(document.documentElement).fontSize);
|
|
183
|
-
var planckToUnitLegacy = (val, units) => new BigNumber(
|
|
184
|
-
val.dividedBy(new BigNumber(10).exponentiatedBy(units)).toFixed(units)
|
|
185
|
-
);
|
|
186
179
|
var planckToUnit = (val, units) => {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
180
|
+
try {
|
|
181
|
+
units = Math.max(units, 0);
|
|
182
|
+
const bigIntVal = typeof val === "bigint" ? val : BigInt(typeof val === "number" ? Math.floor(val).toString() : val);
|
|
183
|
+
const divisor = units === 0 ? 1n : BigInt(10) ** BigInt(units);
|
|
184
|
+
const integerPart = bigIntVal / divisor;
|
|
185
|
+
const fractionalPart = bigIntVal % divisor;
|
|
186
|
+
const fractionalStr = units > 0 ? `.${fractionalPart.toString().padStart(units, "0")}` : ``;
|
|
187
|
+
return `${integerPart}${fractionalStr}`;
|
|
188
|
+
} catch (e) {
|
|
189
|
+
return "0";
|
|
190
|
+
}
|
|
193
191
|
};
|
|
194
192
|
var unitToPlanck = (val, units) => {
|
|
195
|
-
|
|
196
|
-
|
|
193
|
+
try {
|
|
194
|
+
const strVal = (typeof val === "string" ? val : val.toString()) || "0";
|
|
195
|
+
const [integerPart, fractionalPart = ""] = strVal.split(".");
|
|
196
|
+
let bigIntValue = BigInt(integerPart) * BigInt(10) ** BigInt(units);
|
|
197
|
+
if (fractionalPart) {
|
|
198
|
+
const fractionalScale = BigInt(10) ** BigInt(units - fractionalPart.length);
|
|
199
|
+
const fractionalValue = BigInt(fractionalPart.padEnd(units, "0"));
|
|
200
|
+
bigIntValue += fractionalValue / fractionalScale;
|
|
201
|
+
}
|
|
202
|
+
return bigIntValue;
|
|
203
|
+
} catch (e) {
|
|
204
|
+
return BigInt(0);
|
|
205
|
+
}
|
|
197
206
|
};
|
|
198
207
|
var capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1);
|
|
199
208
|
var snakeToCamel = (str) => str.toLowerCase().replace(
|
|
@@ -387,7 +396,6 @@ var mergeDeep = (target, ...sources) => {
|
|
|
387
396
|
}
|
|
388
397
|
return mergeDeep(target, ...sources);
|
|
389
398
|
};
|
|
390
|
-
var stringToBigNumber = (value) => new BigNumber(rmCommas(value));
|
|
391
399
|
export {
|
|
392
400
|
addedTo,
|
|
393
401
|
applyWidthAsPadding,
|
|
@@ -402,14 +410,12 @@ export {
|
|
|
402
410
|
matchedProperties,
|
|
403
411
|
mergeDeep,
|
|
404
412
|
planckToUnit,
|
|
405
|
-
planckToUnitLegacy,
|
|
406
413
|
remToUnit,
|
|
407
414
|
removeVarFromUrlHash,
|
|
408
415
|
removedFrom,
|
|
409
416
|
setStateWithRef,
|
|
410
417
|
snakeToCamel,
|
|
411
418
|
sortWithNull,
|
|
412
|
-
stringToBigNumber,
|
|
413
419
|
transformToBaseUnit,
|
|
414
420
|
unescape,
|
|
415
421
|
unimplemented,
|