@w3ux/utils 0.2.0 → 0.4.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 +176 -0
- package/base.d.cts +77 -0
- package/base.d.ts +1 -1
- package/index.cjs +493 -0
- package/index.d.cts +6 -0
- package/index.d.ts +2 -1
- package/package.json +9 -2
- package/types.cjs +38 -0
- package/types.d.cts +10 -0
- package/types.d.ts +1 -3
- package/unit.cjs +384 -0
- package/unit.d.cts +171 -0
- package/unit.d.ts +3 -2
package/base.cjs
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/base.ts
|
|
30
|
+
var base_exports = {};
|
|
31
|
+
__export(base_exports, {
|
|
32
|
+
appendOr: () => appendOr,
|
|
33
|
+
appendOrEmpty: () => appendOrEmpty,
|
|
34
|
+
camelize: () => camelize,
|
|
35
|
+
ellipsisFn: () => ellipsisFn,
|
|
36
|
+
formatAccountSs58: () => formatAccountSs58,
|
|
37
|
+
greaterThanZero: () => greaterThanZero,
|
|
38
|
+
isNotZero: () => isNotZero,
|
|
39
|
+
minDecimalPlaces: () => minDecimalPlaces,
|
|
40
|
+
pageFromUri: () => pageFromUri,
|
|
41
|
+
removeHexPrefix: () => removeHexPrefix,
|
|
42
|
+
rmCommas: () => rmCommas,
|
|
43
|
+
shuffle: () => shuffle,
|
|
44
|
+
withTimeout: () => withTimeout
|
|
45
|
+
});
|
|
46
|
+
module.exports = __toCommonJS(base_exports);
|
|
47
|
+
var import_bignumber = require("bignumber.js");
|
|
48
|
+
var import_keyring = __toESM(require("@polkadot/keyring"), 1);
|
|
49
|
+
var camelize = (str) => {
|
|
50
|
+
const convertToString = (string) => {
|
|
51
|
+
if (string) {
|
|
52
|
+
if (typeof string === "string") {
|
|
53
|
+
return string;
|
|
54
|
+
}
|
|
55
|
+
return String(string);
|
|
56
|
+
}
|
|
57
|
+
return "";
|
|
58
|
+
};
|
|
59
|
+
const toWords = (inp) => convertToString(inp).match(
|
|
60
|
+
/[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
|
|
61
|
+
);
|
|
62
|
+
const simpleCamelCase = (inp) => {
|
|
63
|
+
let result = "";
|
|
64
|
+
for (let i = 0; i < inp?.length; i++) {
|
|
65
|
+
const currString = inp[i];
|
|
66
|
+
let tmpStr = currString.toLowerCase();
|
|
67
|
+
if (i != 0) {
|
|
68
|
+
tmpStr = tmpStr.slice(0, 1).toUpperCase() + tmpStr.slice(1, tmpStr.length);
|
|
69
|
+
}
|
|
70
|
+
result += tmpStr;
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
73
|
+
};
|
|
74
|
+
const w = toWords(str)?.map((a) => a.toLowerCase());
|
|
75
|
+
return simpleCamelCase(w);
|
|
76
|
+
};
|
|
77
|
+
var ellipsisFn = (str, amount = 6, position = "center") => {
|
|
78
|
+
const half = str.length / 2;
|
|
79
|
+
if (amount <= 4) {
|
|
80
|
+
if (position === "center") {
|
|
81
|
+
return str.slice(0, 4) + "..." + str.slice(-4);
|
|
82
|
+
}
|
|
83
|
+
if (position === "end") {
|
|
84
|
+
return str.slice(0, 4) + "...";
|
|
85
|
+
}
|
|
86
|
+
return "..." + str.slice(-4);
|
|
87
|
+
}
|
|
88
|
+
if (position === "center") {
|
|
89
|
+
return amount >= (str.length - 2) / 2 ? str.slice(0, half - 3) + "..." + str.slice(-(half - 3)) : str.slice(0, amount) + "..." + str.slice(-amount);
|
|
90
|
+
}
|
|
91
|
+
if (amount >= str.length) {
|
|
92
|
+
if (position === "end") {
|
|
93
|
+
return str.slice(0, str.length - 3) + "...";
|
|
94
|
+
}
|
|
95
|
+
return "..." + str.slice(-(str.length - 3));
|
|
96
|
+
} else {
|
|
97
|
+
if (position === "end") {
|
|
98
|
+
return str.slice(0, amount) + "...";
|
|
99
|
+
}
|
|
100
|
+
return "..." + str.slice(amount);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
var greaterThanZero = (val) => val.isGreaterThan(0);
|
|
104
|
+
var isNotZero = (val) => !val.isZero();
|
|
105
|
+
var minDecimalPlaces = (val, minDecimals) => {
|
|
106
|
+
const whole = new import_bignumber.BigNumber(rmCommas(val).split(".")[0] || 0);
|
|
107
|
+
const decimals = val.split(".")[1] || "";
|
|
108
|
+
const missingDecimals = new import_bignumber.BigNumber(minDecimals).minus(decimals.length);
|
|
109
|
+
return missingDecimals.isGreaterThan(0) ? `${whole.toFormat(0)}.${decimals.toString()}${"0".repeat(
|
|
110
|
+
missingDecimals.toNumber()
|
|
111
|
+
)}` : val;
|
|
112
|
+
};
|
|
113
|
+
var pageFromUri = (pathname, fallback) => {
|
|
114
|
+
const lastUriItem = pathname.substring(pathname.lastIndexOf("/") + 1);
|
|
115
|
+
const page = lastUriItem.trim() === "" ? fallback : lastUriItem;
|
|
116
|
+
return page.trim();
|
|
117
|
+
};
|
|
118
|
+
var rmCommas = (val) => val.replace(/,/g, "");
|
|
119
|
+
var shuffle = (array) => {
|
|
120
|
+
let currentIndex = array.length;
|
|
121
|
+
let randomIndex;
|
|
122
|
+
while (currentIndex !== 0) {
|
|
123
|
+
randomIndex = Math.floor(Math.random() * currentIndex);
|
|
124
|
+
currentIndex--;
|
|
125
|
+
[array[currentIndex], array[randomIndex]] = [
|
|
126
|
+
array[randomIndex],
|
|
127
|
+
array[currentIndex]
|
|
128
|
+
];
|
|
129
|
+
}
|
|
130
|
+
return array;
|
|
131
|
+
};
|
|
132
|
+
var withTimeout = (ms, promise, options) => {
|
|
133
|
+
const timeout = new Promise(
|
|
134
|
+
(resolve) => setTimeout(async () => {
|
|
135
|
+
if (typeof options?.onTimeout === "function") {
|
|
136
|
+
options.onTimeout();
|
|
137
|
+
}
|
|
138
|
+
resolve(void 0);
|
|
139
|
+
}, ms)
|
|
140
|
+
);
|
|
141
|
+
return Promise.race([promise, timeout]);
|
|
142
|
+
};
|
|
143
|
+
var appendOrEmpty = (condition, value) => condition ? ` ${value}` : "";
|
|
144
|
+
var appendOr = (condition, value, fallback) => condition ? ` ${value}` : ` ${fallback}`;
|
|
145
|
+
var formatAccountSs58 = (address, ss58) => {
|
|
146
|
+
try {
|
|
147
|
+
const keyring = new import_keyring.default();
|
|
148
|
+
keyring.setSS58Format(ss58);
|
|
149
|
+
const formatted = keyring.addFromAddress(address).address;
|
|
150
|
+
if (formatted !== address) {
|
|
151
|
+
return formatted;
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
} catch (e) {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
var removeHexPrefix = (str) => str.replace(/^0x/, "");
|
|
159
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
160
|
+
0 && (module.exports = {
|
|
161
|
+
appendOr,
|
|
162
|
+
appendOrEmpty,
|
|
163
|
+
camelize,
|
|
164
|
+
ellipsisFn,
|
|
165
|
+
formatAccountSs58,
|
|
166
|
+
greaterThanZero,
|
|
167
|
+
isNotZero,
|
|
168
|
+
minDecimalPlaces,
|
|
169
|
+
pageFromUri,
|
|
170
|
+
removeHexPrefix,
|
|
171
|
+
rmCommas,
|
|
172
|
+
shuffle,
|
|
173
|
+
withTimeout
|
|
174
|
+
});
|
|
175
|
+
/* @license Copyright 2024 w3ux authors & contributors
|
|
176
|
+
SPDX-License-Identifier: GPL-3.0-only */
|
package/base.d.cts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { BigNumber } from 'bignumber.js';
|
|
2
|
+
import { AnyFunction } from '@w3ux/types';
|
|
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
|
+
* @name appendOr
|
|
67
|
+
* @summary Formats an address with the supplied ss58 prefix, or returns null if invalid.
|
|
68
|
+
*/
|
|
69
|
+
declare const formatAccountSs58: (address: string, ss58: number) => string;
|
|
70
|
+
/**
|
|
71
|
+
* @name removeHexPrefix
|
|
72
|
+
* @summary Takes a string str as input and returns a new string with the "0x" prefix removed if it
|
|
73
|
+
* exists at the beginning of the input string.
|
|
74
|
+
*/
|
|
75
|
+
declare const removeHexPrefix: (str: string) => string;
|
|
76
|
+
|
|
77
|
+
export { appendOr, appendOrEmpty, camelize, ellipsisFn, formatAccountSs58, greaterThanZero, isNotZero, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, shuffle, withTimeout };
|
package/base.d.ts
CHANGED