@w3ux/utils 0.10.1-alpha.4 → 1.0.0-alpha.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
@@ -35,6 +35,19 @@ __export(base_exports, {
35
35
  });
36
36
  module.exports = __toCommonJS(base_exports);
37
37
  var import_substrate_bindings = require("@polkadot-api/substrate-bindings");
38
+ var minDecimalPlaces = (val, minDecimals) => {
39
+ try {
40
+ const retainCommas = typeof val === "string" && val.includes(",");
41
+ const strVal = typeof val === "string" ? val.replace(/,/g, "") : val.toString();
42
+ const [integerPart, fractionalPart = ""] = strVal.split(".");
43
+ const whole = BigInt(integerPart || "0");
44
+ const missingDecimals = minDecimals - fractionalPart.length;
45
+ const formattedWhole = retainCommas ? Intl.NumberFormat("en-US").format(whole) : whole.toString();
46
+ return missingDecimals > 0 ? `${formattedWhole}.${fractionalPart}${"0".repeat(missingDecimals)}` : `${formattedWhole}.${fractionalPart}`;
47
+ } catch (e) {
48
+ return "0";
49
+ }
50
+ };
38
51
  var camelize = (str) => {
39
52
  const convertToString = (string) => {
40
53
  if (string) {
@@ -89,16 +102,6 @@ var ellipsisFn = (str, amount = 6, position = "center") => {
89
102
  return "..." + str.slice(amount);
90
103
  }
91
104
  };
92
- var minDecimalPlaces = (val, minDecimals) => {
93
- try {
94
- const whole = BigInt(rmCommas(val).split(".")[0] || "0");
95
- const decimals = val.split(".")[1] || "";
96
- const missingDecimals = minDecimals - decimals.length;
97
- return missingDecimals > 0 ? `${whole.toString()}.${decimals}${"0".repeat(missingDecimals)}` : val;
98
- } catch (e) {
99
- return "0";
100
- }
101
- };
102
105
  var pageFromUri = (pathname, fallback) => {
103
106
  const lastUriItem = pathname.substring(pathname.lastIndexOf("/") + 1);
104
107
  const page = lastUriItem.trim() === "" ? fallback : lastUriItem;
package/base.d.cts CHANGED
@@ -1,5 +1,24 @@
1
1
  import { AnyFunction } from '@w3ux/types';
2
2
 
3
+ /**
4
+ * Ensures a number has at least the specified number of decimal places, retaining commas in the output if they are present in the input.
5
+ *
6
+ * @function minDecimalPlaces
7
+ * @param {string | number | BigInt} val - The input number, which can be a `string` with or without commas, a `number`, or a `BigInt`.
8
+ * @param {number} minDecimals - The minimum number of decimal places to enforce.
9
+ * @returns {string} The formatted number as a string, padded with zeros if needed to meet `minDecimals`, retaining commas if originally provided.
10
+ * If `val` is invalid, returns "0".
11
+ * @example
12
+ * // Pads "1,234.5" to have at least 3 decimal places, with commas
13
+ * minDecimalPlaces("1,234.5", 3); // returns "1,234.500"
14
+ *
15
+ * // Returns "1234.56" unchanged
16
+ * minDecimalPlaces(1234.56, 2); // returns "1234.56"
17
+ *
18
+ * // Pads BigInt 1234 with 2 decimals
19
+ * minDecimalPlaces(BigInt(1234), 2); // returns "1234.00"
20
+ */
21
+ declare const minDecimalPlaces: (val: string | number | bigint, minDecimals: number) => string;
3
22
  /**
4
23
  * @name camelize
5
24
  * @summary Converts a string of text to camelCase.
@@ -14,11 +33,6 @@ declare const camelize: (str: string) => string;
14
33
  * same for beginning and end; if "start" or "end" then its only once the amount; defaults to "start"
15
34
  */
16
35
  declare const ellipsisFn: (str: string, amount?: number, position?: "start" | "end" | "center") => string;
17
- /**
18
- * @name minDecimalPlaces
19
- * @summary Forces a number to have at least the provided decimal places.
20
- */
21
- declare const minDecimalPlaces: (val: string, minDecimals: number) => string;
22
36
  /**
23
37
  * @name pageFromUri
24
38
  * @summary Use url variables to load the default components upon the first page visit.
package/base.d.ts CHANGED
@@ -1,5 +1,24 @@
1
1
  import { AnyFunction } from '@w3ux/types';
2
2
 
3
+ /**
4
+ * Ensures a number has at least the specified number of decimal places, retaining commas in the output if they are present in the input.
5
+ *
6
+ * @function minDecimalPlaces
7
+ * @param {string | number | BigInt} val - The input number, which can be a `string` with or without commas, a `number`, or a `BigInt`.
8
+ * @param {number} minDecimals - The minimum number of decimal places to enforce.
9
+ * @returns {string} The formatted number as a string, padded with zeros if needed to meet `minDecimals`, retaining commas if originally provided.
10
+ * If `val` is invalid, returns "0".
11
+ * @example
12
+ * // Pads "1,234.5" to have at least 3 decimal places, with commas
13
+ * minDecimalPlaces("1,234.5", 3); // returns "1,234.500"
14
+ *
15
+ * // Returns "1234.56" unchanged
16
+ * minDecimalPlaces(1234.56, 2); // returns "1234.56"
17
+ *
18
+ * // Pads BigInt 1234 with 2 decimals
19
+ * minDecimalPlaces(BigInt(1234), 2); // returns "1234.00"
20
+ */
21
+ declare const minDecimalPlaces: (val: string | number | bigint, minDecimals: number) => string;
3
22
  /**
4
23
  * @name camelize
5
24
  * @summary Converts a string of text to camelCase.
@@ -14,11 +33,6 @@ declare const camelize: (str: string) => string;
14
33
  * same for beginning and end; if "start" or "end" then its only once the amount; defaults to "start"
15
34
  */
16
35
  declare const ellipsisFn: (str: string, amount?: number, position?: "start" | "end" | "center") => string;
17
- /**
18
- * @name minDecimalPlaces
19
- * @summary Forces a number to have at least the provided decimal places.
20
- */
21
- declare const minDecimalPlaces: (val: string, minDecimals: number) => string;
22
36
  /**
23
37
  * @name pageFromUri
24
38
  * @summary Use url variables to load the default components upon the first page visit.
package/base.js CHANGED
@@ -1,5 +1,18 @@
1
1
  // src/base.ts
2
2
  import { AccountId } from "@polkadot-api/substrate-bindings";
3
+ var minDecimalPlaces = (val, minDecimals) => {
4
+ try {
5
+ const retainCommas = typeof val === "string" && val.includes(",");
6
+ const strVal = typeof val === "string" ? val.replace(/,/g, "") : val.toString();
7
+ const [integerPart, fractionalPart = ""] = strVal.split(".");
8
+ const whole = BigInt(integerPart || "0");
9
+ const missingDecimals = minDecimals - fractionalPart.length;
10
+ const formattedWhole = retainCommas ? Intl.NumberFormat("en-US").format(whole) : whole.toString();
11
+ return missingDecimals > 0 ? `${formattedWhole}.${fractionalPart}${"0".repeat(missingDecimals)}` : `${formattedWhole}.${fractionalPart}`;
12
+ } catch (e) {
13
+ return "0";
14
+ }
15
+ };
3
16
  var camelize = (str) => {
4
17
  const convertToString = (string) => {
5
18
  if (string) {
@@ -54,16 +67,6 @@ var ellipsisFn = (str, amount = 6, position = "center") => {
54
67
  return "..." + str.slice(amount);
55
68
  }
56
69
  };
57
- var minDecimalPlaces = (val, minDecimals) => {
58
- try {
59
- const whole = BigInt(rmCommas(val).split(".")[0] || "0");
60
- const decimals = val.split(".")[1] || "";
61
- const missingDecimals = minDecimals - decimals.length;
62
- return missingDecimals > 0 ? `${whole.toString()}.${decimals}${"0".repeat(missingDecimals)}` : val;
63
- } catch (e) {
64
- return "0";
65
- }
66
- };
67
70
  var pageFromUri = (pathname, fallback) => {
68
71
  const lastUriItem = pathname.substring(pathname.lastIndexOf("/") + 1);
69
72
  const page = lastUriItem.trim() === "" ? fallback : lastUriItem;
package/index.cjs CHANGED
@@ -71,6 +71,19 @@ module.exports = __toCommonJS(src_exports);
71
71
 
72
72
  // src/base.ts
73
73
  var import_substrate_bindings = require("@polkadot-api/substrate-bindings");
74
+ var minDecimalPlaces = (val, minDecimals) => {
75
+ try {
76
+ const retainCommas = typeof val === "string" && val.includes(",");
77
+ const strVal = typeof val === "string" ? val.replace(/,/g, "") : val.toString();
78
+ const [integerPart, fractionalPart = ""] = strVal.split(".");
79
+ const whole = BigInt(integerPart || "0");
80
+ const missingDecimals = minDecimals - fractionalPart.length;
81
+ const formattedWhole = retainCommas ? Intl.NumberFormat("en-US").format(whole) : whole.toString();
82
+ return missingDecimals > 0 ? `${formattedWhole}.${fractionalPart}${"0".repeat(missingDecimals)}` : `${formattedWhole}.${fractionalPart}`;
83
+ } catch (e) {
84
+ return "0";
85
+ }
86
+ };
74
87
  var camelize = (str) => {
75
88
  const convertToString = (string) => {
76
89
  if (string) {
@@ -125,16 +138,6 @@ var ellipsisFn = (str, amount = 6, position = "center") => {
125
138
  return "..." + str.slice(amount);
126
139
  }
127
140
  };
128
- var minDecimalPlaces = (val, minDecimals) => {
129
- try {
130
- const whole = BigInt(rmCommas(val).split(".")[0] || "0");
131
- const decimals = val.split(".")[1] || "";
132
- const missingDecimals = minDecimals - decimals.length;
133
- return missingDecimals > 0 ? `${whole.toString()}.${decimals}${"0".repeat(missingDecimals)}` : val;
134
- } catch (e) {
135
- return "0";
136
- }
137
- };
138
141
  var pageFromUri = (pathname, fallback) => {
139
142
  const lastUriItem = pathname.substring(pathname.lastIndexOf("/") + 1);
140
143
  const page = lastUriItem.trim() === "" ? fallback : lastUriItem;
@@ -334,7 +337,6 @@ function u8aUnwrapBytes(bytes) {
334
337
 
335
338
  // src/unit.ts
336
339
  var import_substrate_bindings2 = require("@polkadot-api/substrate-bindings");
337
- var remToUnit = (rem) => Number(rem.slice(0, rem.length - 3)) * parseFloat(getComputedStyle(document.documentElement).fontSize);
338
340
  var planckToUnit = (val, units) => {
339
341
  try {
340
342
  units = Math.max(units, 0);
@@ -363,6 +365,7 @@ var unitToPlanck = (val, units) => {
363
365
  return BigInt(0);
364
366
  }
365
367
  };
368
+ var remToUnit = (rem) => Number(rem.slice(0, rem.length - 3)) * parseFloat(getComputedStyle(document.documentElement).fontSize);
366
369
  var capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1);
367
370
  var snakeToCamel = (str) => str.toLowerCase().replace(
368
371
  /([-_][a-z])/g,
package/index.js CHANGED
@@ -1,5 +1,18 @@
1
1
  // src/base.ts
2
2
  import { AccountId } from "@polkadot-api/substrate-bindings";
3
+ var minDecimalPlaces = (val, minDecimals) => {
4
+ try {
5
+ const retainCommas = typeof val === "string" && val.includes(",");
6
+ const strVal = typeof val === "string" ? val.replace(/,/g, "") : val.toString();
7
+ const [integerPart, fractionalPart = ""] = strVal.split(".");
8
+ const whole = BigInt(integerPart || "0");
9
+ const missingDecimals = minDecimals - fractionalPart.length;
10
+ const formattedWhole = retainCommas ? Intl.NumberFormat("en-US").format(whole) : whole.toString();
11
+ return missingDecimals > 0 ? `${formattedWhole}.${fractionalPart}${"0".repeat(missingDecimals)}` : `${formattedWhole}.${fractionalPart}`;
12
+ } catch (e) {
13
+ return "0";
14
+ }
15
+ };
3
16
  var camelize = (str) => {
4
17
  const convertToString = (string) => {
5
18
  if (string) {
@@ -54,16 +67,6 @@ var ellipsisFn = (str, amount = 6, position = "center") => {
54
67
  return "..." + str.slice(amount);
55
68
  }
56
69
  };
57
- var minDecimalPlaces = (val, minDecimals) => {
58
- try {
59
- const whole = BigInt(rmCommas(val).split(".")[0] || "0");
60
- const decimals = val.split(".")[1] || "";
61
- const missingDecimals = minDecimals - decimals.length;
62
- return missingDecimals > 0 ? `${whole.toString()}.${decimals}${"0".repeat(missingDecimals)}` : val;
63
- } catch (e) {
64
- return "0";
65
- }
66
- };
67
70
  var pageFromUri = (pathname, fallback) => {
68
71
  const lastUriItem = pathname.substring(pathname.lastIndexOf("/") + 1);
69
72
  const page = lastUriItem.trim() === "" ? fallback : lastUriItem;
@@ -263,7 +266,6 @@ function u8aUnwrapBytes(bytes) {
263
266
 
264
267
  // src/unit.ts
265
268
  import { AccountId as AccountId2 } from "@polkadot-api/substrate-bindings";
266
- var remToUnit = (rem) => Number(rem.slice(0, rem.length - 3)) * parseFloat(getComputedStyle(document.documentElement).fontSize);
267
269
  var planckToUnit = (val, units) => {
268
270
  try {
269
271
  units = Math.max(units, 0);
@@ -292,6 +294,7 @@ var unitToPlanck = (val, units) => {
292
294
  return BigInt(0);
293
295
  }
294
296
  };
297
+ var remToUnit = (rem) => Number(rem.slice(0, rem.length - 3)) * parseFloat(getComputedStyle(document.documentElement).fontSize);
295
298
  var capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1);
296
299
  var snakeToCamel = (str) => str.toLowerCase().replace(
297
300
  /([-_][a-z])/g,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@w3ux/utils",
3
- "version": "0.10.1-alpha.4",
3
+ "version": "1.0.0-alpha.0",
4
4
  "license": "GPL-3.0-only",
5
5
  "dependencies": {
6
6
  "@polkadot-api/substrate-bindings": "^0.9.3"
package/unit.cjs CHANGED
@@ -233,7 +233,6 @@ var ellipsisFn = (str, amount = 6, position = "center") => {
233
233
 
234
234
  // src/unit.ts
235
235
  var import_substrate_bindings2 = require("@polkadot-api/substrate-bindings");
236
- var remToUnit = (rem) => Number(rem.slice(0, rem.length - 3)) * parseFloat(getComputedStyle(document.documentElement).fontSize);
237
236
  var planckToUnit = (val, units) => {
238
237
  try {
239
238
  units = Math.max(units, 0);
@@ -262,6 +261,7 @@ var unitToPlanck = (val, units) => {
262
261
  return BigInt(0);
263
262
  }
264
263
  };
264
+ var remToUnit = (rem) => Number(rem.slice(0, rem.length - 3)) * parseFloat(getComputedStyle(document.documentElement).fontSize);
265
265
  var capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1);
266
266
  var snakeToCamel = (str) => str.toLowerCase().replace(
267
267
  /([-_][a-z])/g,
package/unit.d.cts CHANGED
@@ -3,26 +3,34 @@ import { AnyObject } from './types.cjs';
3
3
  import { AnyJson } from '@w3ux/types';
4
4
 
5
5
  /**
6
- * @name remToUnit
7
- * @summary Converts a rem string to a number.
8
- */
9
- declare const remToUnit: (rem: string) => number;
10
- /**
11
- * @name planckToUnit
12
- * @summary Convert planck to the token unit.
13
- * @description
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).
6
+ * Converts an on-chain balance value from planck to a decimal value in token units.
7
+ *
8
+ * @function planckToUnit
9
+ * @param {number | BigInt | string} val - The balance value in planck. Accepts a `number`, `BigInt`, or `string`.
10
+ * @param {number} units - The number of decimal places in the token unit (10^units planck per 1 token).
11
+ * @returns {string} The equivalent token unit value as a decimal string.
12
+ * @example
13
+ * // Convert 1500000000000 planck to tokens with 12 decimal places
14
+ * planckToUnit("1500000000000", 12); // returns "1.5"
16
15
  */
17
16
  declare const planckToUnit: (val: number | bigint | string, units: number) => string;
18
17
  /**
19
- * @name unitToPlanck
20
- * @summary Convert token unit to planck.
21
- * @description
22
- * Converts a token unit value (as `string`, `number`, or `BigInt`) to an integer value in planck.
23
- * (1 token = 10^units planck).
18
+ * Converts a token unit value to an integer value in planck.
19
+ *
20
+ * @function unitToPlanck
21
+ * @param {string | number | BigInt} val - The token unit value to convert. Accepts a string, number, or BigInt.
22
+ * @param {number} units - The number of decimal places for conversion (10^units planck per 1 token).
23
+ * @returns {BigInt} The equivalent value in planck as a BigInt.
24
+ * @example
25
+ * // Convert "1.5" tokens to planck with 12 decimal places
26
+ * unitToPlanck("1.5", 12); // returns BigInt("1500000000000")
24
27
  */
25
28
  declare const unitToPlanck: (val: string | number | bigint, units: number) => bigint;
29
+ /**
30
+ * @name remToUnit
31
+ * @summary Converts a rem string to a number.
32
+ */
33
+ declare const remToUnit: (rem: string) => number;
26
34
  /**
27
35
  * @name capitalizeFirstLetter
28
36
  * @summary Capitalize the first letter of a string.
package/unit.d.ts CHANGED
@@ -3,26 +3,34 @@ import { AnyObject } from './types.js';
3
3
  import { AnyJson } from '@w3ux/types';
4
4
 
5
5
  /**
6
- * @name remToUnit
7
- * @summary Converts a rem string to a number.
8
- */
9
- declare const remToUnit: (rem: string) => number;
10
- /**
11
- * @name planckToUnit
12
- * @summary Convert planck to the token unit.
13
- * @description
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).
6
+ * Converts an on-chain balance value from planck to a decimal value in token units.
7
+ *
8
+ * @function planckToUnit
9
+ * @param {number | BigInt | string} val - The balance value in planck. Accepts a `number`, `BigInt`, or `string`.
10
+ * @param {number} units - The number of decimal places in the token unit (10^units planck per 1 token).
11
+ * @returns {string} The equivalent token unit value as a decimal string.
12
+ * @example
13
+ * // Convert 1500000000000 planck to tokens with 12 decimal places
14
+ * planckToUnit("1500000000000", 12); // returns "1.5"
16
15
  */
17
16
  declare const planckToUnit: (val: number | bigint | string, units: number) => string;
18
17
  /**
19
- * @name unitToPlanck
20
- * @summary Convert token unit to planck.
21
- * @description
22
- * Converts a token unit value (as `string`, `number`, or `BigInt`) to an integer value in planck.
23
- * (1 token = 10^units planck).
18
+ * Converts a token unit value to an integer value in planck.
19
+ *
20
+ * @function unitToPlanck
21
+ * @param {string | number | BigInt} val - The token unit value to convert. Accepts a string, number, or BigInt.
22
+ * @param {number} units - The number of decimal places for conversion (10^units planck per 1 token).
23
+ * @returns {BigInt} The equivalent value in planck as a BigInt.
24
+ * @example
25
+ * // Convert "1.5" tokens to planck with 12 decimal places
26
+ * unitToPlanck("1.5", 12); // returns BigInt("1500000000000")
24
27
  */
25
28
  declare const unitToPlanck: (val: string | number | bigint, units: number) => bigint;
29
+ /**
30
+ * @name remToUnit
31
+ * @summary Converts a rem string to a number.
32
+ */
33
+ declare const remToUnit: (rem: string) => number;
26
34
  /**
27
35
  * @name capitalizeFirstLetter
28
36
  * @summary Capitalize the first letter of a string.
package/unit.js CHANGED
@@ -175,7 +175,6 @@ var ellipsisFn = (str, amount = 6, position = "center") => {
175
175
 
176
176
  // src/unit.ts
177
177
  import { AccountId as AccountId2 } from "@polkadot-api/substrate-bindings";
178
- var remToUnit = (rem) => Number(rem.slice(0, rem.length - 3)) * parseFloat(getComputedStyle(document.documentElement).fontSize);
179
178
  var planckToUnit = (val, units) => {
180
179
  try {
181
180
  units = Math.max(units, 0);
@@ -204,6 +203,7 @@ var unitToPlanck = (val, units) => {
204
203
  return BigInt(0);
205
204
  }
206
205
  };
206
+ var remToUnit = (rem) => Number(rem.slice(0, rem.length - 3)) * parseFloat(getComputedStyle(document.documentElement).fontSize);
207
207
  var capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1);
208
208
  var snakeToCamel = (str) => str.toLowerCase().replace(
209
209
  /([-_][a-z])/g,