@welshman/lib 0.0.32 → 0.0.34
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/README.md +10 -8
- package/build/src/Context.cjs +17 -0
- package/build/src/Context.cjs.map +1 -1
- package/build/src/Context.d.ts +17 -0
- package/build/src/Context.mjs +17 -0
- package/build/src/Context.mjs.map +1 -1
- package/build/src/Deferred.cjs +9 -0
- package/build/src/Deferred.cjs.map +1 -1
- package/build/src/Deferred.d.ts +11 -0
- package/build/src/Deferred.mjs +9 -0
- package/build/src/Deferred.mjs.map +1 -1
- package/build/src/Emitter.cjs +9 -0
- package/build/src/Emitter.cjs.map +1 -1
- package/build/src/Emitter.d.ts +9 -0
- package/build/src/Emitter.mjs +9 -0
- package/build/src/Emitter.mjs.map +1 -1
- package/build/src/LRUCache.cjs +16 -0
- package/build/src/LRUCache.cjs.map +1 -1
- package/build/src/LRUCache.d.ts +16 -0
- package/build/src/LRUCache.mjs +16 -0
- package/build/src/LRUCache.mjs.map +1 -1
- package/build/src/Tools.cjs +478 -12
- package/build/src/Tools.cjs.map +1 -1
- package/build/src/Tools.d.ts +465 -18
- package/build/src/Tools.mjs +471 -9
- package/build/src/Tools.mjs.map +1 -1
- package/build/src/Worker.cjs +27 -1
- package/build/src/Worker.cjs.map +1 -1
- package/build/src/Worker.d.ts +25 -0
- package/build/src/Worker.mjs +27 -1
- package/build/src/Worker.mjs.map +1 -1
- package/build/src/index.cjs +0 -1
- package/build/src/index.cjs.map +1 -1
- package/build/src/index.d.ts +0 -1
- package/build/src/index.mjs +0 -1
- package/build/src/index.mjs.map +1 -1
- package/package.json +1 -1
- package/build/src/Fluent.cjs +0 -47
- package/build/src/Fluent.cjs.map +0 -1
- package/build/src/Fluent.d.ts +0 -34
- package/build/src/Fluent.mjs +0 -43
- package/build/src/Fluent.mjs.map +0 -1
package/build/src/Tools.cjs
CHANGED
|
@@ -1,62 +1,135 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.
|
|
5
|
-
exports.bech32ToHex = exports.hexToBech32 = exports.uploadFile = exports.postJson = exports.fetchJson = exports.ms = exports.ago = exports.now = exports.int = exports.YEAR = exports.QUARTER = exports.MONTH = exports.WEEK = void 0;
|
|
3
|
+
exports.without = exports.remove = exports.difference = exports.intersection = exports.union = exports.append = exports.concat = exports.sleep = exports.displayDomain = exports.displayUrl = exports.stripProtocol = exports.randomId = exports.randomInt = exports.within = exports.between = exports.mergeRight = exports.mergeLeft = exports.mapVals = exports.mapKeys = exports.range = exports.pick = exports.omitVals = exports.omit = exports.take = exports.drop = exports.avg = exports.sum = exports.min = exports.max = exports.gte = exports.gt = exports.lte = exports.lt = exports.dec = exports.inc = exports.div = exports.mul = exports.sub = exports.add = exports.num = exports.complement = exports.not = exports.always = exports.identity = exports.last = exports.ffirst = exports.first = exports.noop = exports.ifLet = exports.isNil = void 0;
|
|
4
|
+
exports.pushToMapKey = exports.addToMapKey = exports.pushToKey = exports.addToKey = exports.batcher = exports.batch = exports.throttleWithValue = exports.throttle = exports.memoize = exports.once = exports.chunks = exports.chunk = exports.initArray = exports.indexBy = exports.groupBy = exports.sortBy = exports.sort = exports.uniqBy = exports.uniq = exports.partition = exports.flatten = exports.fromPairs = exports.pluck = exports.ensureNumber = exports.ensurePlural = exports.toIterable = exports.isIterable = exports.sample = exports.shuffle = exports.choice = exports.insert = exports.splitAt = exports.hash = exports.assoc = exports.prop = exports.ne = exports.eq = exports.spec = exports.nthNe = exports.nthEq = exports.nth = exports.equals = exports.isPojo = exports.ellipsize = exports.tryCatch = exports.setJson = exports.getJson = exports.parseJson = exports.clamp = exports.toggle = void 0;
|
|
5
|
+
exports.bech32ToHex = exports.hexToBech32 = exports.uploadFile = exports.postJson = exports.fetchJson = exports.ms = exports.ago = exports.now = exports.int = exports.YEAR = exports.QUARTER = exports.MONTH = exports.WEEK = exports.DAY = exports.HOUR = exports.MINUTE = exports.switcher = void 0;
|
|
6
6
|
const base_1 = require("@scure/base");
|
|
7
|
+
/** Checks if a value is null or undefined */
|
|
7
8
|
const isNil = (x) => [null, undefined].includes(x);
|
|
8
9
|
exports.isNil = isNil;
|
|
10
|
+
/**
|
|
11
|
+
* Executes a function if the value is defined
|
|
12
|
+
* @param x - The value to check
|
|
13
|
+
* @param f - Function to execute if x is defined
|
|
14
|
+
* @returns Result of f(x) if x is defined, undefined otherwise
|
|
15
|
+
*/
|
|
9
16
|
const ifLet = (x, f) => x === undefined ? undefined : f(x);
|
|
10
17
|
exports.ifLet = ifLet;
|
|
11
|
-
|
|
18
|
+
/** Function that does nothing and returns undefined */
|
|
12
19
|
const noop = (...args) => undefined;
|
|
13
20
|
exports.noop = noop;
|
|
21
|
+
/**
|
|
22
|
+
* Returns the first element of an array
|
|
23
|
+
* @param xs - The array
|
|
24
|
+
* @returns First element or undefined
|
|
25
|
+
*/
|
|
14
26
|
const first = (xs, ...args) => xs[0];
|
|
15
27
|
exports.first = first;
|
|
28
|
+
/**
|
|
29
|
+
* Returns the first element of the first array in a nested array
|
|
30
|
+
* @param xs - Array of arrays
|
|
31
|
+
* @returns First element of first array or undefined
|
|
32
|
+
*/
|
|
16
33
|
const ffirst = (xs, ...args) => xs[0][0];
|
|
17
34
|
exports.ffirst = ffirst;
|
|
35
|
+
/**
|
|
36
|
+
* Returns the last element of an array
|
|
37
|
+
* @param xs - The array
|
|
38
|
+
* @returns Last element or undefined
|
|
39
|
+
*/
|
|
18
40
|
const last = (xs, ...args) => xs[xs.length - 1];
|
|
19
41
|
exports.last = last;
|
|
42
|
+
/**
|
|
43
|
+
* Returns the input value unchanged
|
|
44
|
+
* @param x - Any value
|
|
45
|
+
* @returns The same value
|
|
46
|
+
*/
|
|
20
47
|
const identity = (x, ...args) => x;
|
|
21
48
|
exports.identity = identity;
|
|
49
|
+
/**
|
|
50
|
+
* Creates a function that always returns the same value
|
|
51
|
+
* @param x - Value to return
|
|
52
|
+
* @returns Function that returns x
|
|
53
|
+
*/
|
|
22
54
|
const always = (x, ...args) => () => x;
|
|
23
55
|
exports.always = always;
|
|
56
|
+
/**
|
|
57
|
+
* Returns the logical NOT of a value
|
|
58
|
+
* @param x - Value to negate
|
|
59
|
+
* @returns !x
|
|
60
|
+
*/
|
|
24
61
|
const not = (x, ...args) => !x;
|
|
25
62
|
exports.not = not;
|
|
63
|
+
/** Returns a function that returns the boolean negation of the given function */
|
|
64
|
+
const complement = (f) => (...args) => !f(...args);
|
|
65
|
+
exports.complement = complement;
|
|
66
|
+
/** Converts a Maybe<number> to a number, defaulting to 0 */
|
|
26
67
|
const num = (x) => x || 0;
|
|
27
68
|
exports.num = num;
|
|
69
|
+
/** Adds two numbers, handling undefined values */
|
|
28
70
|
const add = (x, y) => (0, exports.num)(x) + (0, exports.num)(y);
|
|
29
71
|
exports.add = add;
|
|
72
|
+
/** Subtracts two numbers, handling undefined values */
|
|
30
73
|
const sub = (x, y) => (0, exports.num)(x) - (0, exports.num)(y);
|
|
31
74
|
exports.sub = sub;
|
|
75
|
+
/** Multiplies two numbers, handling undefined values */
|
|
32
76
|
const mul = (x, y) => (0, exports.num)(x) * (0, exports.num)(y);
|
|
33
77
|
exports.mul = mul;
|
|
78
|
+
/** Divides two numbers, handling undefined values */
|
|
34
79
|
const div = (x, y) => (0, exports.num)(x) / y;
|
|
35
80
|
exports.div = div;
|
|
81
|
+
/** Increments a number by 1, handling undefined values */
|
|
36
82
|
const inc = (x) => (0, exports.add)(x, 1);
|
|
37
83
|
exports.inc = inc;
|
|
84
|
+
/** Decrements a number by 1, handling undefined values */
|
|
38
85
|
const dec = (x) => (0, exports.sub)(x, 1);
|
|
39
86
|
exports.dec = dec;
|
|
87
|
+
/** Less than comparison, handling undefined values */
|
|
40
88
|
const lt = (x, y) => (0, exports.num)(x) < (0, exports.num)(y);
|
|
41
89
|
exports.lt = lt;
|
|
90
|
+
/** Less than or equal comparison, handling undefined values */
|
|
42
91
|
const lte = (x, y) => (0, exports.num)(x) <= (0, exports.num)(y);
|
|
43
92
|
exports.lte = lte;
|
|
93
|
+
/** Greater than comparison, handling undefined values */
|
|
44
94
|
const gt = (x, y) => (0, exports.num)(x) > (0, exports.num)(y);
|
|
45
95
|
exports.gt = gt;
|
|
96
|
+
/** Greater than or equal comparison, handling undefined values */
|
|
46
97
|
const gte = (x, y) => (0, exports.num)(x) >= (0, exports.num)(y);
|
|
47
98
|
exports.gte = gte;
|
|
99
|
+
/** Returns maximum value in array, handling undefined values */
|
|
48
100
|
const max = (xs) => xs.reduce((a, b) => Math.max((0, exports.num)(a), (0, exports.num)(b)), 0);
|
|
49
101
|
exports.max = max;
|
|
102
|
+
/** Returns minimum value in array, handling undefined values */
|
|
50
103
|
const min = (xs) => xs.reduce((a, b) => Math.min((0, exports.num)(a), (0, exports.num)(b)), 0);
|
|
51
104
|
exports.min = min;
|
|
105
|
+
/** Returns sum of array values, handling undefined values */
|
|
52
106
|
const sum = (xs) => xs.reduce((a, b) => (0, exports.add)(a, b), 0);
|
|
53
107
|
exports.sum = sum;
|
|
108
|
+
/** Returns average of array values, handling undefined values */
|
|
54
109
|
const avg = (xs) => (0, exports.sum)(xs) / xs.length;
|
|
55
110
|
exports.avg = avg;
|
|
111
|
+
/**
|
|
112
|
+
* Returns array with first n elements removed
|
|
113
|
+
* @param n - Number of elements to drop
|
|
114
|
+
* @param xs - Input array
|
|
115
|
+
* @returns Array with first n elements removed
|
|
116
|
+
*/
|
|
56
117
|
const drop = (n, xs) => xs.slice(n);
|
|
57
118
|
exports.drop = drop;
|
|
119
|
+
/**
|
|
120
|
+
* Returns first n elements of array
|
|
121
|
+
* @param n - Number of elements to take
|
|
122
|
+
* @param xs - Input array
|
|
123
|
+
* @returns Array of first n elements
|
|
124
|
+
*/
|
|
58
125
|
const take = (n, xs) => xs.slice(0, n);
|
|
59
126
|
exports.take = take;
|
|
127
|
+
/**
|
|
128
|
+
* Creates new object with specified keys removed
|
|
129
|
+
* @param ks - Keys to remove
|
|
130
|
+
* @param x - Source object
|
|
131
|
+
* @returns New object without specified keys
|
|
132
|
+
*/
|
|
60
133
|
const omit = (ks, x) => {
|
|
61
134
|
const r = { ...x };
|
|
62
135
|
for (const k of ks) {
|
|
@@ -65,6 +138,12 @@ const omit = (ks, x) => {
|
|
|
65
138
|
return r;
|
|
66
139
|
};
|
|
67
140
|
exports.omit = omit;
|
|
141
|
+
/**
|
|
142
|
+
* Creates new object excluding entries with specified values
|
|
143
|
+
* @param xs - Values to exclude
|
|
144
|
+
* @param x - Source object
|
|
145
|
+
* @returns New object without entries containing specified values
|
|
146
|
+
*/
|
|
68
147
|
const omitVals = (xs, x) => {
|
|
69
148
|
const r = {};
|
|
70
149
|
for (const [k, v] of Object.entries(x)) {
|
|
@@ -75,6 +154,12 @@ const omitVals = (xs, x) => {
|
|
|
75
154
|
return r;
|
|
76
155
|
};
|
|
77
156
|
exports.omitVals = omitVals;
|
|
157
|
+
/**
|
|
158
|
+
* Creates new object with only specified keys
|
|
159
|
+
* @param ks - Keys to keep
|
|
160
|
+
* @param x - Source object
|
|
161
|
+
* @returns New object with only specified keys
|
|
162
|
+
*/
|
|
78
163
|
const pick = (ks, x) => {
|
|
79
164
|
const r = { ...x };
|
|
80
165
|
for (const k of Object.keys(x)) {
|
|
@@ -85,12 +170,25 @@ const pick = (ks, x) => {
|
|
|
85
170
|
return r;
|
|
86
171
|
};
|
|
87
172
|
exports.pick = pick;
|
|
173
|
+
/**
|
|
174
|
+
* Generates sequence of numbers from a to b
|
|
175
|
+
* @param a - Start number (inclusive)
|
|
176
|
+
* @param b - End number (exclusive)
|
|
177
|
+
* @param step - Increment between numbers
|
|
178
|
+
* @yields Numbers in sequence
|
|
179
|
+
*/
|
|
88
180
|
function* range(a, b, step = 1) {
|
|
89
181
|
for (let i = a; i < b; i += step) {
|
|
90
182
|
yield i;
|
|
91
183
|
}
|
|
92
184
|
}
|
|
93
185
|
exports.range = range;
|
|
186
|
+
/**
|
|
187
|
+
* Creates new object with transformed keys
|
|
188
|
+
* @param f - Function to transform keys
|
|
189
|
+
* @param x - Source object
|
|
190
|
+
* @returns Object with transformed keys
|
|
191
|
+
*/
|
|
94
192
|
const mapKeys = (f, x) => {
|
|
95
193
|
const r = {};
|
|
96
194
|
for (const [k, v] of Object.entries(x)) {
|
|
@@ -99,6 +197,12 @@ const mapKeys = (f, x) => {
|
|
|
99
197
|
return r;
|
|
100
198
|
};
|
|
101
199
|
exports.mapKeys = mapKeys;
|
|
200
|
+
/**
|
|
201
|
+
* Creates new object with transformed values
|
|
202
|
+
* @param f - Function to transform values
|
|
203
|
+
* @param x - Source object
|
|
204
|
+
* @returns Object with transformed values
|
|
205
|
+
*/
|
|
102
206
|
const mapVals = (f, x) => {
|
|
103
207
|
const r = {};
|
|
104
208
|
for (const [k, v] of Object.entries(x)) {
|
|
@@ -107,63 +211,193 @@ const mapVals = (f, x) => {
|
|
|
107
211
|
return r;
|
|
108
212
|
};
|
|
109
213
|
exports.mapVals = mapVals;
|
|
214
|
+
/**
|
|
215
|
+
* Merges two objects, with left object taking precedence
|
|
216
|
+
* @param a - Left object
|
|
217
|
+
* @param b - Right object
|
|
218
|
+
* @returns Merged object with a's properties overriding b's
|
|
219
|
+
*/
|
|
110
220
|
const mergeLeft = (a, b) => ({ ...b, ...a });
|
|
111
221
|
exports.mergeLeft = mergeLeft;
|
|
222
|
+
/**
|
|
223
|
+
* Merges two objects, with right object taking precedence
|
|
224
|
+
* @param a - Left object
|
|
225
|
+
* @param b - Right object
|
|
226
|
+
* @returns Merged object with b's properties overriding a's
|
|
227
|
+
*/
|
|
112
228
|
const mergeRight = (a, b) => ({ ...a, ...b });
|
|
113
229
|
exports.mergeRight = mergeRight;
|
|
230
|
+
/**
|
|
231
|
+
* Checks if a number is between two values (exclusive)
|
|
232
|
+
* @param bounds - Lower and upper bounds
|
|
233
|
+
* @param n - Number to check
|
|
234
|
+
* @returns True if n is between low and high
|
|
235
|
+
*/
|
|
114
236
|
const between = ([low, high], n) => n > low && n < high;
|
|
115
237
|
exports.between = between;
|
|
238
|
+
/**
|
|
239
|
+
* Checks if a number is between two values (inclusive)
|
|
240
|
+
* @param bounds - Lower and upper bounds
|
|
241
|
+
* @param n - Number to check
|
|
242
|
+
* @returns True if n is between low and high
|
|
243
|
+
*/
|
|
244
|
+
const within = ([low, high], n) => n >= low && n <= high;
|
|
245
|
+
exports.within = within;
|
|
246
|
+
/**
|
|
247
|
+
* Generates random integer between min and max (inclusive)
|
|
248
|
+
* @param min - Minimum value
|
|
249
|
+
* @param max - Maximum value
|
|
250
|
+
* @returns Random integer
|
|
251
|
+
*/
|
|
116
252
|
const randomInt = (min = 0, max = 9) => min + Math.round(Math.random() * (max - min));
|
|
117
253
|
exports.randomInt = randomInt;
|
|
254
|
+
/**
|
|
255
|
+
* Generates random string ID
|
|
256
|
+
* @returns Random string suitable for use as an ID
|
|
257
|
+
*/
|
|
118
258
|
const randomId = () => Math.random().toString().slice(2);
|
|
119
259
|
exports.randomId = randomId;
|
|
260
|
+
/**
|
|
261
|
+
* Removes protocol (http://, https://, etc) from URL
|
|
262
|
+
* @param url - URL to process
|
|
263
|
+
* @returns URL without protocol
|
|
264
|
+
*/
|
|
120
265
|
const stripProtocol = (url) => url.replace(/.*:\/\//, "");
|
|
121
266
|
exports.stripProtocol = stripProtocol;
|
|
267
|
+
/**
|
|
268
|
+
* Formats URL for display by removing protocol, www, and trailing slash
|
|
269
|
+
* @param url - URL to format
|
|
270
|
+
* @returns Formatted URL
|
|
271
|
+
*/
|
|
122
272
|
const displayUrl = (url) => (0, exports.stripProtocol)(url).replace(/^(www\.)?/i, "").replace(/\/$/, "");
|
|
123
273
|
exports.displayUrl = displayUrl;
|
|
274
|
+
/**
|
|
275
|
+
* Extracts and formats domain from URL
|
|
276
|
+
* @param url - URL to process
|
|
277
|
+
* @returns Formatted domain name
|
|
278
|
+
*/
|
|
124
279
|
const displayDomain = (url) => (0, exports.displayUrl)((0, exports.first)(url.split(/[\/\?]/)));
|
|
125
280
|
exports.displayDomain = displayDomain;
|
|
281
|
+
/**
|
|
282
|
+
* Creates a promise that resolves after specified time
|
|
283
|
+
* @param t - Time in milliseconds
|
|
284
|
+
* @returns Promise that resolves after t milliseconds
|
|
285
|
+
*/
|
|
126
286
|
const sleep = (t) => new Promise(resolve => setTimeout(resolve, t));
|
|
127
287
|
exports.sleep = sleep;
|
|
128
|
-
|
|
288
|
+
/**
|
|
289
|
+
* Concatenates multiple arrays, filtering out null/undefined
|
|
290
|
+
* @param xs - Arrays to concatenate
|
|
291
|
+
* @returns Combined array
|
|
292
|
+
*/
|
|
293
|
+
const concat = (...xs) => xs.flatMap(x => (0, exports.isNil)(x) ? [] : x);
|
|
129
294
|
exports.concat = concat;
|
|
295
|
+
/**
|
|
296
|
+
* Appends element to array
|
|
297
|
+
* @param x - Element to append
|
|
298
|
+
* @param xs - Array to append to
|
|
299
|
+
* @returns New array with element appended
|
|
300
|
+
*/
|
|
130
301
|
const append = (x, xs) => (0, exports.concat)(xs, [x]);
|
|
131
302
|
exports.append = append;
|
|
303
|
+
/**
|
|
304
|
+
* Creates union of two arrays
|
|
305
|
+
* @param a - First array
|
|
306
|
+
* @param b - Second array
|
|
307
|
+
* @returns Array containing unique elements from both arrays
|
|
308
|
+
*/
|
|
132
309
|
const union = (a, b) => (0, exports.uniq)([...a, ...b]);
|
|
133
310
|
exports.union = union;
|
|
311
|
+
/**
|
|
312
|
+
* Returns elements common to both arrays
|
|
313
|
+
* @param a - First array
|
|
314
|
+
* @param b - Second array
|
|
315
|
+
* @returns Array of elements present in both inputs
|
|
316
|
+
*/
|
|
134
317
|
const intersection = (a, b) => {
|
|
135
318
|
const s = new Set(b);
|
|
136
319
|
return a.filter(x => s.has(x));
|
|
137
320
|
};
|
|
138
321
|
exports.intersection = intersection;
|
|
322
|
+
/**
|
|
323
|
+
* Returns elements in first array not present in second
|
|
324
|
+
* @param a - Source array
|
|
325
|
+
* @param b - Array of elements to exclude
|
|
326
|
+
* @returns Array containing elements unique to first array
|
|
327
|
+
*/
|
|
139
328
|
const difference = (a, b) => {
|
|
140
329
|
const s = new Set(b);
|
|
141
330
|
return a.filter(x => !s.has(x));
|
|
142
331
|
};
|
|
143
332
|
exports.difference = difference;
|
|
333
|
+
/**
|
|
334
|
+
* Removes all instances of an element from array
|
|
335
|
+
* @param a - Element to remove
|
|
336
|
+
* @param xs - Source array
|
|
337
|
+
* @returns New array with element removed
|
|
338
|
+
*/
|
|
144
339
|
const remove = (a, xs) => xs.filter(x => x !== a);
|
|
145
340
|
exports.remove = remove;
|
|
341
|
+
/**
|
|
342
|
+
* Returns elements from second array not present in first
|
|
343
|
+
* @param a - Array of elements to exclude
|
|
344
|
+
* @param b - Source array
|
|
345
|
+
* @returns Filtered array
|
|
346
|
+
*/
|
|
146
347
|
const without = (a, b) => b.filter(x => !a.includes(x));
|
|
147
348
|
exports.without = without;
|
|
349
|
+
/**
|
|
350
|
+
* Toggles presence of element in array
|
|
351
|
+
* @param x - Element to toggle
|
|
352
|
+
* @param xs - Source array
|
|
353
|
+
* @returns New array with element added or removed
|
|
354
|
+
*/
|
|
148
355
|
const toggle = (x, xs) => xs.includes(x) ? (0, exports.remove)(x, xs) : (0, exports.append)(x, xs);
|
|
149
356
|
exports.toggle = toggle;
|
|
357
|
+
/**
|
|
358
|
+
* Constrains number between min and max values
|
|
359
|
+
* @param bounds - Minimum and maximum allowed values
|
|
360
|
+
* @param n - Number to clamp
|
|
361
|
+
* @returns Clamped value
|
|
362
|
+
*/
|
|
150
363
|
const clamp = ([min, max], n) => Math.min(max, Math.max(min, n));
|
|
151
364
|
exports.clamp = clamp;
|
|
365
|
+
/**
|
|
366
|
+
* Safely parses JSON string
|
|
367
|
+
* @param json - JSON string to parse
|
|
368
|
+
* @returns Parsed object or null if invalid
|
|
369
|
+
*/
|
|
152
370
|
const parseJson = (json) => {
|
|
153
371
|
if (!json)
|
|
154
|
-
return
|
|
372
|
+
return undefined;
|
|
155
373
|
try {
|
|
156
374
|
return JSON.parse(json);
|
|
157
375
|
}
|
|
158
376
|
catch (e) {
|
|
159
|
-
return
|
|
377
|
+
return undefined;
|
|
160
378
|
}
|
|
161
379
|
};
|
|
162
380
|
exports.parseJson = parseJson;
|
|
381
|
+
/**
|
|
382
|
+
* Gets and parses JSON from localStorage
|
|
383
|
+
* @param k - Storage key
|
|
384
|
+
* @returns Parsed value or undefined if invalid/missing
|
|
385
|
+
*/
|
|
163
386
|
const getJson = (k) => (0, exports.parseJson)(localStorage.getItem(k) || "");
|
|
164
387
|
exports.getJson = getJson;
|
|
388
|
+
/**
|
|
389
|
+
* Stringifies and stores value in localStorage
|
|
390
|
+
* @param k - Storage key
|
|
391
|
+
* @param v - Value to store
|
|
392
|
+
*/
|
|
165
393
|
const setJson = (k, v) => localStorage.setItem(k, JSON.stringify(v));
|
|
166
394
|
exports.setJson = setJson;
|
|
395
|
+
/**
|
|
396
|
+
* Safely executes function and handles errors
|
|
397
|
+
* @param f - Function to execute
|
|
398
|
+
* @param onError - Optional error handler
|
|
399
|
+
* @returns Function result or undefined if error
|
|
400
|
+
*/
|
|
167
401
|
const tryCatch = (f, onError) => {
|
|
168
402
|
try {
|
|
169
403
|
const r = f();
|
|
@@ -178,6 +412,13 @@ const tryCatch = (f, onError) => {
|
|
|
178
412
|
return undefined;
|
|
179
413
|
};
|
|
180
414
|
exports.tryCatch = tryCatch;
|
|
415
|
+
/**
|
|
416
|
+
* Truncates string to length, breaking at word boundaries
|
|
417
|
+
* @param s - String to truncate
|
|
418
|
+
* @param l - Maximum length
|
|
419
|
+
* @param suffix - String to append if truncated
|
|
420
|
+
* @returns Truncated string
|
|
421
|
+
*/
|
|
181
422
|
const ellipsize = (s, l, suffix = '...') => {
|
|
182
423
|
if (s.length < l * 1.1) {
|
|
183
424
|
return s;
|
|
@@ -188,6 +429,11 @@ const ellipsize = (s, l, suffix = '...') => {
|
|
|
188
429
|
return s + suffix;
|
|
189
430
|
};
|
|
190
431
|
exports.ellipsize = ellipsize;
|
|
432
|
+
/**
|
|
433
|
+
* Checks if value is a plain object
|
|
434
|
+
* @param obj - Value to check
|
|
435
|
+
* @returns True if value is a plain object
|
|
436
|
+
*/
|
|
191
437
|
const isPojo = (obj) => {
|
|
192
438
|
if (obj === null || typeof obj !== "object") {
|
|
193
439
|
return false;
|
|
@@ -195,6 +441,12 @@ const isPojo = (obj) => {
|
|
|
195
441
|
return Object.getPrototypeOf(obj) === Object.prototype;
|
|
196
442
|
};
|
|
197
443
|
exports.isPojo = isPojo;
|
|
444
|
+
/**
|
|
445
|
+
* Deep equality comparison
|
|
446
|
+
* @param a - First value
|
|
447
|
+
* @param b - Second value
|
|
448
|
+
* @returns True if values are deeply equal
|
|
449
|
+
*/
|
|
198
450
|
const equals = (a, b) => {
|
|
199
451
|
if (a === b)
|
|
200
452
|
return true;
|
|
@@ -239,41 +491,75 @@ const equals = (a, b) => {
|
|
|
239
491
|
};
|
|
240
492
|
exports.equals = equals;
|
|
241
493
|
// Curried utils
|
|
494
|
+
/** Returns a function that gets the nth element of an array */
|
|
242
495
|
const nth = (i) => (xs, ...args) => xs[i];
|
|
243
496
|
exports.nth = nth;
|
|
497
|
+
/** Returns a function that checks if nth element equals value */
|
|
244
498
|
const nthEq = (i, v) => (xs, ...args) => xs[i] === v;
|
|
245
499
|
exports.nthEq = nthEq;
|
|
500
|
+
/** Returns a function that checks if nth element does not equal value */
|
|
246
501
|
const nthNe = (i, v) => (xs, ...args) => xs[i] !== v;
|
|
247
502
|
exports.nthNe = nthNe;
|
|
503
|
+
/** Returns a function that checks if key/value pairs of x match all pairs in spec */
|
|
504
|
+
const spec = (values) => (x) => {
|
|
505
|
+
for (const [k, v] of Object.entries(values)) {
|
|
506
|
+
if (x[k] !== v)
|
|
507
|
+
return false;
|
|
508
|
+
}
|
|
509
|
+
return true;
|
|
510
|
+
};
|
|
511
|
+
exports.spec = spec;
|
|
512
|
+
/** Returns a function that checks equality with value */
|
|
248
513
|
const eq = (v) => (x) => x === v;
|
|
249
514
|
exports.eq = eq;
|
|
515
|
+
/** Returns a function that checks inequality with value */
|
|
250
516
|
const ne = (v) => (x) => x !== v;
|
|
251
517
|
exports.ne = ne;
|
|
518
|
+
/** Returns a function that gets property value from object */
|
|
252
519
|
const prop = (k) => (x) => x[k];
|
|
253
520
|
exports.prop = prop;
|
|
521
|
+
/** Returns a function that adds/updates property on object */
|
|
254
522
|
const assoc = (k, v) => (o) => ({ ...o, [k]: v });
|
|
255
523
|
exports.assoc = assoc;
|
|
524
|
+
/** Generates a hash string from input string */
|
|
256
525
|
const hash = (s) => Math.abs(s.split("").reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0)).toString();
|
|
257
526
|
exports.hash = hash;
|
|
258
527
|
// Collections
|
|
528
|
+
/** Splits array into two parts at index */
|
|
259
529
|
const splitAt = (n, xs) => [xs.slice(0, n), xs.slice(n)];
|
|
260
530
|
exports.splitAt = splitAt;
|
|
531
|
+
/** Inserts element into array at index */
|
|
261
532
|
const insert = (n, x, xs) => [...xs.slice(0, n), x, ...xs.slice(n)];
|
|
262
533
|
exports.insert = insert;
|
|
534
|
+
/** Returns random element from array */
|
|
263
535
|
const choice = (xs) => xs[Math.floor(xs.length * Math.random())];
|
|
264
536
|
exports.choice = choice;
|
|
537
|
+
/** Returns shuffled copy of iterable */
|
|
265
538
|
const shuffle = (xs) => Array.from(xs).sort(() => Math.random() > 0.5 ? 1 : -1);
|
|
266
539
|
exports.shuffle = shuffle;
|
|
540
|
+
/** Returns n random elements from array */
|
|
267
541
|
const sample = (n, xs) => (0, exports.shuffle)(xs).slice(0, n);
|
|
268
542
|
exports.sample = sample;
|
|
543
|
+
/** Checks if value is iterable */
|
|
269
544
|
const isIterable = (x) => Symbol.iterator in Object(x);
|
|
270
545
|
exports.isIterable = isIterable;
|
|
546
|
+
/** Ensures value is iterable by wrapping in array if needed */
|
|
271
547
|
const toIterable = (x) => (0, exports.isIterable)(x) ? x : [x];
|
|
272
548
|
exports.toIterable = toIterable;
|
|
549
|
+
/** Ensures value is array by wrapping if needed */
|
|
273
550
|
const ensurePlural = (x) => (x instanceof Array ? x : [x]);
|
|
274
551
|
exports.ensurePlural = ensurePlural;
|
|
552
|
+
/** Converts string or number to number */
|
|
275
553
|
const ensureNumber = (x) => parseFloat(x);
|
|
276
554
|
exports.ensureNumber = ensureNumber;
|
|
555
|
+
/** Returns a function that gets property value from object */
|
|
556
|
+
const pluck = (k, xs) => xs.map(x => x[k]);
|
|
557
|
+
exports.pluck = pluck;
|
|
558
|
+
/**
|
|
559
|
+
* Creates object from array of key-value pairs
|
|
560
|
+
* @param pairs - Array of [key, value] tuples
|
|
561
|
+
* @returns Object with keys and values from pairs
|
|
562
|
+
*/
|
|
277
563
|
const fromPairs = (pairs) => {
|
|
278
564
|
const r = {};
|
|
279
565
|
for (const [k, v] of pairs) {
|
|
@@ -284,8 +570,19 @@ const fromPairs = (pairs) => {
|
|
|
284
570
|
return r;
|
|
285
571
|
};
|
|
286
572
|
exports.fromPairs = fromPairs;
|
|
573
|
+
/**
|
|
574
|
+
* Flattens array of arrays into single array
|
|
575
|
+
* @param xs - Array of arrays to flatten
|
|
576
|
+
* @returns Flattened array
|
|
577
|
+
*/
|
|
287
578
|
const flatten = (xs) => xs.flatMap(exports.identity);
|
|
288
579
|
exports.flatten = flatten;
|
|
580
|
+
/**
|
|
581
|
+
* Splits array into two arrays based on predicate
|
|
582
|
+
* @param f - Function to test elements
|
|
583
|
+
* @param xs - Array to partition
|
|
584
|
+
* @returns Tuple of [matching, non-matching] arrays
|
|
585
|
+
*/
|
|
289
586
|
const partition = (f, xs) => {
|
|
290
587
|
const a = [];
|
|
291
588
|
const b = [];
|
|
@@ -300,8 +597,19 @@ const partition = (f, xs) => {
|
|
|
300
597
|
return [a, b];
|
|
301
598
|
};
|
|
302
599
|
exports.partition = partition;
|
|
600
|
+
/**
|
|
601
|
+
* Returns array with duplicate elements removed
|
|
602
|
+
* @param xs - Array with possible duplicates
|
|
603
|
+
* @returns Array with unique elements
|
|
604
|
+
*/
|
|
303
605
|
const uniq = (xs) => Array.from(new Set(xs));
|
|
304
606
|
exports.uniq = uniq;
|
|
607
|
+
/**
|
|
608
|
+
* Returns array with elements unique by key function
|
|
609
|
+
* @param f - Function to generate key for each element
|
|
610
|
+
* @param xs - Input array
|
|
611
|
+
* @returns Array with elements unique by key
|
|
612
|
+
*/
|
|
305
613
|
const uniqBy = (f, xs) => {
|
|
306
614
|
const s = new Set();
|
|
307
615
|
const r = [];
|
|
@@ -316,14 +624,31 @@ const uniqBy = (f, xs) => {
|
|
|
316
624
|
return r;
|
|
317
625
|
};
|
|
318
626
|
exports.uniqBy = uniqBy;
|
|
627
|
+
/**
|
|
628
|
+
* Returns sorted copy of array
|
|
629
|
+
* @param xs - Array to sort
|
|
630
|
+
* @returns New sorted array
|
|
631
|
+
*/
|
|
319
632
|
const sort = (xs) => [...xs].sort();
|
|
320
633
|
exports.sort = sort;
|
|
634
|
+
/**
|
|
635
|
+
* Returns array sorted by key function
|
|
636
|
+
* @param f - Function to generate sort key
|
|
637
|
+
* @param xs - Array to sort
|
|
638
|
+
* @returns Sorted array
|
|
639
|
+
*/
|
|
321
640
|
const sortBy = (f, xs) => [...xs].sort((a, b) => {
|
|
322
641
|
const x = f(a);
|
|
323
642
|
const y = f(b);
|
|
324
643
|
return x < y ? -1 : x > y ? 1 : 0;
|
|
325
644
|
});
|
|
326
645
|
exports.sortBy = sortBy;
|
|
646
|
+
/**
|
|
647
|
+
* Groups array elements by key function
|
|
648
|
+
* @param f - Function to generate group key
|
|
649
|
+
* @param xs - Array to group
|
|
650
|
+
* @returns Map of groups
|
|
651
|
+
*/
|
|
327
652
|
const groupBy = (f, xs) => {
|
|
328
653
|
const r = new Map();
|
|
329
654
|
for (const x of xs) {
|
|
@@ -338,6 +663,12 @@ const groupBy = (f, xs) => {
|
|
|
338
663
|
return r;
|
|
339
664
|
};
|
|
340
665
|
exports.groupBy = groupBy;
|
|
666
|
+
/**
|
|
667
|
+
* Creates map from array using key function
|
|
668
|
+
* @param f - Function to generate key
|
|
669
|
+
* @param xs - Array to index
|
|
670
|
+
* @returns Map of values by key
|
|
671
|
+
*/
|
|
341
672
|
const indexBy = (f, xs) => {
|
|
342
673
|
const r = new Map();
|
|
343
674
|
for (const x of xs) {
|
|
@@ -346,6 +677,12 @@ const indexBy = (f, xs) => {
|
|
|
346
677
|
return r;
|
|
347
678
|
};
|
|
348
679
|
exports.indexBy = indexBy;
|
|
680
|
+
/**
|
|
681
|
+
* Creates array of specified length using generator function
|
|
682
|
+
* @param n - Length of array
|
|
683
|
+
* @param f - Function to generate each element
|
|
684
|
+
* @returns Generated array
|
|
685
|
+
*/
|
|
349
686
|
const initArray = (n, f) => {
|
|
350
687
|
const result = [];
|
|
351
688
|
for (let i = 0; i < n; i++) {
|
|
@@ -354,6 +691,12 @@ const initArray = (n, f) => {
|
|
|
354
691
|
return result;
|
|
355
692
|
};
|
|
356
693
|
exports.initArray = initArray;
|
|
694
|
+
/**
|
|
695
|
+
* Splits array into chunks of specified length
|
|
696
|
+
* @param chunkLength - Maximum length of each chunk
|
|
697
|
+
* @param xs - Array to split
|
|
698
|
+
* @returns Array of chunks
|
|
699
|
+
*/
|
|
357
700
|
const chunk = (chunkLength, xs) => {
|
|
358
701
|
const result = [];
|
|
359
702
|
const current = [];
|
|
@@ -371,6 +714,12 @@ const chunk = (chunkLength, xs) => {
|
|
|
371
714
|
return result;
|
|
372
715
|
};
|
|
373
716
|
exports.chunk = chunk;
|
|
717
|
+
/**
|
|
718
|
+
* Splits array into specified number of chunks
|
|
719
|
+
* @param n - Number of chunks
|
|
720
|
+
* @param xs - Array to split
|
|
721
|
+
* @returns Array of n chunks
|
|
722
|
+
*/
|
|
374
723
|
const chunks = (n, xs) => {
|
|
375
724
|
const result = (0, exports.initArray)(n, () => []);
|
|
376
725
|
for (let i = 0; i < xs.length; i++) {
|
|
@@ -379,6 +728,11 @@ const chunks = (n, xs) => {
|
|
|
379
728
|
return result;
|
|
380
729
|
};
|
|
381
730
|
exports.chunks = chunks;
|
|
731
|
+
/**
|
|
732
|
+
* Creates function that only executes once
|
|
733
|
+
* @param f - Function to wrap
|
|
734
|
+
* @returns Function that executes f only on first call
|
|
735
|
+
*/
|
|
382
736
|
const once = (f) => {
|
|
383
737
|
let called = false;
|
|
384
738
|
return (...args) => {
|
|
@@ -389,6 +743,11 @@ const once = (f) => {
|
|
|
389
743
|
};
|
|
390
744
|
};
|
|
391
745
|
exports.once = once;
|
|
746
|
+
/**
|
|
747
|
+
* Memoizes function results based on arguments
|
|
748
|
+
* @param f - Function to memoize
|
|
749
|
+
* @returns Memoized function
|
|
750
|
+
*/
|
|
392
751
|
const memoize = (f) => {
|
|
393
752
|
let prevArgs;
|
|
394
753
|
let result;
|
|
@@ -401,6 +760,12 @@ const memoize = (f) => {
|
|
|
401
760
|
};
|
|
402
761
|
};
|
|
403
762
|
exports.memoize = memoize;
|
|
763
|
+
/**
|
|
764
|
+
* Creates throttled version of function
|
|
765
|
+
* @param ms - Minimum time between calls
|
|
766
|
+
* @param f - Function to throttle
|
|
767
|
+
* @returns Throttled function
|
|
768
|
+
*/
|
|
404
769
|
const throttle = (ms, f) => {
|
|
405
770
|
if (ms === 0) {
|
|
406
771
|
return f;
|
|
@@ -426,6 +791,12 @@ const throttle = (ms, f) => {
|
|
|
426
791
|
};
|
|
427
792
|
};
|
|
428
793
|
exports.throttle = throttle;
|
|
794
|
+
/**
|
|
795
|
+
* Creates throttled function that returns cached value
|
|
796
|
+
* @param ms - Minimum time between updates
|
|
797
|
+
* @param f - Function to throttle
|
|
798
|
+
* @returns Function returning latest value
|
|
799
|
+
*/
|
|
429
800
|
const throttleWithValue = (ms, f) => {
|
|
430
801
|
let value;
|
|
431
802
|
const update = (0, exports.throttle)(ms, () => {
|
|
@@ -437,6 +808,12 @@ const throttleWithValue = (ms, f) => {
|
|
|
437
808
|
};
|
|
438
809
|
};
|
|
439
810
|
exports.throttleWithValue = throttleWithValue;
|
|
811
|
+
/**
|
|
812
|
+
* Creates batching function that collects items
|
|
813
|
+
* @param t - Time window for batching
|
|
814
|
+
* @param f - Function to process batch
|
|
815
|
+
* @returns Function that adds items to batch
|
|
816
|
+
*/
|
|
440
817
|
const batch = (t, f) => {
|
|
441
818
|
const xs = [];
|
|
442
819
|
const cb = (0, exports.throttle)(t, () => xs.length > 0 && f(xs.splice(0)));
|
|
@@ -446,6 +823,12 @@ const batch = (t, f) => {
|
|
|
446
823
|
};
|
|
447
824
|
};
|
|
448
825
|
exports.batch = batch;
|
|
826
|
+
/**
|
|
827
|
+
* Creates batching function that returns results
|
|
828
|
+
* @param t - Time window for batching
|
|
829
|
+
* @param execute - Function to process batch
|
|
830
|
+
* @returns Function that returns promise of result
|
|
831
|
+
*/
|
|
449
832
|
const batcher = (t, execute) => {
|
|
450
833
|
const queue = [];
|
|
451
834
|
const _execute = async () => {
|
|
@@ -464,48 +847,108 @@ const batcher = (t, execute) => {
|
|
|
464
847
|
});
|
|
465
848
|
};
|
|
466
849
|
exports.batcher = batcher;
|
|
850
|
+
/**
|
|
851
|
+
* Adds value to Set at key in object
|
|
852
|
+
* @param m - Object mapping keys to Sets
|
|
853
|
+
* @param k - Key to add to
|
|
854
|
+
* @param v - Value to add
|
|
855
|
+
*/
|
|
467
856
|
const addToKey = (m, k, v) => {
|
|
468
857
|
const s = m[k] || new Set();
|
|
469
858
|
s.add(v);
|
|
470
859
|
m[k] = s;
|
|
471
860
|
};
|
|
472
861
|
exports.addToKey = addToKey;
|
|
862
|
+
/**
|
|
863
|
+
* Pushes value to array at key in object
|
|
864
|
+
* @param m - Object mapping keys to arrays
|
|
865
|
+
* @param k - Key to push to
|
|
866
|
+
* @param v - Value to push
|
|
867
|
+
*/
|
|
473
868
|
const pushToKey = (m, k, v) => {
|
|
474
869
|
const a = m[k] || [];
|
|
475
870
|
a.push(v);
|
|
476
871
|
m[k] = a;
|
|
477
872
|
};
|
|
478
873
|
exports.pushToKey = pushToKey;
|
|
874
|
+
/**
|
|
875
|
+
* Adds value to Set at key in Map
|
|
876
|
+
* @param m - Map of Sets
|
|
877
|
+
* @param k - Key to add to
|
|
878
|
+
* @param v - Value to add
|
|
879
|
+
*/
|
|
479
880
|
const addToMapKey = (m, k, v) => {
|
|
480
881
|
const s = m.get(k) || new Set();
|
|
481
882
|
s.add(v);
|
|
482
883
|
m.set(k, s);
|
|
483
884
|
};
|
|
484
885
|
exports.addToMapKey = addToMapKey;
|
|
886
|
+
/**
|
|
887
|
+
* Pushes value to array at key in Map
|
|
888
|
+
* @param m - Map of arrays
|
|
889
|
+
* @param k - Key to push to
|
|
890
|
+
* @param v - Value to push
|
|
891
|
+
*/
|
|
485
892
|
const pushToMapKey = (m, k, v) => {
|
|
486
893
|
const a = m.get(k) || [];
|
|
487
894
|
a.push(v);
|
|
488
895
|
m.set(k, a);
|
|
489
896
|
};
|
|
490
897
|
exports.pushToMapKey = pushToMapKey;
|
|
898
|
+
/**
|
|
899
|
+
* Switches on key in object, with default fallback
|
|
900
|
+
* @param k - Key to look up
|
|
901
|
+
* @param m - Object with values and optional default
|
|
902
|
+
* @returns Value at key or default value
|
|
903
|
+
*/
|
|
491
904
|
const switcher = (k, m) => m[k] === undefined ? m.default : m[k];
|
|
492
905
|
exports.switcher = switcher;
|
|
493
|
-
|
|
906
|
+
/** One minute in seconds */
|
|
494
907
|
exports.MINUTE = 60;
|
|
908
|
+
/** One hour in seconds */
|
|
495
909
|
exports.HOUR = 60 * exports.MINUTE;
|
|
910
|
+
/** One day in seconds */
|
|
496
911
|
exports.DAY = 24 * exports.HOUR;
|
|
912
|
+
/** One week in seconds */
|
|
497
913
|
exports.WEEK = 7 * exports.DAY;
|
|
914
|
+
/** One month in seconds (approximate) */
|
|
498
915
|
exports.MONTH = 30 * exports.DAY;
|
|
916
|
+
/** One quarter in seconds (approximate) */
|
|
499
917
|
exports.QUARTER = 90 * exports.DAY;
|
|
918
|
+
/** One year in seconds (approximate) */
|
|
500
919
|
exports.YEAR = 365 * exports.DAY;
|
|
920
|
+
/**
|
|
921
|
+
* Multiplies time unit by count
|
|
922
|
+
* @param unit - Time unit in seconds
|
|
923
|
+
* @param count - Number of units
|
|
924
|
+
* @returns Total seconds
|
|
925
|
+
*/
|
|
501
926
|
const int = (unit, count = 1) => unit * count;
|
|
502
927
|
exports.int = int;
|
|
928
|
+
/** Returns current Unix timestamp in seconds */
|
|
503
929
|
const now = () => Math.round(Date.now() / 1000);
|
|
504
930
|
exports.now = now;
|
|
931
|
+
/**
|
|
932
|
+
* Returns Unix timestamp from specified time ago
|
|
933
|
+
* @param unit - Time unit in seconds
|
|
934
|
+
* @param count - Number of units
|
|
935
|
+
* @returns Timestamp in seconds
|
|
936
|
+
*/
|
|
505
937
|
const ago = (unit, count = 1) => (0, exports.now)() - (0, exports.int)(unit, count);
|
|
506
938
|
exports.ago = ago;
|
|
939
|
+
/**
|
|
940
|
+
* Converts seconds to milliseconds
|
|
941
|
+
* @param seconds - Time in seconds
|
|
942
|
+
* @returns Time in milliseconds
|
|
943
|
+
*/
|
|
507
944
|
const ms = (seconds) => seconds * 1000;
|
|
508
945
|
exports.ms = ms;
|
|
946
|
+
/**
|
|
947
|
+
* Fetches JSON from URL with options
|
|
948
|
+
* @param url - URL to fetch from
|
|
949
|
+
* @param opts - Fetch options
|
|
950
|
+
* @returns Promise of parsed JSON response
|
|
951
|
+
*/
|
|
509
952
|
const fetchJson = async (url, opts = {}) => {
|
|
510
953
|
if (!opts.headers) {
|
|
511
954
|
opts.headers = {};
|
|
@@ -516,6 +959,13 @@ const fetchJson = async (url, opts = {}) => {
|
|
|
516
959
|
return json;
|
|
517
960
|
};
|
|
518
961
|
exports.fetchJson = fetchJson;
|
|
962
|
+
/**
|
|
963
|
+
* Posts JSON data to URL
|
|
964
|
+
* @param url - URL to post to
|
|
965
|
+
* @param data - Data to send
|
|
966
|
+
* @param opts - Additional fetch options
|
|
967
|
+
* @returns Promise of parsed JSON response
|
|
968
|
+
*/
|
|
519
969
|
const postJson = async (url, data, opts = {}) => {
|
|
520
970
|
if (!opts.method) {
|
|
521
971
|
opts.method = "POST";
|
|
@@ -528,15 +978,31 @@ const postJson = async (url, data, opts = {}) => {
|
|
|
528
978
|
return (0, exports.fetchJson)(url, opts);
|
|
529
979
|
};
|
|
530
980
|
exports.postJson = postJson;
|
|
531
|
-
|
|
981
|
+
/**
|
|
982
|
+
* Uploads file to URL
|
|
983
|
+
* @param url - Upload URL
|
|
984
|
+
* @param file - File to upload
|
|
985
|
+
* @returns Promise of parsed JSON response
|
|
986
|
+
*/
|
|
987
|
+
const uploadFile = (url, file) => {
|
|
532
988
|
const body = new FormData();
|
|
533
|
-
body.append("file",
|
|
989
|
+
body.append("file", file);
|
|
534
990
|
return (0, exports.fetchJson)(url, { method: "POST", body });
|
|
535
991
|
};
|
|
536
992
|
exports.uploadFile = uploadFile;
|
|
537
|
-
|
|
538
|
-
|
|
993
|
+
/**
|
|
994
|
+
* Converts hex string to bech32 format
|
|
995
|
+
* @param prefix - Bech32 prefix
|
|
996
|
+
* @param hex - Hex string to convert
|
|
997
|
+
* @returns Bech32 encoded string
|
|
998
|
+
*/
|
|
999
|
+
const hexToBech32 = (prefix, hex) => base_1.bech32.encode(prefix, base_1.bech32.toWords(base_1.utf8.decode(hex)), false);
|
|
539
1000
|
exports.hexToBech32 = hexToBech32;
|
|
1001
|
+
/**
|
|
1002
|
+
* Converts bech32 string to hex format
|
|
1003
|
+
* @param b32 - Bech32 string to convert
|
|
1004
|
+
* @returns Hex encoded string
|
|
1005
|
+
*/
|
|
540
1006
|
const bech32ToHex = (b32) => base_1.utf8.encode(base_1.bech32.fromWords(base_1.bech32.decode(b32, false).words));
|
|
541
1007
|
exports.bech32ToHex = bech32ToHex;
|
|
542
1008
|
//# sourceMappingURL=Tools.cjs.map
|