@w3ux/utils 0.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.
- package/base.d.ts +66 -0
- package/base.js +113 -0
- package/index.d.ts +5 -0
- package/index.js +400 -0
- package/package.json +6 -0
- package/types.d.ts +12 -0
- package/types.js +14 -0
- package/unit.d.ts +165 -0
- package/unit.js +321 -0
package/base.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { BigNumber } from 'bignumber.js';
|
|
2
|
+
import { AnyFunction } from './types.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @name camelize
|
|
6
|
+
* @summary Converts a string of text to camelCase.
|
|
7
|
+
*/
|
|
8
|
+
declare const camelize: (str: string) => string;
|
|
9
|
+
/**
|
|
10
|
+
* @name ellipsisFn
|
|
11
|
+
* @summary Receives an address and creates ellipsis on the given string, based on parameters.
|
|
12
|
+
* @param str - The string to apply the ellipsis on
|
|
13
|
+
* @param amount - The amount of characters that the ellipsis will be
|
|
14
|
+
* @param position - where the ellipsis will apply; if center the amount of character is the
|
|
15
|
+
* same for beginning and end; if "start" or "end" then its only once the amount; defaults to "start"
|
|
16
|
+
*/
|
|
17
|
+
declare const ellipsisFn: (str: string, amount?: number, position?: "start" | "end" | "center") => string;
|
|
18
|
+
/**
|
|
19
|
+
* @name greaterThanZero
|
|
20
|
+
* @summary Returns whether a BigNumber is greater than zero.
|
|
21
|
+
*/
|
|
22
|
+
declare const greaterThanZero: (val: BigNumber) => boolean;
|
|
23
|
+
/**
|
|
24
|
+
* @name isNotZero
|
|
25
|
+
* @summary Returns whether a BigNumber is zero.
|
|
26
|
+
*/
|
|
27
|
+
declare const isNotZero: (val: BigNumber) => boolean;
|
|
28
|
+
/**
|
|
29
|
+
* @name minDecimalPlaces
|
|
30
|
+
* @summary Forces a number to have at least the provided decimal places.
|
|
31
|
+
*/
|
|
32
|
+
declare const minDecimalPlaces: (val: string, minDecimals: number) => string;
|
|
33
|
+
/**
|
|
34
|
+
* @name pageFromUri
|
|
35
|
+
* @summary Use url variables to load the default components upon the first page visit.
|
|
36
|
+
*/
|
|
37
|
+
declare const pageFromUri: (pathname: string, fallback: string) => string;
|
|
38
|
+
/**
|
|
39
|
+
* @name rmCommas
|
|
40
|
+
* @summary Removes the commas from a string.
|
|
41
|
+
*/
|
|
42
|
+
declare const rmCommas: (val: string) => string;
|
|
43
|
+
/**
|
|
44
|
+
* @name shuffle
|
|
45
|
+
* @summary Shuffle a set of objects.
|
|
46
|
+
*/
|
|
47
|
+
declare const shuffle: <T>(array: T[]) => T[];
|
|
48
|
+
/**
|
|
49
|
+
* @name withTimeout
|
|
50
|
+
* @summary Timeout a promise after a specified number of milliseconds.
|
|
51
|
+
*/
|
|
52
|
+
declare const withTimeout: (ms: number, promise: AnyFunction, options?: {
|
|
53
|
+
onTimeout?: AnyFunction;
|
|
54
|
+
}) => Promise<any>;
|
|
55
|
+
/**
|
|
56
|
+
* @name appendOrEmpty
|
|
57
|
+
* @summary Returns ` value` if a condition is truthy, or an empty string otherwise.
|
|
58
|
+
*/
|
|
59
|
+
declare const appendOrEmpty: (condition: boolean | string | undefined, value: string) => string;
|
|
60
|
+
/**
|
|
61
|
+
* @name appendOr
|
|
62
|
+
* @summary Returns ` value` if condition is truthy, or ` fallback` otherwise.
|
|
63
|
+
*/
|
|
64
|
+
declare const appendOr: (condition: boolean | string | undefined, value: string, fallback: string) => string;
|
|
65
|
+
|
|
66
|
+
export { appendOr, appendOrEmpty, camelize, ellipsisFn, greaterThanZero, isNotZero, minDecimalPlaces, pageFromUri, rmCommas, shuffle, withTimeout };
|
package/base.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// src/base.ts
|
|
2
|
+
import { BigNumber } from "bignumber.js";
|
|
3
|
+
var camelize = (str) => {
|
|
4
|
+
const convertToString = (string) => {
|
|
5
|
+
if (string) {
|
|
6
|
+
if (typeof string === "string") {
|
|
7
|
+
return string;
|
|
8
|
+
}
|
|
9
|
+
return String(string);
|
|
10
|
+
}
|
|
11
|
+
return "";
|
|
12
|
+
};
|
|
13
|
+
const toWords = (inp) => convertToString(inp).match(
|
|
14
|
+
/[A-Z\xC0-\xD6\xD8-\xDE]?[a-z\xDF-\xF6\xF8-\xFF]+|[A-Z\xC0-\xD6\xD8-\xDE]+(?![a-z\xDF-\xF6\xF8-\xFF])|\d+/g
|
|
15
|
+
);
|
|
16
|
+
const simpleCamelCase = (inp) => {
|
|
17
|
+
let result = "";
|
|
18
|
+
for (let i = 0; i < inp?.length; i++) {
|
|
19
|
+
const currString = inp[i];
|
|
20
|
+
let tmpStr = currString.toLowerCase();
|
|
21
|
+
if (i != 0) {
|
|
22
|
+
tmpStr = tmpStr.slice(0, 1).toUpperCase() + tmpStr.slice(1, tmpStr.length);
|
|
23
|
+
}
|
|
24
|
+
result += tmpStr;
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
const w = toWords(str)?.map((a) => a.toLowerCase());
|
|
29
|
+
return simpleCamelCase(w);
|
|
30
|
+
};
|
|
31
|
+
var ellipsisFn = (str, amount = 6, position = "center") => {
|
|
32
|
+
const half = str.length / 2;
|
|
33
|
+
if (amount <= 4) {
|
|
34
|
+
if (position === "center") {
|
|
35
|
+
return str.slice(0, 4) + "..." + str.slice(-4);
|
|
36
|
+
}
|
|
37
|
+
if (position === "end") {
|
|
38
|
+
return str.slice(0, 4) + "...";
|
|
39
|
+
}
|
|
40
|
+
return "..." + str.slice(-4);
|
|
41
|
+
}
|
|
42
|
+
if (position === "center") {
|
|
43
|
+
return amount >= (str.length - 2) / 2 ? str.slice(0, half - 3) + "..." + str.slice(-(half - 3)) : str.slice(0, amount) + "..." + str.slice(-amount);
|
|
44
|
+
}
|
|
45
|
+
if (amount >= str.length) {
|
|
46
|
+
if (position === "end") {
|
|
47
|
+
return str.slice(0, str.length - 3) + "...";
|
|
48
|
+
}
|
|
49
|
+
return "..." + str.slice(-(str.length - 3));
|
|
50
|
+
} else {
|
|
51
|
+
if (position === "end") {
|
|
52
|
+
return str.slice(0, amount) + "...";
|
|
53
|
+
}
|
|
54
|
+
return "..." + str.slice(amount);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var greaterThanZero = (val) => val.isGreaterThan(0);
|
|
58
|
+
var isNotZero = (val) => !val.isZero();
|
|
59
|
+
var minDecimalPlaces = (val, minDecimals) => {
|
|
60
|
+
const whole = new BigNumber(rmCommas(val).split(".")[0] || 0);
|
|
61
|
+
const decimals = val.split(".")[1] || "";
|
|
62
|
+
const missingDecimals = new BigNumber(minDecimals).minus(decimals.length);
|
|
63
|
+
return missingDecimals.isGreaterThan(0) ? `${whole.toFormat(0)}.${decimals.toString()}${"0".repeat(
|
|
64
|
+
missingDecimals.toNumber()
|
|
65
|
+
)}` : val;
|
|
66
|
+
};
|
|
67
|
+
var pageFromUri = (pathname, fallback) => {
|
|
68
|
+
const lastUriItem = pathname.substring(pathname.lastIndexOf("/") + 1);
|
|
69
|
+
const page = lastUriItem.trim() === "" ? fallback : lastUriItem;
|
|
70
|
+
return page.trim();
|
|
71
|
+
};
|
|
72
|
+
var rmCommas = (val) => val.replace(/,/g, "");
|
|
73
|
+
var shuffle = (array) => {
|
|
74
|
+
let currentIndex = array.length;
|
|
75
|
+
let randomIndex;
|
|
76
|
+
while (currentIndex !== 0) {
|
|
77
|
+
randomIndex = Math.floor(Math.random() * currentIndex);
|
|
78
|
+
currentIndex--;
|
|
79
|
+
[array[currentIndex], array[randomIndex]] = [
|
|
80
|
+
array[randomIndex],
|
|
81
|
+
array[currentIndex]
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
return array;
|
|
85
|
+
};
|
|
86
|
+
var withTimeout = (ms, promise, options) => {
|
|
87
|
+
const timeout = new Promise(
|
|
88
|
+
(resolve) => setTimeout(async () => {
|
|
89
|
+
if (typeof options?.onTimeout === "function") {
|
|
90
|
+
options.onTimeout();
|
|
91
|
+
}
|
|
92
|
+
resolve(void 0);
|
|
93
|
+
}, ms)
|
|
94
|
+
);
|
|
95
|
+
return Promise.race([promise, timeout]);
|
|
96
|
+
};
|
|
97
|
+
var appendOrEmpty = (condition, value) => condition ? ` ${value}` : "";
|
|
98
|
+
var appendOr = (condition, value, fallback) => condition ? ` ${value}` : ` ${fallback}`;
|
|
99
|
+
export {
|
|
100
|
+
appendOr,
|
|
101
|
+
appendOrEmpty,
|
|
102
|
+
camelize,
|
|
103
|
+
ellipsisFn,
|
|
104
|
+
greaterThanZero,
|
|
105
|
+
isNotZero,
|
|
106
|
+
minDecimalPlaces,
|
|
107
|
+
pageFromUri,
|
|
108
|
+
rmCommas,
|
|
109
|
+
shuffle,
|
|
110
|
+
withTimeout
|
|
111
|
+
};
|
|
112
|
+
/* @license Copyright 2024 w3ux authors & contributors
|
|
113
|
+
SPDX-License-Identifier: GPL-3.0-only */
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { appendOr, appendOrEmpty, camelize, ellipsisFn, greaterThanZero, isNotZero, minDecimalPlaces, pageFromUri, rmCommas, shuffle, withTimeout } from './base.js';
|
|
2
|
+
export { addedTo, applyWidthAsPadding, capitalizeFirstLetter, determinePoolDisplay, evalUnits, extractUrlValue, inChrome, isValidAddress, isValidHttpUrl, localStorageOrDefault, makeCancelable, matchedProperties, mergeDeep, planckToUnit, remToUnit, removeVarFromUrlHash, removedFrom, setStateWithRef, snakeToCamel, sortWithNull, transformToBaseUnit, unescape, unimplemented, unitToPlanck, varToUrlHash } from './unit.js';
|
|
3
|
+
import 'bignumber.js';
|
|
4
|
+
import './types.js';
|
|
5
|
+
import 'react';
|
package/index.js
ADDED
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
// src/base.ts
|
|
2
|
+
import { BigNumber } from "bignumber.js";
|
|
3
|
+
var camelize = (str) => {
|
|
4
|
+
const convertToString = (string) => {
|
|
5
|
+
if (string) {
|
|
6
|
+
if (typeof string === "string") {
|
|
7
|
+
return string;
|
|
8
|
+
}
|
|
9
|
+
return String(string);
|
|
10
|
+
}
|
|
11
|
+
return "";
|
|
12
|
+
};
|
|
13
|
+
const toWords = (inp) => convertToString(inp).match(
|
|
14
|
+
/[A-Z\xC0-\xD6\xD8-\xDE]?[a-z\xDF-\xF6\xF8-\xFF]+|[A-Z\xC0-\xD6\xD8-\xDE]+(?![a-z\xDF-\xF6\xF8-\xFF])|\d+/g
|
|
15
|
+
);
|
|
16
|
+
const simpleCamelCase = (inp) => {
|
|
17
|
+
let result = "";
|
|
18
|
+
for (let i = 0; i < inp?.length; i++) {
|
|
19
|
+
const currString = inp[i];
|
|
20
|
+
let tmpStr = currString.toLowerCase();
|
|
21
|
+
if (i != 0) {
|
|
22
|
+
tmpStr = tmpStr.slice(0, 1).toUpperCase() + tmpStr.slice(1, tmpStr.length);
|
|
23
|
+
}
|
|
24
|
+
result += tmpStr;
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
const w = toWords(str)?.map((a) => a.toLowerCase());
|
|
29
|
+
return simpleCamelCase(w);
|
|
30
|
+
};
|
|
31
|
+
var ellipsisFn = (str, amount = 6, position = "center") => {
|
|
32
|
+
const half = str.length / 2;
|
|
33
|
+
if (amount <= 4) {
|
|
34
|
+
if (position === "center") {
|
|
35
|
+
return str.slice(0, 4) + "..." + str.slice(-4);
|
|
36
|
+
}
|
|
37
|
+
if (position === "end") {
|
|
38
|
+
return str.slice(0, 4) + "...";
|
|
39
|
+
}
|
|
40
|
+
return "..." + str.slice(-4);
|
|
41
|
+
}
|
|
42
|
+
if (position === "center") {
|
|
43
|
+
return amount >= (str.length - 2) / 2 ? str.slice(0, half - 3) + "..." + str.slice(-(half - 3)) : str.slice(0, amount) + "..." + str.slice(-amount);
|
|
44
|
+
}
|
|
45
|
+
if (amount >= str.length) {
|
|
46
|
+
if (position === "end") {
|
|
47
|
+
return str.slice(0, str.length - 3) + "...";
|
|
48
|
+
}
|
|
49
|
+
return "..." + str.slice(-(str.length - 3));
|
|
50
|
+
} else {
|
|
51
|
+
if (position === "end") {
|
|
52
|
+
return str.slice(0, amount) + "...";
|
|
53
|
+
}
|
|
54
|
+
return "..." + str.slice(amount);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var greaterThanZero = (val) => val.isGreaterThan(0);
|
|
58
|
+
var isNotZero = (val) => !val.isZero();
|
|
59
|
+
var minDecimalPlaces = (val, minDecimals) => {
|
|
60
|
+
const whole = new BigNumber(rmCommas(val).split(".")[0] || 0);
|
|
61
|
+
const decimals = val.split(".")[1] || "";
|
|
62
|
+
const missingDecimals = new BigNumber(minDecimals).minus(decimals.length);
|
|
63
|
+
return missingDecimals.isGreaterThan(0) ? `${whole.toFormat(0)}.${decimals.toString()}${"0".repeat(
|
|
64
|
+
missingDecimals.toNumber()
|
|
65
|
+
)}` : val;
|
|
66
|
+
};
|
|
67
|
+
var pageFromUri = (pathname, fallback) => {
|
|
68
|
+
const lastUriItem = pathname.substring(pathname.lastIndexOf("/") + 1);
|
|
69
|
+
const page = lastUriItem.trim() === "" ? fallback : lastUriItem;
|
|
70
|
+
return page.trim();
|
|
71
|
+
};
|
|
72
|
+
var rmCommas = (val) => val.replace(/,/g, "");
|
|
73
|
+
var shuffle = (array) => {
|
|
74
|
+
let currentIndex = array.length;
|
|
75
|
+
let randomIndex;
|
|
76
|
+
while (currentIndex !== 0) {
|
|
77
|
+
randomIndex = Math.floor(Math.random() * currentIndex);
|
|
78
|
+
currentIndex--;
|
|
79
|
+
[array[currentIndex], array[randomIndex]] = [
|
|
80
|
+
array[randomIndex],
|
|
81
|
+
array[currentIndex]
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
return array;
|
|
85
|
+
};
|
|
86
|
+
var withTimeout = (ms, promise, options) => {
|
|
87
|
+
const timeout = new Promise(
|
|
88
|
+
(resolve) => setTimeout(async () => {
|
|
89
|
+
if (typeof options?.onTimeout === "function") {
|
|
90
|
+
options.onTimeout();
|
|
91
|
+
}
|
|
92
|
+
resolve(void 0);
|
|
93
|
+
}, ms)
|
|
94
|
+
);
|
|
95
|
+
return Promise.race([promise, timeout]);
|
|
96
|
+
};
|
|
97
|
+
var appendOrEmpty = (condition, value) => condition ? ` ${value}` : "";
|
|
98
|
+
var appendOr = (condition, value, fallback) => condition ? ` ${value}` : ` ${fallback}`;
|
|
99
|
+
|
|
100
|
+
// src/unit.ts
|
|
101
|
+
import { decodeAddress, encodeAddress } from "@polkadot/keyring";
|
|
102
|
+
import { hexToU8a, isHex, u8aToString, u8aUnwrapBytes } from "@polkadot/util";
|
|
103
|
+
import { BigNumber as BigNumber2 } from "bignumber.js";
|
|
104
|
+
var remToUnit = (rem) => Number(rem.slice(0, rem.length - 3)) * parseFloat(getComputedStyle(document.documentElement).fontSize);
|
|
105
|
+
var planckToUnit = (val, units) => new BigNumber2(
|
|
106
|
+
val.dividedBy(new BigNumber2(10).exponentiatedBy(units)).toFixed(units)
|
|
107
|
+
);
|
|
108
|
+
var unitToPlanck = (val, units) => {
|
|
109
|
+
const init = new BigNumber2(!val.length || !val ? "0" : val);
|
|
110
|
+
return (!init.isNaN() ? init : new BigNumber2(0)).multipliedBy(new BigNumber2(10).exponentiatedBy(units)).integerValue();
|
|
111
|
+
};
|
|
112
|
+
var capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1);
|
|
113
|
+
var snakeToCamel = (str) => str.toLowerCase().replace(
|
|
114
|
+
/([-_][a-z])/g,
|
|
115
|
+
(group) => group.toUpperCase().replace("-", "").replace("_", "")
|
|
116
|
+
);
|
|
117
|
+
var setStateWithRef = (value, setState, ref) => {
|
|
118
|
+
setState(value);
|
|
119
|
+
ref.current = value;
|
|
120
|
+
};
|
|
121
|
+
var localStorageOrDefault = (key, _default, parse = false) => {
|
|
122
|
+
const val = localStorage.getItem(key);
|
|
123
|
+
if (val === null) {
|
|
124
|
+
return _default;
|
|
125
|
+
}
|
|
126
|
+
if (parse) {
|
|
127
|
+
return JSON.parse(val);
|
|
128
|
+
}
|
|
129
|
+
return val;
|
|
130
|
+
};
|
|
131
|
+
var isValidAddress = (address) => {
|
|
132
|
+
try {
|
|
133
|
+
encodeAddress(isHex(address) ? hexToU8a(address) : decodeAddress(address));
|
|
134
|
+
return true;
|
|
135
|
+
} catch (e) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
var determinePoolDisplay = (address, batchItem) => {
|
|
140
|
+
const defaultDisplay = ellipsisFn(address, 6);
|
|
141
|
+
let display = batchItem ?? defaultDisplay;
|
|
142
|
+
const displayAsBytes = u8aToString(u8aUnwrapBytes(display));
|
|
143
|
+
if (displayAsBytes !== "") {
|
|
144
|
+
display = displayAsBytes;
|
|
145
|
+
}
|
|
146
|
+
if (display === "") {
|
|
147
|
+
display = defaultDisplay;
|
|
148
|
+
}
|
|
149
|
+
return display;
|
|
150
|
+
};
|
|
151
|
+
var extractUrlValue = (key, url) => {
|
|
152
|
+
if (typeof url === "undefined") {
|
|
153
|
+
url = window.location.href;
|
|
154
|
+
}
|
|
155
|
+
const match = url.match(`[?&]${key}=([^&]+)`);
|
|
156
|
+
return match ? match[1] : null;
|
|
157
|
+
};
|
|
158
|
+
var varToUrlHash = (key, val, addIfMissing) => {
|
|
159
|
+
const hash = window.location.hash;
|
|
160
|
+
const [page, params] = hash.split("?");
|
|
161
|
+
const searchParams = new URLSearchParams(params);
|
|
162
|
+
if (searchParams.get(key) === null && !addIfMissing) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
searchParams.set(key, val);
|
|
166
|
+
window.location.hash = `${page}?${searchParams.toString()}`;
|
|
167
|
+
};
|
|
168
|
+
var removeVarFromUrlHash = (key) => {
|
|
169
|
+
const hash = window.location.hash;
|
|
170
|
+
const [page, params] = hash.split("?");
|
|
171
|
+
const searchParams = new URLSearchParams(params);
|
|
172
|
+
if (searchParams.get(key) === null) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
searchParams.delete(key);
|
|
176
|
+
const paramsAsStr = searchParams.toString();
|
|
177
|
+
window.location.hash = `${page}${paramsAsStr ? `?${paramsAsStr}` : ``}`;
|
|
178
|
+
};
|
|
179
|
+
var sortWithNull = (ascending) => (a, b) => {
|
|
180
|
+
if (a === b) {
|
|
181
|
+
return 0;
|
|
182
|
+
}
|
|
183
|
+
if (a === null) {
|
|
184
|
+
return 1;
|
|
185
|
+
}
|
|
186
|
+
if (b === null) {
|
|
187
|
+
return -1;
|
|
188
|
+
}
|
|
189
|
+
if (ascending) {
|
|
190
|
+
return a < b ? -1 : 1;
|
|
191
|
+
}
|
|
192
|
+
return a < b ? 1 : -1;
|
|
193
|
+
};
|
|
194
|
+
var applyWidthAsPadding = (subjectRef, containerRef) => {
|
|
195
|
+
if (containerRef.current && subjectRef.current) {
|
|
196
|
+
containerRef.current.style.paddingRight = `${subjectRef.current.offsetWidth + remToUnit("1rem")}px`;
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
var unescape = (val) => val.replace(/\\"/g, '"');
|
|
200
|
+
var inChrome = () => {
|
|
201
|
+
const isChromium = window?.chrome || null;
|
|
202
|
+
const winNav = window?.navigator || null;
|
|
203
|
+
const isOpera = typeof window?.opr !== "undefined";
|
|
204
|
+
const isIEedge = winNav?.userAgent.indexOf("Edg") > -1 || false;
|
|
205
|
+
const isIOSChrome = winNav?.userAgent.match("CriOS") || false;
|
|
206
|
+
if (isIOSChrome) {
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
if (isChromium !== null && typeof isChromium !== "undefined" && isOpera === false && isIEedge === false) {
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
return false;
|
|
213
|
+
};
|
|
214
|
+
var addedTo = (fresh, stale, keys) => typeof fresh !== "object" || typeof stale !== "object" || !keys.length ? [] : fresh.filter(
|
|
215
|
+
(freshItem) => !stale.find(
|
|
216
|
+
(staleItem) => keys.every(
|
|
217
|
+
(key) => !(key in staleItem) || !(key in freshItem) ? false : staleItem[key] === freshItem[key]
|
|
218
|
+
)
|
|
219
|
+
)
|
|
220
|
+
);
|
|
221
|
+
var removedFrom = (fresh, stale, keys) => typeof fresh !== "object" || typeof stale !== "object" || !keys.length ? [] : stale.filter(
|
|
222
|
+
(staleItem) => !fresh.find(
|
|
223
|
+
(freshItem) => keys.every(
|
|
224
|
+
(key) => !(key in staleItem) || !(key in freshItem) ? false : freshItem[key] === staleItem[key]
|
|
225
|
+
)
|
|
226
|
+
)
|
|
227
|
+
);
|
|
228
|
+
var matchedProperties = (objX, objY, keys) => typeof objX !== "object" || typeof objY !== "object" || !keys.length ? [] : objY.filter(
|
|
229
|
+
(x) => objX.find(
|
|
230
|
+
(y) => keys.every(
|
|
231
|
+
(key) => !(key in x) || !(key in y) ? false : y[key] === x[key]
|
|
232
|
+
)
|
|
233
|
+
)
|
|
234
|
+
);
|
|
235
|
+
var isValidHttpUrl = (string) => {
|
|
236
|
+
let url;
|
|
237
|
+
try {
|
|
238
|
+
url = new URL(string);
|
|
239
|
+
} catch (_) {
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
return url.protocol === "http:" || url.protocol === "https:";
|
|
243
|
+
};
|
|
244
|
+
var makeCancelable = (promise) => {
|
|
245
|
+
let hasCanceled = false;
|
|
246
|
+
const wrappedPromise = new Promise((resolve, reject) => {
|
|
247
|
+
promise.then(
|
|
248
|
+
(val) => hasCanceled ? reject(Error("Cancelled")) : resolve(val)
|
|
249
|
+
);
|
|
250
|
+
promise.catch(
|
|
251
|
+
(error) => hasCanceled ? reject(Error("Cancelled")) : reject(error)
|
|
252
|
+
);
|
|
253
|
+
});
|
|
254
|
+
return {
|
|
255
|
+
promise: wrappedPromise,
|
|
256
|
+
cancel: () => {
|
|
257
|
+
hasCanceled = true;
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
};
|
|
261
|
+
var getSiValue = (si2) => new BigNumber2(10).pow(new BigNumber2(si2));
|
|
262
|
+
var si = [
|
|
263
|
+
{ value: getSiValue(24), symbol: "y", isMil: true },
|
|
264
|
+
{ value: getSiValue(21), symbol: "z", isMil: true },
|
|
265
|
+
{ value: getSiValue(18), symbol: "a", isMil: true },
|
|
266
|
+
{ value: getSiValue(15), symbol: "f", isMil: true },
|
|
267
|
+
{ value: getSiValue(12), symbol: "p", isMil: true },
|
|
268
|
+
{ value: getSiValue(9), symbol: "n", isMil: true },
|
|
269
|
+
{ value: getSiValue(6), symbol: "\u03BC", isMil: true },
|
|
270
|
+
{ value: getSiValue(3), symbol: "m", isMil: true },
|
|
271
|
+
{ value: new BigNumber2(1), symbol: "" },
|
|
272
|
+
{ value: getSiValue(3), symbol: "k" },
|
|
273
|
+
{ value: getSiValue(6), symbol: "M" },
|
|
274
|
+
{ value: getSiValue(9), symbol: "G" },
|
|
275
|
+
{ value: getSiValue(12), symbol: "T" },
|
|
276
|
+
{ value: getSiValue(15), symbol: "P" },
|
|
277
|
+
{ value: getSiValue(18), symbol: "E" },
|
|
278
|
+
{ value: getSiValue(21), symbol: "Y" },
|
|
279
|
+
{ value: getSiValue(24), symbol: "Z" }
|
|
280
|
+
];
|
|
281
|
+
var allowedSymbols = si.map((s) => s.symbol).join(", ").replace(", ,", ",");
|
|
282
|
+
var floats = new RegExp("^[+]?[0-9]*[.,]{1}[0-9]*$");
|
|
283
|
+
var ints = new RegExp("^[+]?[0-9]+$");
|
|
284
|
+
var alphaFloats = new RegExp(
|
|
285
|
+
"^[+]?[0-9]*[.,]{1}[0-9]*[" + allowedSymbols + "]{1}$"
|
|
286
|
+
);
|
|
287
|
+
var alphaInts = new RegExp("^[+]?[0-9]*[" + allowedSymbols + "]{1}$");
|
|
288
|
+
var evalUnits = (input, chainDecimals) => {
|
|
289
|
+
input = input && input.replace("+", "");
|
|
290
|
+
if (!floats.test(input) && !ints.test(input) && !alphaInts.test(input) && !alphaFloats.test(input)) {
|
|
291
|
+
return [null, "Input is not correct. Use numbers, floats or expression (e.g. 1k, 1.3m)" /* GIBBERISH */];
|
|
292
|
+
}
|
|
293
|
+
const symbol = input.replace(/[0-9.,]/g, "");
|
|
294
|
+
const siVal = si.find((s) => s.symbol === symbol);
|
|
295
|
+
const numberStr = input.replace(symbol, "").replace(",", ".");
|
|
296
|
+
let numeric = new BigNumber2(0);
|
|
297
|
+
if (!siVal) {
|
|
298
|
+
return [null, "Provided symbol is not correct" /* SYMBOL_ERROR */];
|
|
299
|
+
}
|
|
300
|
+
const decimalsBn = new BigNumber2(10).pow(new BigNumber2(chainDecimals));
|
|
301
|
+
const containDecimal = numberStr.includes(".");
|
|
302
|
+
const [decPart, fracPart] = numberStr.split(".");
|
|
303
|
+
const fracDecimals = fracPart?.length || 0;
|
|
304
|
+
const fracExp = new BigNumber2(10).pow(new BigNumber2(fracDecimals));
|
|
305
|
+
numeric = containDecimal ? new BigNumber2(
|
|
306
|
+
new BigNumber2(decPart).multipliedBy(fracExp).plus(new BigNumber2(fracPart))
|
|
307
|
+
) : new BigNumber2(new BigNumber2(numberStr));
|
|
308
|
+
numeric = numeric.multipliedBy(decimalsBn);
|
|
309
|
+
if (containDecimal) {
|
|
310
|
+
numeric = siVal.isMil ? numeric.dividedBy(siVal.value).dividedBy(fracExp) : numeric.multipliedBy(siVal.value).dividedBy(fracExp);
|
|
311
|
+
} else {
|
|
312
|
+
numeric = siVal.isMil ? numeric.dividedBy(siVal.value) : numeric.multipliedBy(siVal.value);
|
|
313
|
+
}
|
|
314
|
+
if (numeric.eq(new BigNumber2(0))) {
|
|
315
|
+
return [null, "You cannot send 0 funds" /* ZERO */];
|
|
316
|
+
}
|
|
317
|
+
return [numeric, "" /* SUCCESS */];
|
|
318
|
+
};
|
|
319
|
+
var transformToBaseUnit = (estFee, chainDecimals) => {
|
|
320
|
+
const t = estFee.length - chainDecimals;
|
|
321
|
+
let s = "";
|
|
322
|
+
if (t < 0) {
|
|
323
|
+
for (let i = 0; i < Math.abs(t) - 1; i++) {
|
|
324
|
+
s += "0";
|
|
325
|
+
}
|
|
326
|
+
s = s + estFee;
|
|
327
|
+
for (let i = 0; i < s.length; i++) {
|
|
328
|
+
if (s.slice(s.length - 1) !== "0") {
|
|
329
|
+
break;
|
|
330
|
+
}
|
|
331
|
+
s = s.substring(0, s.length - 1);
|
|
332
|
+
}
|
|
333
|
+
s = "0." + s;
|
|
334
|
+
} else {
|
|
335
|
+
s = (parseInt(estFee) / 10 ** chainDecimals).toString();
|
|
336
|
+
}
|
|
337
|
+
return parseFloat(s) !== 0 ? s : "0";
|
|
338
|
+
};
|
|
339
|
+
var unimplemented = ({ ...props }) => {
|
|
340
|
+
};
|
|
341
|
+
var mergeDeep = (target, ...sources) => {
|
|
342
|
+
if (!sources.length) {
|
|
343
|
+
return target;
|
|
344
|
+
}
|
|
345
|
+
const isObject = (item) => item && typeof item === "object" && !Array.isArray(item);
|
|
346
|
+
const source = sources.shift();
|
|
347
|
+
if (isObject(target) && isObject(source)) {
|
|
348
|
+
for (const key in source) {
|
|
349
|
+
if (isObject(source[key])) {
|
|
350
|
+
if (!target[key]) {
|
|
351
|
+
Object.assign(target, { [key]: {} });
|
|
352
|
+
}
|
|
353
|
+
mergeDeep(target[key], source[key]);
|
|
354
|
+
} else {
|
|
355
|
+
Object.assign(target, { [key]: source[key] });
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return mergeDeep(target, ...sources);
|
|
360
|
+
};
|
|
361
|
+
export {
|
|
362
|
+
addedTo,
|
|
363
|
+
appendOr,
|
|
364
|
+
appendOrEmpty,
|
|
365
|
+
applyWidthAsPadding,
|
|
366
|
+
camelize,
|
|
367
|
+
capitalizeFirstLetter,
|
|
368
|
+
determinePoolDisplay,
|
|
369
|
+
ellipsisFn,
|
|
370
|
+
evalUnits,
|
|
371
|
+
extractUrlValue,
|
|
372
|
+
greaterThanZero,
|
|
373
|
+
inChrome,
|
|
374
|
+
isNotZero,
|
|
375
|
+
isValidAddress,
|
|
376
|
+
isValidHttpUrl,
|
|
377
|
+
localStorageOrDefault,
|
|
378
|
+
makeCancelable,
|
|
379
|
+
matchedProperties,
|
|
380
|
+
mergeDeep,
|
|
381
|
+
minDecimalPlaces,
|
|
382
|
+
pageFromUri,
|
|
383
|
+
planckToUnit,
|
|
384
|
+
remToUnit,
|
|
385
|
+
removeVarFromUrlHash,
|
|
386
|
+
removedFrom,
|
|
387
|
+
rmCommas,
|
|
388
|
+
setStateWithRef,
|
|
389
|
+
shuffle,
|
|
390
|
+
snakeToCamel,
|
|
391
|
+
sortWithNull,
|
|
392
|
+
transformToBaseUnit,
|
|
393
|
+
unescape,
|
|
394
|
+
unimplemented,
|
|
395
|
+
unitToPlanck,
|
|
396
|
+
varToUrlHash,
|
|
397
|
+
withTimeout
|
|
398
|
+
};
|
|
399
|
+
/* @license Copyright 2024 w3ux authors & contributors
|
|
400
|
+
SPDX-License-Identifier: GPL-3.0-only */
|
package/package.json
ADDED
package/types.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type AnyJson = any;
|
|
2
|
+
type AnyObject = any;
|
|
3
|
+
type AnyFunction = any;
|
|
4
|
+
declare enum EvalMessages {
|
|
5
|
+
GIBBERISH = "Input is not correct. Use numbers, floats or expression (e.g. 1k, 1.3m)",
|
|
6
|
+
ZERO = "You cannot send 0 funds",
|
|
7
|
+
SUCCESS = "",
|
|
8
|
+
SYMBOL_ERROR = "Provided symbol is not correct",
|
|
9
|
+
GENERAL_ERROR = "Check your input. Something went wrong"
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { type AnyFunction, type AnyJson, type AnyObject, EvalMessages };
|
package/types.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
var EvalMessages = /* @__PURE__ */ ((EvalMessages2) => {
|
|
3
|
+
EvalMessages2["GIBBERISH"] = "Input is not correct. Use numbers, floats or expression (e.g. 1k, 1.3m)";
|
|
4
|
+
EvalMessages2["ZERO"] = "You cannot send 0 funds";
|
|
5
|
+
EvalMessages2["SUCCESS"] = "";
|
|
6
|
+
EvalMessages2["SYMBOL_ERROR"] = "Provided symbol is not correct";
|
|
7
|
+
EvalMessages2["GENERAL_ERROR"] = "Check your input. Something went wrong";
|
|
8
|
+
return EvalMessages2;
|
|
9
|
+
})(EvalMessages || {});
|
|
10
|
+
export {
|
|
11
|
+
EvalMessages
|
|
12
|
+
};
|
|
13
|
+
/* @license Copyright 2024 w3ux authors & contributors
|
|
14
|
+
SPDX-License-Identifier: GPL-3.0-only */
|
package/unit.d.ts
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { BigNumber } from 'bignumber.js';
|
|
2
|
+
import { MutableRefObject, RefObject } from 'react';
|
|
3
|
+
import { AnyJson, AnyObject } from './types.js';
|
|
4
|
+
|
|
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 in BigNumber planck to a decimal value in token unit. (1 token
|
|
15
|
+
* = 10^units planck).
|
|
16
|
+
*/
|
|
17
|
+
declare const planckToUnit: (val: BigNumber, units: number) => BigNumber;
|
|
18
|
+
/**
|
|
19
|
+
* @name unitToPlanck
|
|
20
|
+
* @summary Convert the token unit to planck.
|
|
21
|
+
* @description
|
|
22
|
+
* Converts a balance in token unit to an equivalent value in planck by applying the chain decimals
|
|
23
|
+
* point. (1 token = 10^units planck).
|
|
24
|
+
*/
|
|
25
|
+
declare const unitToPlanck: (val: string, units: number) => BigNumber;
|
|
26
|
+
/**
|
|
27
|
+
* @name capitalizeFirstLetter
|
|
28
|
+
* @summary Capitalize the first letter of a string.
|
|
29
|
+
*/
|
|
30
|
+
declare const capitalizeFirstLetter: (string: string) => string;
|
|
31
|
+
/**
|
|
32
|
+
* @name snakeToCamel
|
|
33
|
+
* @summary converts a string from snake / kebab-case to camel-case.
|
|
34
|
+
*/
|
|
35
|
+
declare const snakeToCamel: (str: string) => string;
|
|
36
|
+
/**
|
|
37
|
+
* @name setStateWithRef
|
|
38
|
+
* @summary Synchronize React state and its reference with the provided value.
|
|
39
|
+
*/
|
|
40
|
+
declare const setStateWithRef: <T>(value: T, setState: (_state: T) => void, ref: MutableRefObject<T>) => void;
|
|
41
|
+
/**
|
|
42
|
+
* @name localStorageOrDefault
|
|
43
|
+
* @summary Retrieve the local stroage value with the key, return defult value if it is not
|
|
44
|
+
* found.
|
|
45
|
+
*/
|
|
46
|
+
declare const localStorageOrDefault: <T>(key: string, _default: T, parse?: boolean) => string | T;
|
|
47
|
+
/**
|
|
48
|
+
* @name isValidAddress
|
|
49
|
+
* @summary Return whether an address is valid Substrate address.
|
|
50
|
+
*/
|
|
51
|
+
declare const isValidAddress: (address: string) => boolean;
|
|
52
|
+
/**
|
|
53
|
+
* @name determinePoolDisplay
|
|
54
|
+
* @summary A pool will be displayed with either its set metadata or its address.
|
|
55
|
+
*/
|
|
56
|
+
declare const determinePoolDisplay: (address: string, batchItem: AnyJson) => any;
|
|
57
|
+
/**
|
|
58
|
+
* @name extractUrlValue
|
|
59
|
+
* @summary Extracts a URL value from a URL string.
|
|
60
|
+
*/
|
|
61
|
+
declare const extractUrlValue: (key: string, url?: string) => string;
|
|
62
|
+
/**
|
|
63
|
+
* @name varToUrlHash
|
|
64
|
+
* @summary Puts a variable into the URL hash as a param.
|
|
65
|
+
* @description
|
|
66
|
+
* Since url variables are added to the hash and are not treated as URL params, the params are split
|
|
67
|
+
* and parsed into a `URLSearchParams`.
|
|
68
|
+
*/
|
|
69
|
+
declare const varToUrlHash: (key: string, val: string, addIfMissing: boolean) => void;
|
|
70
|
+
/**
|
|
71
|
+
* @name removeVarFromUrlHash
|
|
72
|
+
* @summary
|
|
73
|
+
* Removes a variable `key` from the URL hash if it exists. Removes dangling `?` if no URL variables
|
|
74
|
+
* exist.
|
|
75
|
+
*/
|
|
76
|
+
declare const removeVarFromUrlHash: (key: string) => void;
|
|
77
|
+
/**
|
|
78
|
+
* @name sortWithNull
|
|
79
|
+
* @summary Sorts an array with nulls last.
|
|
80
|
+
*/
|
|
81
|
+
declare const sortWithNull: (ascending: boolean) => (a: AnyJson, b: AnyJson) => 0 | 1 | -1;
|
|
82
|
+
/**
|
|
83
|
+
* @name applyWidthAsPadding
|
|
84
|
+
* @summary Applies width of subject to paddingRight of container.
|
|
85
|
+
*/
|
|
86
|
+
declare const applyWidthAsPadding: (subjectRef: RefObject<HTMLDivElement>, containerRef: RefObject<HTMLDivElement>) => void;
|
|
87
|
+
/**
|
|
88
|
+
* @name unescape
|
|
89
|
+
* @summary Replaces \” with “
|
|
90
|
+
*/
|
|
91
|
+
declare const unescape: (val: string) => string;
|
|
92
|
+
/**
|
|
93
|
+
* @name inChrome
|
|
94
|
+
* @summary Whether the application is rendering in Chrome.
|
|
95
|
+
*/
|
|
96
|
+
declare const inChrome: () => boolean;
|
|
97
|
+
/**
|
|
98
|
+
* @name addedTo
|
|
99
|
+
* @summary Given 2 objects and some keys, return items in the fresh object that do not exist in the
|
|
100
|
+
* stale object by matching the given common key values of both objects.
|
|
101
|
+
*/
|
|
102
|
+
declare const addedTo: (fresh: AnyObject[], stale: AnyObject[], keys: string[]) => AnyObject[];
|
|
103
|
+
/**
|
|
104
|
+
* @name removedFrom
|
|
105
|
+
* @summary Given 2 objects and some keys, return items in the stale object that do not exist in the
|
|
106
|
+
* fresh object by matching the given common key values of both objects.
|
|
107
|
+
*/
|
|
108
|
+
declare const removedFrom: (fresh: AnyObject[], stale: AnyObject[], keys: string[]) => AnyObject[];
|
|
109
|
+
/**
|
|
110
|
+
* @name matchedProperties
|
|
111
|
+
* @summary Given 2 objects and some keys, return items in object 1 that also exist in object 2 by
|
|
112
|
+
* matching the given common key values of both objects.
|
|
113
|
+
*/
|
|
114
|
+
declare const matchedProperties: (objX: AnyObject[], objY: AnyObject[], keys: string[]) => AnyObject[];
|
|
115
|
+
/**
|
|
116
|
+
* @name isValidHttpUrl
|
|
117
|
+
* @summary Give a string, return whether it is a valid http URL.
|
|
118
|
+
* @param string - The string to check.
|
|
119
|
+
*/
|
|
120
|
+
declare const isValidHttpUrl: (string: string) => boolean;
|
|
121
|
+
/**
|
|
122
|
+
* @name makeCancelable
|
|
123
|
+
* @summary Makes a promise cancellable.
|
|
124
|
+
* @param promise - The promise to make cancellable.
|
|
125
|
+
*/
|
|
126
|
+
declare const makeCancelable: (promise: Promise<AnyObject>) => {
|
|
127
|
+
promise: Promise<unknown>;
|
|
128
|
+
cancel: () => void;
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* A function that identifes integer/float(comma or dot)/expressions (such as 1k)
|
|
132
|
+
* and converts to actual value (or reports an error).
|
|
133
|
+
* @param {string} input
|
|
134
|
+
* @returns {[number | null, string]} an array of 2 items
|
|
135
|
+
* the first is the actual calculated number (or null if none) while
|
|
136
|
+
* the second is the message that should appear in case of error
|
|
137
|
+
*/
|
|
138
|
+
declare const evalUnits: (input: string, chainDecimals: number) => [BigNumber | null, string];
|
|
139
|
+
/**
|
|
140
|
+
* The transformToBaseUnit function is used to transform a given estimated
|
|
141
|
+
* fee value from its current representation to its base unit representation,
|
|
142
|
+
* considering the provided chain decimals. The function is designed to handle
|
|
143
|
+
* cases where the chain decimals are either greater or less than the length
|
|
144
|
+
* of the estimated fee.
|
|
145
|
+
* @param {string} estFee : The estimated fee value that needs to be transformed
|
|
146
|
+
* to its base unit representation.
|
|
147
|
+
* @param {number} chainDecimals: The number of decimal places used by the blockchain.
|
|
148
|
+
*/
|
|
149
|
+
declare const transformToBaseUnit: (estFee: string, chainDecimals: number) => string;
|
|
150
|
+
/**
|
|
151
|
+
* @name unimplemented
|
|
152
|
+
* @summary A placeholder function to signal a deliberate unimplementation.
|
|
153
|
+
* Consumes an arbitrary number of props.
|
|
154
|
+
*/
|
|
155
|
+
declare const unimplemented: ({ ...props }: {
|
|
156
|
+
[x: string]: any;
|
|
157
|
+
}) => void;
|
|
158
|
+
/**
|
|
159
|
+
* Deep merge two objects.
|
|
160
|
+
* @param target
|
|
161
|
+
* @param ...sources
|
|
162
|
+
*/
|
|
163
|
+
declare const mergeDeep: (target: AnyObject, ...sources: AnyObject[]) => AnyObject;
|
|
164
|
+
|
|
165
|
+
export { addedTo, applyWidthAsPadding, capitalizeFirstLetter, determinePoolDisplay, evalUnits, extractUrlValue, inChrome, isValidAddress, isValidHttpUrl, localStorageOrDefault, makeCancelable, matchedProperties, mergeDeep, planckToUnit, remToUnit, removeVarFromUrlHash, removedFrom, setStateWithRef, snakeToCamel, sortWithNull, transformToBaseUnit, unescape, unimplemented, unitToPlanck, varToUrlHash };
|
package/unit.js
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
// src/unit.ts
|
|
2
|
+
import { decodeAddress, encodeAddress } from "@polkadot/keyring";
|
|
3
|
+
import { hexToU8a, isHex, u8aToString, u8aUnwrapBytes } from "@polkadot/util";
|
|
4
|
+
import { BigNumber as BigNumber2 } from "bignumber.js";
|
|
5
|
+
|
|
6
|
+
// src/base.ts
|
|
7
|
+
import { BigNumber } from "bignumber.js";
|
|
8
|
+
var ellipsisFn = (str, amount = 6, position = "center") => {
|
|
9
|
+
const half = str.length / 2;
|
|
10
|
+
if (amount <= 4) {
|
|
11
|
+
if (position === "center") {
|
|
12
|
+
return str.slice(0, 4) + "..." + str.slice(-4);
|
|
13
|
+
}
|
|
14
|
+
if (position === "end") {
|
|
15
|
+
return str.slice(0, 4) + "...";
|
|
16
|
+
}
|
|
17
|
+
return "..." + str.slice(-4);
|
|
18
|
+
}
|
|
19
|
+
if (position === "center") {
|
|
20
|
+
return amount >= (str.length - 2) / 2 ? str.slice(0, half - 3) + "..." + str.slice(-(half - 3)) : str.slice(0, amount) + "..." + str.slice(-amount);
|
|
21
|
+
}
|
|
22
|
+
if (amount >= str.length) {
|
|
23
|
+
if (position === "end") {
|
|
24
|
+
return str.slice(0, str.length - 3) + "...";
|
|
25
|
+
}
|
|
26
|
+
return "..." + str.slice(-(str.length - 3));
|
|
27
|
+
} else {
|
|
28
|
+
if (position === "end") {
|
|
29
|
+
return str.slice(0, amount) + "...";
|
|
30
|
+
}
|
|
31
|
+
return "..." + str.slice(amount);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// src/unit.ts
|
|
36
|
+
var remToUnit = (rem) => Number(rem.slice(0, rem.length - 3)) * parseFloat(getComputedStyle(document.documentElement).fontSize);
|
|
37
|
+
var planckToUnit = (val, units) => new BigNumber2(
|
|
38
|
+
val.dividedBy(new BigNumber2(10).exponentiatedBy(units)).toFixed(units)
|
|
39
|
+
);
|
|
40
|
+
var unitToPlanck = (val, units) => {
|
|
41
|
+
const init = new BigNumber2(!val.length || !val ? "0" : val);
|
|
42
|
+
return (!init.isNaN() ? init : new BigNumber2(0)).multipliedBy(new BigNumber2(10).exponentiatedBy(units)).integerValue();
|
|
43
|
+
};
|
|
44
|
+
var capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1);
|
|
45
|
+
var snakeToCamel = (str) => str.toLowerCase().replace(
|
|
46
|
+
/([-_][a-z])/g,
|
|
47
|
+
(group) => group.toUpperCase().replace("-", "").replace("_", "")
|
|
48
|
+
);
|
|
49
|
+
var setStateWithRef = (value, setState, ref) => {
|
|
50
|
+
setState(value);
|
|
51
|
+
ref.current = value;
|
|
52
|
+
};
|
|
53
|
+
var localStorageOrDefault = (key, _default, parse = false) => {
|
|
54
|
+
const val = localStorage.getItem(key);
|
|
55
|
+
if (val === null) {
|
|
56
|
+
return _default;
|
|
57
|
+
}
|
|
58
|
+
if (parse) {
|
|
59
|
+
return JSON.parse(val);
|
|
60
|
+
}
|
|
61
|
+
return val;
|
|
62
|
+
};
|
|
63
|
+
var isValidAddress = (address) => {
|
|
64
|
+
try {
|
|
65
|
+
encodeAddress(isHex(address) ? hexToU8a(address) : decodeAddress(address));
|
|
66
|
+
return true;
|
|
67
|
+
} catch (e) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
var determinePoolDisplay = (address, batchItem) => {
|
|
72
|
+
const defaultDisplay = ellipsisFn(address, 6);
|
|
73
|
+
let display = batchItem ?? defaultDisplay;
|
|
74
|
+
const displayAsBytes = u8aToString(u8aUnwrapBytes(display));
|
|
75
|
+
if (displayAsBytes !== "") {
|
|
76
|
+
display = displayAsBytes;
|
|
77
|
+
}
|
|
78
|
+
if (display === "") {
|
|
79
|
+
display = defaultDisplay;
|
|
80
|
+
}
|
|
81
|
+
return display;
|
|
82
|
+
};
|
|
83
|
+
var extractUrlValue = (key, url) => {
|
|
84
|
+
if (typeof url === "undefined") {
|
|
85
|
+
url = window.location.href;
|
|
86
|
+
}
|
|
87
|
+
const match = url.match(`[?&]${key}=([^&]+)`);
|
|
88
|
+
return match ? match[1] : null;
|
|
89
|
+
};
|
|
90
|
+
var varToUrlHash = (key, val, addIfMissing) => {
|
|
91
|
+
const hash = window.location.hash;
|
|
92
|
+
const [page, params] = hash.split("?");
|
|
93
|
+
const searchParams = new URLSearchParams(params);
|
|
94
|
+
if (searchParams.get(key) === null && !addIfMissing) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
searchParams.set(key, val);
|
|
98
|
+
window.location.hash = `${page}?${searchParams.toString()}`;
|
|
99
|
+
};
|
|
100
|
+
var removeVarFromUrlHash = (key) => {
|
|
101
|
+
const hash = window.location.hash;
|
|
102
|
+
const [page, params] = hash.split("?");
|
|
103
|
+
const searchParams = new URLSearchParams(params);
|
|
104
|
+
if (searchParams.get(key) === null) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
searchParams.delete(key);
|
|
108
|
+
const paramsAsStr = searchParams.toString();
|
|
109
|
+
window.location.hash = `${page}${paramsAsStr ? `?${paramsAsStr}` : ``}`;
|
|
110
|
+
};
|
|
111
|
+
var sortWithNull = (ascending) => (a, b) => {
|
|
112
|
+
if (a === b) {
|
|
113
|
+
return 0;
|
|
114
|
+
}
|
|
115
|
+
if (a === null) {
|
|
116
|
+
return 1;
|
|
117
|
+
}
|
|
118
|
+
if (b === null) {
|
|
119
|
+
return -1;
|
|
120
|
+
}
|
|
121
|
+
if (ascending) {
|
|
122
|
+
return a < b ? -1 : 1;
|
|
123
|
+
}
|
|
124
|
+
return a < b ? 1 : -1;
|
|
125
|
+
};
|
|
126
|
+
var applyWidthAsPadding = (subjectRef, containerRef) => {
|
|
127
|
+
if (containerRef.current && subjectRef.current) {
|
|
128
|
+
containerRef.current.style.paddingRight = `${subjectRef.current.offsetWidth + remToUnit("1rem")}px`;
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
var unescape = (val) => val.replace(/\\"/g, '"');
|
|
132
|
+
var inChrome = () => {
|
|
133
|
+
const isChromium = window?.chrome || null;
|
|
134
|
+
const winNav = window?.navigator || null;
|
|
135
|
+
const isOpera = typeof window?.opr !== "undefined";
|
|
136
|
+
const isIEedge = winNav?.userAgent.indexOf("Edg") > -1 || false;
|
|
137
|
+
const isIOSChrome = winNav?.userAgent.match("CriOS") || false;
|
|
138
|
+
if (isIOSChrome) {
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
if (isChromium !== null && typeof isChromium !== "undefined" && isOpera === false && isIEedge === false) {
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
return false;
|
|
145
|
+
};
|
|
146
|
+
var addedTo = (fresh, stale, keys) => typeof fresh !== "object" || typeof stale !== "object" || !keys.length ? [] : fresh.filter(
|
|
147
|
+
(freshItem) => !stale.find(
|
|
148
|
+
(staleItem) => keys.every(
|
|
149
|
+
(key) => !(key in staleItem) || !(key in freshItem) ? false : staleItem[key] === freshItem[key]
|
|
150
|
+
)
|
|
151
|
+
)
|
|
152
|
+
);
|
|
153
|
+
var removedFrom = (fresh, stale, keys) => typeof fresh !== "object" || typeof stale !== "object" || !keys.length ? [] : stale.filter(
|
|
154
|
+
(staleItem) => !fresh.find(
|
|
155
|
+
(freshItem) => keys.every(
|
|
156
|
+
(key) => !(key in staleItem) || !(key in freshItem) ? false : freshItem[key] === staleItem[key]
|
|
157
|
+
)
|
|
158
|
+
)
|
|
159
|
+
);
|
|
160
|
+
var matchedProperties = (objX, objY, keys) => typeof objX !== "object" || typeof objY !== "object" || !keys.length ? [] : objY.filter(
|
|
161
|
+
(x) => objX.find(
|
|
162
|
+
(y) => keys.every(
|
|
163
|
+
(key) => !(key in x) || !(key in y) ? false : y[key] === x[key]
|
|
164
|
+
)
|
|
165
|
+
)
|
|
166
|
+
);
|
|
167
|
+
var isValidHttpUrl = (string) => {
|
|
168
|
+
let url;
|
|
169
|
+
try {
|
|
170
|
+
url = new URL(string);
|
|
171
|
+
} catch (_) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
return url.protocol === "http:" || url.protocol === "https:";
|
|
175
|
+
};
|
|
176
|
+
var makeCancelable = (promise) => {
|
|
177
|
+
let hasCanceled = false;
|
|
178
|
+
const wrappedPromise = new Promise((resolve, reject) => {
|
|
179
|
+
promise.then(
|
|
180
|
+
(val) => hasCanceled ? reject(Error("Cancelled")) : resolve(val)
|
|
181
|
+
);
|
|
182
|
+
promise.catch(
|
|
183
|
+
(error) => hasCanceled ? reject(Error("Cancelled")) : reject(error)
|
|
184
|
+
);
|
|
185
|
+
});
|
|
186
|
+
return {
|
|
187
|
+
promise: wrappedPromise,
|
|
188
|
+
cancel: () => {
|
|
189
|
+
hasCanceled = true;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
};
|
|
193
|
+
var getSiValue = (si2) => new BigNumber2(10).pow(new BigNumber2(si2));
|
|
194
|
+
var si = [
|
|
195
|
+
{ value: getSiValue(24), symbol: "y", isMil: true },
|
|
196
|
+
{ value: getSiValue(21), symbol: "z", isMil: true },
|
|
197
|
+
{ value: getSiValue(18), symbol: "a", isMil: true },
|
|
198
|
+
{ value: getSiValue(15), symbol: "f", isMil: true },
|
|
199
|
+
{ value: getSiValue(12), symbol: "p", isMil: true },
|
|
200
|
+
{ value: getSiValue(9), symbol: "n", isMil: true },
|
|
201
|
+
{ value: getSiValue(6), symbol: "\u03BC", isMil: true },
|
|
202
|
+
{ value: getSiValue(3), symbol: "m", isMil: true },
|
|
203
|
+
{ value: new BigNumber2(1), symbol: "" },
|
|
204
|
+
{ value: getSiValue(3), symbol: "k" },
|
|
205
|
+
{ value: getSiValue(6), symbol: "M" },
|
|
206
|
+
{ value: getSiValue(9), symbol: "G" },
|
|
207
|
+
{ value: getSiValue(12), symbol: "T" },
|
|
208
|
+
{ value: getSiValue(15), symbol: "P" },
|
|
209
|
+
{ value: getSiValue(18), symbol: "E" },
|
|
210
|
+
{ value: getSiValue(21), symbol: "Y" },
|
|
211
|
+
{ value: getSiValue(24), symbol: "Z" }
|
|
212
|
+
];
|
|
213
|
+
var allowedSymbols = si.map((s) => s.symbol).join(", ").replace(", ,", ",");
|
|
214
|
+
var floats = new RegExp("^[+]?[0-9]*[.,]{1}[0-9]*$");
|
|
215
|
+
var ints = new RegExp("^[+]?[0-9]+$");
|
|
216
|
+
var alphaFloats = new RegExp(
|
|
217
|
+
"^[+]?[0-9]*[.,]{1}[0-9]*[" + allowedSymbols + "]{1}$"
|
|
218
|
+
);
|
|
219
|
+
var alphaInts = new RegExp("^[+]?[0-9]*[" + allowedSymbols + "]{1}$");
|
|
220
|
+
var evalUnits = (input, chainDecimals) => {
|
|
221
|
+
input = input && input.replace("+", "");
|
|
222
|
+
if (!floats.test(input) && !ints.test(input) && !alphaInts.test(input) && !alphaFloats.test(input)) {
|
|
223
|
+
return [null, "Input is not correct. Use numbers, floats or expression (e.g. 1k, 1.3m)" /* GIBBERISH */];
|
|
224
|
+
}
|
|
225
|
+
const symbol = input.replace(/[0-9.,]/g, "");
|
|
226
|
+
const siVal = si.find((s) => s.symbol === symbol);
|
|
227
|
+
const numberStr = input.replace(symbol, "").replace(",", ".");
|
|
228
|
+
let numeric = new BigNumber2(0);
|
|
229
|
+
if (!siVal) {
|
|
230
|
+
return [null, "Provided symbol is not correct" /* SYMBOL_ERROR */];
|
|
231
|
+
}
|
|
232
|
+
const decimalsBn = new BigNumber2(10).pow(new BigNumber2(chainDecimals));
|
|
233
|
+
const containDecimal = numberStr.includes(".");
|
|
234
|
+
const [decPart, fracPart] = numberStr.split(".");
|
|
235
|
+
const fracDecimals = fracPart?.length || 0;
|
|
236
|
+
const fracExp = new BigNumber2(10).pow(new BigNumber2(fracDecimals));
|
|
237
|
+
numeric = containDecimal ? new BigNumber2(
|
|
238
|
+
new BigNumber2(decPart).multipliedBy(fracExp).plus(new BigNumber2(fracPart))
|
|
239
|
+
) : new BigNumber2(new BigNumber2(numberStr));
|
|
240
|
+
numeric = numeric.multipliedBy(decimalsBn);
|
|
241
|
+
if (containDecimal) {
|
|
242
|
+
numeric = siVal.isMil ? numeric.dividedBy(siVal.value).dividedBy(fracExp) : numeric.multipliedBy(siVal.value).dividedBy(fracExp);
|
|
243
|
+
} else {
|
|
244
|
+
numeric = siVal.isMil ? numeric.dividedBy(siVal.value) : numeric.multipliedBy(siVal.value);
|
|
245
|
+
}
|
|
246
|
+
if (numeric.eq(new BigNumber2(0))) {
|
|
247
|
+
return [null, "You cannot send 0 funds" /* ZERO */];
|
|
248
|
+
}
|
|
249
|
+
return [numeric, "" /* SUCCESS */];
|
|
250
|
+
};
|
|
251
|
+
var transformToBaseUnit = (estFee, chainDecimals) => {
|
|
252
|
+
const t = estFee.length - chainDecimals;
|
|
253
|
+
let s = "";
|
|
254
|
+
if (t < 0) {
|
|
255
|
+
for (let i = 0; i < Math.abs(t) - 1; i++) {
|
|
256
|
+
s += "0";
|
|
257
|
+
}
|
|
258
|
+
s = s + estFee;
|
|
259
|
+
for (let i = 0; i < s.length; i++) {
|
|
260
|
+
if (s.slice(s.length - 1) !== "0") {
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
s = s.substring(0, s.length - 1);
|
|
264
|
+
}
|
|
265
|
+
s = "0." + s;
|
|
266
|
+
} else {
|
|
267
|
+
s = (parseInt(estFee) / 10 ** chainDecimals).toString();
|
|
268
|
+
}
|
|
269
|
+
return parseFloat(s) !== 0 ? s : "0";
|
|
270
|
+
};
|
|
271
|
+
var unimplemented = ({ ...props }) => {
|
|
272
|
+
};
|
|
273
|
+
var mergeDeep = (target, ...sources) => {
|
|
274
|
+
if (!sources.length) {
|
|
275
|
+
return target;
|
|
276
|
+
}
|
|
277
|
+
const isObject = (item) => item && typeof item === "object" && !Array.isArray(item);
|
|
278
|
+
const source = sources.shift();
|
|
279
|
+
if (isObject(target) && isObject(source)) {
|
|
280
|
+
for (const key in source) {
|
|
281
|
+
if (isObject(source[key])) {
|
|
282
|
+
if (!target[key]) {
|
|
283
|
+
Object.assign(target, { [key]: {} });
|
|
284
|
+
}
|
|
285
|
+
mergeDeep(target[key], source[key]);
|
|
286
|
+
} else {
|
|
287
|
+
Object.assign(target, { [key]: source[key] });
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return mergeDeep(target, ...sources);
|
|
292
|
+
};
|
|
293
|
+
export {
|
|
294
|
+
addedTo,
|
|
295
|
+
applyWidthAsPadding,
|
|
296
|
+
capitalizeFirstLetter,
|
|
297
|
+
determinePoolDisplay,
|
|
298
|
+
evalUnits,
|
|
299
|
+
extractUrlValue,
|
|
300
|
+
inChrome,
|
|
301
|
+
isValidAddress,
|
|
302
|
+
isValidHttpUrl,
|
|
303
|
+
localStorageOrDefault,
|
|
304
|
+
makeCancelable,
|
|
305
|
+
matchedProperties,
|
|
306
|
+
mergeDeep,
|
|
307
|
+
planckToUnit,
|
|
308
|
+
remToUnit,
|
|
309
|
+
removeVarFromUrlHash,
|
|
310
|
+
removedFrom,
|
|
311
|
+
setStateWithRef,
|
|
312
|
+
snakeToCamel,
|
|
313
|
+
sortWithNull,
|
|
314
|
+
transformToBaseUnit,
|
|
315
|
+
unescape,
|
|
316
|
+
unimplemented,
|
|
317
|
+
unitToPlanck,
|
|
318
|
+
varToUrlHash
|
|
319
|
+
};
|
|
320
|
+
/* @license Copyright 2024 w3ux authors & contributors
|
|
321
|
+
SPDX-License-Identifier: GPL-3.0-only */
|