@w3ux/utils 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.cts +269 -5
- package/index.d.ts +269 -5
- package/package.json +1 -1
- package/base.cjs +0 -194
- package/base.d.cts +0 -114
- package/base.d.ts +0 -114
- package/base.js +0 -153
- package/convert.cjs +0 -40
- package/convert.d.cts +0 -9
- package/convert.d.ts +0 -9
- package/convert.js +0 -15
- package/types.cjs +0 -20
- package/types.d.cts +0 -3
- package/types.d.ts +0 -3
- package/types.js +0 -2
- package/unit.cjs +0 -282
- package/unit.d.cts +0 -147
- package/unit.d.ts +0 -147
- package/unit.js +0 -236
package/unit.js
DELETED
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
// src/unit.ts
|
|
2
|
-
import { decodeAddress } from "dedot/utils";
|
|
3
|
-
|
|
4
|
-
// src/base.ts
|
|
5
|
-
import { encodeAddress } from "dedot/utils";
|
|
6
|
-
var rmCommas = (val) => val.replace(/,/g, "");
|
|
7
|
-
var rmDecimals = (str) => str.split(".")[0];
|
|
8
|
-
|
|
9
|
-
// src/unit.ts
|
|
10
|
-
var planckToUnit = (val, units) => {
|
|
11
|
-
try {
|
|
12
|
-
units = Math.max(Math.round(units), 0);
|
|
13
|
-
const bigIntVal = typeof val === "bigint" ? val : BigInt(
|
|
14
|
-
typeof val === "number" ? Math.floor(val).toString() : rmDecimals(rmCommas(val))
|
|
15
|
-
);
|
|
16
|
-
const divisor = units === 0 ? 1n : BigInt(10) ** BigInt(units);
|
|
17
|
-
const integerPart = bigIntVal / divisor;
|
|
18
|
-
const fractionalPart = bigIntVal % divisor;
|
|
19
|
-
const fractionalStr = units > 0 ? `.${fractionalPart.toString().padStart(units, "0")}` : ``;
|
|
20
|
-
return `${integerPart}${fractionalStr}`;
|
|
21
|
-
} catch (e) {
|
|
22
|
-
return "0";
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
var unitToPlanck = (val, units) => {
|
|
26
|
-
try {
|
|
27
|
-
units = Math.max(Math.round(units), 0);
|
|
28
|
-
const strVal = (typeof val === "string" ? rmCommas(val) : val.toString()) || "0";
|
|
29
|
-
const [integerPart, fractionalPart = ""] = strVal.split(".");
|
|
30
|
-
let bigIntValue = BigInt(integerPart) * BigInt(10) ** BigInt(units);
|
|
31
|
-
if (fractionalPart) {
|
|
32
|
-
let fractionalValue;
|
|
33
|
-
if (fractionalPart.length > units) {
|
|
34
|
-
fractionalValue = BigInt(fractionalPart.slice(0, units));
|
|
35
|
-
} else {
|
|
36
|
-
fractionalValue = BigInt(fractionalPart.padEnd(units, "0"));
|
|
37
|
-
}
|
|
38
|
-
bigIntValue += fractionalValue;
|
|
39
|
-
}
|
|
40
|
-
return bigIntValue;
|
|
41
|
-
} catch (e) {
|
|
42
|
-
return BigInt(0);
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
var remToUnit = (rem) => Number(rem.slice(0, rem.length - 3)) * parseFloat(getComputedStyle(document.documentElement).fontSize);
|
|
46
|
-
var capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1);
|
|
47
|
-
var snakeToCamel = (str) => str.toLowerCase().replace(
|
|
48
|
-
/([-_][a-z])/g,
|
|
49
|
-
(group) => group.toUpperCase().replace("-", "").replace("_", "")
|
|
50
|
-
);
|
|
51
|
-
var setStateWithRef = (value, setState, ref) => {
|
|
52
|
-
setState(value);
|
|
53
|
-
ref.current = value;
|
|
54
|
-
};
|
|
55
|
-
var localStorageOrDefault = (key, _default, parse = false) => {
|
|
56
|
-
const val = localStorage.getItem(key);
|
|
57
|
-
if (val === null) {
|
|
58
|
-
return _default;
|
|
59
|
-
}
|
|
60
|
-
if (parse) {
|
|
61
|
-
return JSON.parse(val);
|
|
62
|
-
}
|
|
63
|
-
return val;
|
|
64
|
-
};
|
|
65
|
-
var isValidAddress = (address) => {
|
|
66
|
-
try {
|
|
67
|
-
decodeAddress(address);
|
|
68
|
-
return true;
|
|
69
|
-
} catch (e) {
|
|
70
|
-
return false;
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
var extractUrlValue = (key, url) => {
|
|
74
|
-
if (typeof url === "undefined") {
|
|
75
|
-
url = window.location.href;
|
|
76
|
-
}
|
|
77
|
-
const match = url.match(`[?&]${key}=([^&]+)`);
|
|
78
|
-
return match ? match[1] : null;
|
|
79
|
-
};
|
|
80
|
-
var varToUrlHash = (key, val, addIfMissing) => {
|
|
81
|
-
const hash = window.location.hash;
|
|
82
|
-
const [page, params] = hash.split("?");
|
|
83
|
-
const searchParams = new URLSearchParams(params);
|
|
84
|
-
if (searchParams.get(key) === null && !addIfMissing) {
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
searchParams.set(key, val);
|
|
88
|
-
window.location.hash = `${page}?${searchParams.toString()}`;
|
|
89
|
-
};
|
|
90
|
-
var removeVarFromUrlHash = (key) => {
|
|
91
|
-
const hash = window.location.hash;
|
|
92
|
-
const [page, params] = hash.split("?");
|
|
93
|
-
const searchParams = new URLSearchParams(params);
|
|
94
|
-
if (searchParams.get(key) === null) {
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
searchParams.delete(key);
|
|
98
|
-
const paramsAsStr = searchParams.toString();
|
|
99
|
-
window.location.hash = `${page}${paramsAsStr ? `?${paramsAsStr}` : ``}`;
|
|
100
|
-
};
|
|
101
|
-
var sortWithNull = (ascending) => (a, b) => {
|
|
102
|
-
if (typeof a === "undefined" && typeof b === "undefined") {
|
|
103
|
-
return 0;
|
|
104
|
-
}
|
|
105
|
-
if (typeof a === "undefined" || typeof b === "undefined") {
|
|
106
|
-
return typeof a === "undefined" ? 1 : -1;
|
|
107
|
-
}
|
|
108
|
-
if (a === b) {
|
|
109
|
-
return 0;
|
|
110
|
-
}
|
|
111
|
-
if (a === null) {
|
|
112
|
-
return 1;
|
|
113
|
-
}
|
|
114
|
-
if (b === null) {
|
|
115
|
-
return -1;
|
|
116
|
-
}
|
|
117
|
-
if (ascending) {
|
|
118
|
-
return a < b ? -1 : 1;
|
|
119
|
-
}
|
|
120
|
-
return a < b ? 1 : -1;
|
|
121
|
-
};
|
|
122
|
-
var applyWidthAsPadding = (subjectRef, containerRef) => {
|
|
123
|
-
if (containerRef.current && subjectRef.current) {
|
|
124
|
-
containerRef.current.style.paddingRight = `${subjectRef.current.offsetWidth + remToUnit("1rem")}px`;
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
var unescape = (val) => val.replace(/\\"/g, '"');
|
|
128
|
-
var inChrome = () => {
|
|
129
|
-
const isChromium = window?.chrome || null;
|
|
130
|
-
const winNav = window?.navigator || null;
|
|
131
|
-
const isOpera = typeof window?.opr !== "undefined";
|
|
132
|
-
const isIEedge = winNav?.userAgent.indexOf("Edg") > -1 || false;
|
|
133
|
-
const isIOSChrome = winNav?.userAgent.match("CriOS") || false;
|
|
134
|
-
if (isIOSChrome) {
|
|
135
|
-
return true;
|
|
136
|
-
}
|
|
137
|
-
if (isChromium !== null && typeof isChromium !== "undefined" && isOpera === false && isIEedge === false) {
|
|
138
|
-
return true;
|
|
139
|
-
}
|
|
140
|
-
return false;
|
|
141
|
-
};
|
|
142
|
-
var addedTo = (fresh, stale, keys) => typeof fresh !== "object" || typeof stale !== "object" || !keys.length ? [] : fresh.filter(
|
|
143
|
-
(freshItem) => !stale.find(
|
|
144
|
-
(staleItem) => keys.every(
|
|
145
|
-
(key) => !(key in staleItem) || !(key in freshItem) ? false : staleItem[key] === freshItem[key]
|
|
146
|
-
)
|
|
147
|
-
)
|
|
148
|
-
);
|
|
149
|
-
var removedFrom = (fresh, stale, keys) => typeof fresh !== "object" || typeof stale !== "object" || !keys.length ? [] : stale.filter(
|
|
150
|
-
(staleItem) => !fresh.find(
|
|
151
|
-
(freshItem) => keys.every(
|
|
152
|
-
(key) => !(key in staleItem) || !(key in freshItem) ? false : freshItem[key] === staleItem[key]
|
|
153
|
-
)
|
|
154
|
-
)
|
|
155
|
-
);
|
|
156
|
-
var matchedProperties = (objX, objY, keys) => typeof objX !== "object" || typeof objY !== "object" || !keys.length ? [] : objY.filter(
|
|
157
|
-
(x) => objX.find(
|
|
158
|
-
(y) => keys.every(
|
|
159
|
-
(key) => !(key in x) || !(key in y) ? false : y[key] === x[key]
|
|
160
|
-
)
|
|
161
|
-
)
|
|
162
|
-
);
|
|
163
|
-
var isValidHttpUrl = (string) => {
|
|
164
|
-
let url;
|
|
165
|
-
try {
|
|
166
|
-
url = new URL(string);
|
|
167
|
-
} catch (_) {
|
|
168
|
-
return false;
|
|
169
|
-
}
|
|
170
|
-
return url.protocol === "http:" || url.protocol === "https:";
|
|
171
|
-
};
|
|
172
|
-
var makeCancelable = (promise) => {
|
|
173
|
-
let hasCanceled = false;
|
|
174
|
-
const wrappedPromise = new Promise((resolve, reject) => {
|
|
175
|
-
promise.then(
|
|
176
|
-
(val) => hasCanceled ? reject(Error("Cancelled")) : resolve(val)
|
|
177
|
-
);
|
|
178
|
-
promise.catch(
|
|
179
|
-
(error) => hasCanceled ? reject(Error("Cancelled")) : reject(error)
|
|
180
|
-
);
|
|
181
|
-
});
|
|
182
|
-
return {
|
|
183
|
-
promise: wrappedPromise,
|
|
184
|
-
cancel: () => {
|
|
185
|
-
hasCanceled = true;
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
};
|
|
189
|
-
var unimplemented = ({ ...props }) => {
|
|
190
|
-
};
|
|
191
|
-
var mergeDeep = (target, ...sources) => {
|
|
192
|
-
if (!sources.length) {
|
|
193
|
-
return target;
|
|
194
|
-
}
|
|
195
|
-
const isObject = (item) => item && typeof item === "object" && !Array.isArray(item);
|
|
196
|
-
const source = sources.shift();
|
|
197
|
-
if (isObject(target) && isObject(source)) {
|
|
198
|
-
for (const key in source) {
|
|
199
|
-
if (isObject(source[key])) {
|
|
200
|
-
if (!target[key]) {
|
|
201
|
-
Object.assign(target, { [key]: {} });
|
|
202
|
-
}
|
|
203
|
-
mergeDeep(target[key], source[key]);
|
|
204
|
-
} else {
|
|
205
|
-
Object.assign(target, { [key]: source[key] });
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
return mergeDeep(target, ...sources);
|
|
210
|
-
};
|
|
211
|
-
export {
|
|
212
|
-
addedTo,
|
|
213
|
-
applyWidthAsPadding,
|
|
214
|
-
capitalizeFirstLetter,
|
|
215
|
-
extractUrlValue,
|
|
216
|
-
inChrome,
|
|
217
|
-
isValidAddress,
|
|
218
|
-
isValidHttpUrl,
|
|
219
|
-
localStorageOrDefault,
|
|
220
|
-
makeCancelable,
|
|
221
|
-
matchedProperties,
|
|
222
|
-
mergeDeep,
|
|
223
|
-
planckToUnit,
|
|
224
|
-
remToUnit,
|
|
225
|
-
removeVarFromUrlHash,
|
|
226
|
-
removedFrom,
|
|
227
|
-
setStateWithRef,
|
|
228
|
-
snakeToCamel,
|
|
229
|
-
sortWithNull,
|
|
230
|
-
unescape,
|
|
231
|
-
unimplemented,
|
|
232
|
-
unitToPlanck,
|
|
233
|
-
varToUrlHash
|
|
234
|
-
};
|
|
235
|
-
/* @license Copyright 2024 w3ux authors & contributors
|
|
236
|
-
SPDX-License-Identifier: GPL-3.0-only */
|