@welshman/lib 0.0.32 → 0.0.33

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.
Files changed (42) hide show
  1. package/README.md +10 -8
  2. package/build/src/Context.cjs +17 -0
  3. package/build/src/Context.cjs.map +1 -1
  4. package/build/src/Context.d.ts +17 -0
  5. package/build/src/Context.mjs +17 -0
  6. package/build/src/Context.mjs.map +1 -1
  7. package/build/src/Deferred.cjs +9 -0
  8. package/build/src/Deferred.cjs.map +1 -1
  9. package/build/src/Deferred.d.ts +11 -0
  10. package/build/src/Deferred.mjs +9 -0
  11. package/build/src/Deferred.mjs.map +1 -1
  12. package/build/src/Emitter.cjs +9 -0
  13. package/build/src/Emitter.cjs.map +1 -1
  14. package/build/src/Emitter.d.ts +9 -0
  15. package/build/src/Emitter.mjs +9 -0
  16. package/build/src/Emitter.mjs.map +1 -1
  17. package/build/src/LRUCache.cjs +16 -0
  18. package/build/src/LRUCache.cjs.map +1 -1
  19. package/build/src/LRUCache.d.ts +16 -0
  20. package/build/src/LRUCache.mjs +16 -0
  21. package/build/src/LRUCache.mjs.map +1 -1
  22. package/build/src/Tools.cjs +463 -12
  23. package/build/src/Tools.cjs.map +1 -1
  24. package/build/src/Tools.d.ts +458 -17
  25. package/build/src/Tools.mjs +459 -9
  26. package/build/src/Tools.mjs.map +1 -1
  27. package/build/src/Worker.cjs +27 -1
  28. package/build/src/Worker.cjs.map +1 -1
  29. package/build/src/Worker.d.ts +25 -0
  30. package/build/src/Worker.mjs +27 -1
  31. package/build/src/Worker.mjs.map +1 -1
  32. package/build/src/index.cjs +0 -1
  33. package/build/src/index.cjs.map +1 -1
  34. package/build/src/index.d.ts +0 -1
  35. package/build/src/index.mjs +0 -1
  36. package/build/src/index.mjs.map +1 -1
  37. package/package.json +1 -1
  38. package/build/src/Fluent.cjs +0 -47
  39. package/build/src/Fluent.cjs.map +0 -1
  40. package/build/src/Fluent.d.ts +0 -34
  41. package/build/src/Fluent.mjs +0 -43
  42. package/build/src/Fluent.mjs.map +0 -1
@@ -1,31 +1,101 @@
1
1
  import { bech32, utf8 } from "@scure/base";
2
+ /** Checks if a value is null or undefined */
2
3
  export const isNil = (x) => [null, undefined].includes(x);
4
+ /**
5
+ * Executes a function if the value is defined
6
+ * @param x - The value to check
7
+ * @param f - Function to execute if x is defined
8
+ * @returns Result of f(x) if x is defined, undefined otherwise
9
+ */
3
10
  export const ifLet = (x, f) => x === undefined ? undefined : f(x);
4
- // Regular old utils
11
+ /** Function that does nothing and returns undefined */
5
12
  export const noop = (...args) => undefined;
13
+ /**
14
+ * Returns the first element of an array
15
+ * @param xs - The array
16
+ * @returns First element or undefined
17
+ */
6
18
  export const first = (xs, ...args) => xs[0];
19
+ /**
20
+ * Returns the first element of the first array in a nested array
21
+ * @param xs - Array of arrays
22
+ * @returns First element of first array or undefined
23
+ */
7
24
  export const ffirst = (xs, ...args) => xs[0][0];
25
+ /**
26
+ * Returns the last element of an array
27
+ * @param xs - The array
28
+ * @returns Last element or undefined
29
+ */
8
30
  export const last = (xs, ...args) => xs[xs.length - 1];
31
+ /**
32
+ * Returns the input value unchanged
33
+ * @param x - Any value
34
+ * @returns The same value
35
+ */
9
36
  export const identity = (x, ...args) => x;
37
+ /**
38
+ * Creates a function that always returns the same value
39
+ * @param x - Value to return
40
+ * @returns Function that returns x
41
+ */
10
42
  export const always = (x, ...args) => () => x;
43
+ /**
44
+ * Returns the logical NOT of a value
45
+ * @param x - Value to negate
46
+ * @returns !x
47
+ */
11
48
  export const not = (x, ...args) => !x;
49
+ /** Converts a Maybe<number> to a number, defaulting to 0 */
12
50
  export const num = (x) => x || 0;
51
+ /** Adds two numbers, handling undefined values */
13
52
  export const add = (x, y) => num(x) + num(y);
53
+ /** Subtracts two numbers, handling undefined values */
14
54
  export const sub = (x, y) => num(x) - num(y);
55
+ /** Multiplies two numbers, handling undefined values */
15
56
  export const mul = (x, y) => num(x) * num(y);
57
+ /** Divides two numbers, handling undefined values */
16
58
  export const div = (x, y) => num(x) / y;
59
+ /** Increments a number by 1, handling undefined values */
17
60
  export const inc = (x) => add(x, 1);
61
+ /** Decrements a number by 1, handling undefined values */
18
62
  export const dec = (x) => sub(x, 1);
63
+ /** Less than comparison, handling undefined values */
19
64
  export const lt = (x, y) => num(x) < num(y);
65
+ /** Less than or equal comparison, handling undefined values */
20
66
  export const lte = (x, y) => num(x) <= num(y);
67
+ /** Greater than comparison, handling undefined values */
21
68
  export const gt = (x, y) => num(x) > num(y);
69
+ /** Greater than or equal comparison, handling undefined values */
22
70
  export const gte = (x, y) => num(x) >= num(y);
71
+ /** Returns maximum value in array, handling undefined values */
23
72
  export const max = (xs) => xs.reduce((a, b) => Math.max(num(a), num(b)), 0);
73
+ /** Returns minimum value in array, handling undefined values */
24
74
  export const min = (xs) => xs.reduce((a, b) => Math.min(num(a), num(b)), 0);
75
+ /** Returns sum of array values, handling undefined values */
25
76
  export const sum = (xs) => xs.reduce((a, b) => add(a, b), 0);
77
+ /** Returns average of array values, handling undefined values */
26
78
  export const avg = (xs) => sum(xs) / xs.length;
79
+ /**
80
+ * Returns array with first n elements removed
81
+ * @param n - Number of elements to drop
82
+ * @param xs - Input array
83
+ * @returns Array with first n elements removed
84
+ */
27
85
  export const drop = (n, xs) => xs.slice(n);
86
+ /**
87
+ * Returns first n elements of array
88
+ * @param n - Number of elements to take
89
+ * @param xs - Input array
90
+ * @returns Array of first n elements
91
+ */
28
92
  export const take = (n, xs) => xs.slice(0, n);
93
+ /**
94
+ * Creates new object with specified keys removed
95
+ * @param ks - Keys to remove
96
+ * @param x - Source object
97
+ * @returns New object without specified keys
98
+ */
29
99
  export const omit = (ks, x) => {
30
100
  const r = { ...x };
31
101
  for (const k of ks) {
@@ -33,6 +103,12 @@ export const omit = (ks, x) => {
33
103
  }
34
104
  return r;
35
105
  };
106
+ /**
107
+ * Creates new object excluding entries with specified values
108
+ * @param xs - Values to exclude
109
+ * @param x - Source object
110
+ * @returns New object without entries containing specified values
111
+ */
36
112
  export const omitVals = (xs, x) => {
37
113
  const r = {};
38
114
  for (const [k, v] of Object.entries(x)) {
@@ -42,6 +118,12 @@ export const omitVals = (xs, x) => {
42
118
  }
43
119
  return r;
44
120
  };
121
+ /**
122
+ * Creates new object with only specified keys
123
+ * @param ks - Keys to keep
124
+ * @param x - Source object
125
+ * @returns New object with only specified keys
126
+ */
45
127
  export const pick = (ks, x) => {
46
128
  const r = { ...x };
47
129
  for (const k of Object.keys(x)) {
@@ -51,11 +133,24 @@ export const pick = (ks, x) => {
51
133
  }
52
134
  return r;
53
135
  };
136
+ /**
137
+ * Generates sequence of numbers from a to b
138
+ * @param a - Start number (inclusive)
139
+ * @param b - End number (exclusive)
140
+ * @param step - Increment between numbers
141
+ * @yields Numbers in sequence
142
+ */
54
143
  export function* range(a, b, step = 1) {
55
144
  for (let i = a; i < b; i += step) {
56
145
  yield i;
57
146
  }
58
147
  }
148
+ /**
149
+ * Creates new object with transformed keys
150
+ * @param f - Function to transform keys
151
+ * @param x - Source object
152
+ * @returns Object with transformed keys
153
+ */
59
154
  export const mapKeys = (f, x) => {
60
155
  const r = {};
61
156
  for (const [k, v] of Object.entries(x)) {
@@ -63,6 +158,12 @@ export const mapKeys = (f, x) => {
63
158
  }
64
159
  return r;
65
160
  };
161
+ /**
162
+ * Creates new object with transformed values
163
+ * @param f - Function to transform values
164
+ * @param x - Source object
165
+ * @returns Object with transformed values
166
+ */
66
167
  export const mapVals = (f, x) => {
67
168
  const r = {};
68
169
  for (const [k, v] of Object.entries(x)) {
@@ -70,42 +171,171 @@ export const mapVals = (f, x) => {
70
171
  }
71
172
  return r;
72
173
  };
174
+ /**
175
+ * Merges two objects, with left object taking precedence
176
+ * @param a - Left object
177
+ * @param b - Right object
178
+ * @returns Merged object with a's properties overriding b's
179
+ */
73
180
  export const mergeLeft = (a, b) => ({ ...b, ...a });
181
+ /**
182
+ * Merges two objects, with right object taking precedence
183
+ * @param a - Left object
184
+ * @param b - Right object
185
+ * @returns Merged object with b's properties overriding a's
186
+ */
74
187
  export const mergeRight = (a, b) => ({ ...a, ...b });
188
+ /**
189
+ * Checks if a number is between two values (exclusive)
190
+ * @param bounds - Lower and upper bounds
191
+ * @param n - Number to check
192
+ * @returns True if n is between low and high
193
+ */
75
194
  export const between = ([low, high], n) => n > low && n < high;
195
+ /**
196
+ * Checks if a number is between two values (inclusive)
197
+ * @param bounds - Lower and upper bounds
198
+ * @param n - Number to check
199
+ * @returns True if n is between low and high
200
+ */
201
+ export const within = ([low, high], n) => n >= low && n <= high;
202
+ /**
203
+ * Generates random integer between min and max (inclusive)
204
+ * @param min - Minimum value
205
+ * @param max - Maximum value
206
+ * @returns Random integer
207
+ */
76
208
  export const randomInt = (min = 0, max = 9) => min + Math.round(Math.random() * (max - min));
209
+ /**
210
+ * Generates random string ID
211
+ * @returns Random string suitable for use as an ID
212
+ */
77
213
  export const randomId = () => Math.random().toString().slice(2);
214
+ /**
215
+ * Removes protocol (http://, https://, etc) from URL
216
+ * @param url - URL to process
217
+ * @returns URL without protocol
218
+ */
78
219
  export const stripProtocol = (url) => url.replace(/.*:\/\//, "");
220
+ /**
221
+ * Formats URL for display by removing protocol, www, and trailing slash
222
+ * @param url - URL to format
223
+ * @returns Formatted URL
224
+ */
79
225
  export const displayUrl = (url) => stripProtocol(url).replace(/^(www\.)?/i, "").replace(/\/$/, "");
226
+ /**
227
+ * Extracts and formats domain from URL
228
+ * @param url - URL to process
229
+ * @returns Formatted domain name
230
+ */
80
231
  export const displayDomain = (url) => displayUrl(first(url.split(/[\/\?]/)));
232
+ /**
233
+ * Creates a promise that resolves after specified time
234
+ * @param t - Time in milliseconds
235
+ * @returns Promise that resolves after t milliseconds
236
+ */
81
237
  export const sleep = (t) => new Promise(resolve => setTimeout(resolve, t));
82
- export const concat = (...xs) => xs.flatMap(x => x || []);
238
+ /**
239
+ * Concatenates multiple arrays, filtering out null/undefined
240
+ * @param xs - Arrays to concatenate
241
+ * @returns Combined array
242
+ */
243
+ export const concat = (...xs) => xs.flatMap(x => isNil(x) ? [] : x);
244
+ /**
245
+ * Appends element to array
246
+ * @param x - Element to append
247
+ * @param xs - Array to append to
248
+ * @returns New array with element appended
249
+ */
83
250
  export const append = (x, xs) => concat(xs, [x]);
251
+ /**
252
+ * Creates union of two arrays
253
+ * @param a - First array
254
+ * @param b - Second array
255
+ * @returns Array containing unique elements from both arrays
256
+ */
84
257
  export const union = (a, b) => uniq([...a, ...b]);
258
+ /**
259
+ * Returns elements common to both arrays
260
+ * @param a - First array
261
+ * @param b - Second array
262
+ * @returns Array of elements present in both inputs
263
+ */
85
264
  export const intersection = (a, b) => {
86
265
  const s = new Set(b);
87
266
  return a.filter(x => s.has(x));
88
267
  };
268
+ /**
269
+ * Returns elements in first array not present in second
270
+ * @param a - Source array
271
+ * @param b - Array of elements to exclude
272
+ * @returns Array containing elements unique to first array
273
+ */
89
274
  export const difference = (a, b) => {
90
275
  const s = new Set(b);
91
276
  return a.filter(x => !s.has(x));
92
277
  };
278
+ /**
279
+ * Removes all instances of an element from array
280
+ * @param a - Element to remove
281
+ * @param xs - Source array
282
+ * @returns New array with element removed
283
+ */
93
284
  export const remove = (a, xs) => xs.filter(x => x !== a);
285
+ /**
286
+ * Returns elements from second array not present in first
287
+ * @param a - Array of elements to exclude
288
+ * @param b - Source array
289
+ * @returns Filtered array
290
+ */
94
291
  export const without = (a, b) => b.filter(x => !a.includes(x));
292
+ /**
293
+ * Toggles presence of element in array
294
+ * @param x - Element to toggle
295
+ * @param xs - Source array
296
+ * @returns New array with element added or removed
297
+ */
95
298
  export const toggle = (x, xs) => xs.includes(x) ? remove(x, xs) : append(x, xs);
299
+ /**
300
+ * Constrains number between min and max values
301
+ * @param bounds - Minimum and maximum allowed values
302
+ * @param n - Number to clamp
303
+ * @returns Clamped value
304
+ */
96
305
  export const clamp = ([min, max], n) => Math.min(max, Math.max(min, n));
306
+ /**
307
+ * Safely parses JSON string
308
+ * @param json - JSON string to parse
309
+ * @returns Parsed object or null if invalid
310
+ */
97
311
  export const parseJson = (json) => {
98
312
  if (!json)
99
- return null;
313
+ return undefined;
100
314
  try {
101
315
  return JSON.parse(json);
102
316
  }
103
317
  catch (e) {
104
- return null;
318
+ return undefined;
105
319
  }
106
320
  };
321
+ /**
322
+ * Gets and parses JSON from localStorage
323
+ * @param k - Storage key
324
+ * @returns Parsed value or undefined if invalid/missing
325
+ */
107
326
  export const getJson = (k) => parseJson(localStorage.getItem(k) || "");
327
+ /**
328
+ * Stringifies and stores value in localStorage
329
+ * @param k - Storage key
330
+ * @param v - Value to store
331
+ */
108
332
  export const setJson = (k, v) => localStorage.setItem(k, JSON.stringify(v));
333
+ /**
334
+ * Safely executes function and handles errors
335
+ * @param f - Function to execute
336
+ * @param onError - Optional error handler
337
+ * @returns Function result or undefined if error
338
+ */
109
339
  export const tryCatch = (f, onError) => {
110
340
  try {
111
341
  const r = f();
@@ -119,6 +349,13 @@ export const tryCatch = (f, onError) => {
119
349
  }
120
350
  return undefined;
121
351
  };
352
+ /**
353
+ * Truncates string to length, breaking at word boundaries
354
+ * @param s - String to truncate
355
+ * @param l - Maximum length
356
+ * @param suffix - String to append if truncated
357
+ * @returns Truncated string
358
+ */
122
359
  export const ellipsize = (s, l, suffix = '...') => {
123
360
  if (s.length < l * 1.1) {
124
361
  return s;
@@ -128,12 +365,23 @@ export const ellipsize = (s, l, suffix = '...') => {
128
365
  }
129
366
  return s + suffix;
130
367
  };
368
+ /**
369
+ * Checks if value is a plain object
370
+ * @param obj - Value to check
371
+ * @returns True if value is a plain object
372
+ */
131
373
  export const isPojo = (obj) => {
132
374
  if (obj === null || typeof obj !== "object") {
133
375
  return false;
134
376
  }
135
377
  return Object.getPrototypeOf(obj) === Object.prototype;
136
378
  };
379
+ /**
380
+ * Deep equality comparison
381
+ * @param a - First value
382
+ * @param b - Second value
383
+ * @returns True if values are deeply equal
384
+ */
137
385
  export const equals = (a, b) => {
138
386
  if (a === b)
139
387
  return true;
@@ -177,24 +425,46 @@ export const equals = (a, b) => {
177
425
  return false;
178
426
  };
179
427
  // Curried utils
428
+ /** Returns a function that gets the nth element of an array */
180
429
  export const nth = (i) => (xs, ...args) => xs[i];
430
+ /** Returns a function that checks if nth element equals value */
181
431
  export const nthEq = (i, v) => (xs, ...args) => xs[i] === v;
432
+ /** Returns a function that checks if nth element does not equal value */
182
433
  export const nthNe = (i, v) => (xs, ...args) => xs[i] !== v;
434
+ /** Returns a function that checks equality with value */
183
435
  export const eq = (v) => (x) => x === v;
436
+ /** Returns a function that checks inequality with value */
184
437
  export const ne = (v) => (x) => x !== v;
438
+ /** Returns a function that gets property value from object */
185
439
  export const prop = (k) => (x) => x[k];
440
+ /** Returns a function that adds/updates property on object */
186
441
  export const assoc = (k, v) => (o) => ({ ...o, [k]: v });
442
+ /** Generates a hash string from input string */
187
443
  export const hash = (s) => Math.abs(s.split("").reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0)).toString();
188
444
  // Collections
445
+ /** Splits array into two parts at index */
189
446
  export const splitAt = (n, xs) => [xs.slice(0, n), xs.slice(n)];
447
+ /** Inserts element into array at index */
190
448
  export const insert = (n, x, xs) => [...xs.slice(0, n), x, ...xs.slice(n)];
449
+ /** Returns random element from array */
191
450
  export const choice = (xs) => xs[Math.floor(xs.length * Math.random())];
451
+ /** Returns shuffled copy of iterable */
192
452
  export const shuffle = (xs) => Array.from(xs).sort(() => Math.random() > 0.5 ? 1 : -1);
453
+ /** Returns n random elements from array */
193
454
  export const sample = (n, xs) => shuffle(xs).slice(0, n);
455
+ /** Checks if value is iterable */
194
456
  export const isIterable = (x) => Symbol.iterator in Object(x);
457
+ /** Ensures value is iterable by wrapping in array if needed */
195
458
  export const toIterable = (x) => isIterable(x) ? x : [x];
459
+ /** Ensures value is array by wrapping if needed */
196
460
  export const ensurePlural = (x) => (x instanceof Array ? x : [x]);
461
+ /** Converts string or number to number */
197
462
  export const ensureNumber = (x) => parseFloat(x);
463
+ /**
464
+ * Creates object from array of key-value pairs
465
+ * @param pairs - Array of [key, value] tuples
466
+ * @returns Object with keys and values from pairs
467
+ */
198
468
  export const fromPairs = (pairs) => {
199
469
  const r = {};
200
470
  for (const [k, v] of pairs) {
@@ -204,7 +474,18 @@ export const fromPairs = (pairs) => {
204
474
  }
205
475
  return r;
206
476
  };
477
+ /**
478
+ * Flattens array of arrays into single array
479
+ * @param xs - Array of arrays to flatten
480
+ * @returns Flattened array
481
+ */
207
482
  export const flatten = (xs) => xs.flatMap(identity);
483
+ /**
484
+ * Splits array into two arrays based on predicate
485
+ * @param f - Function to test elements
486
+ * @param xs - Array to partition
487
+ * @returns Tuple of [matching, non-matching] arrays
488
+ */
208
489
  export const partition = (f, xs) => {
209
490
  const a = [];
210
491
  const b = [];
@@ -218,7 +499,18 @@ export const partition = (f, xs) => {
218
499
  }
219
500
  return [a, b];
220
501
  };
502
+ /**
503
+ * Returns array with duplicate elements removed
504
+ * @param xs - Array with possible duplicates
505
+ * @returns Array with unique elements
506
+ */
221
507
  export const uniq = (xs) => Array.from(new Set(xs));
508
+ /**
509
+ * Returns array with elements unique by key function
510
+ * @param f - Function to generate key for each element
511
+ * @param xs - Input array
512
+ * @returns Array with elements unique by key
513
+ */
222
514
  export const uniqBy = (f, xs) => {
223
515
  const s = new Set();
224
516
  const r = [];
@@ -232,12 +524,29 @@ export const uniqBy = (f, xs) => {
232
524
  }
233
525
  return r;
234
526
  };
527
+ /**
528
+ * Returns sorted copy of array
529
+ * @param xs - Array to sort
530
+ * @returns New sorted array
531
+ */
235
532
  export const sort = (xs) => [...xs].sort();
533
+ /**
534
+ * Returns array sorted by key function
535
+ * @param f - Function to generate sort key
536
+ * @param xs - Array to sort
537
+ * @returns Sorted array
538
+ */
236
539
  export const sortBy = (f, xs) => [...xs].sort((a, b) => {
237
540
  const x = f(a);
238
541
  const y = f(b);
239
542
  return x < y ? -1 : x > y ? 1 : 0;
240
543
  });
544
+ /**
545
+ * Groups array elements by key function
546
+ * @param f - Function to generate group key
547
+ * @param xs - Array to group
548
+ * @returns Map of groups
549
+ */
241
550
  export const groupBy = (f, xs) => {
242
551
  const r = new Map();
243
552
  for (const x of xs) {
@@ -251,6 +560,12 @@ export const groupBy = (f, xs) => {
251
560
  }
252
561
  return r;
253
562
  };
563
+ /**
564
+ * Creates map from array using key function
565
+ * @param f - Function to generate key
566
+ * @param xs - Array to index
567
+ * @returns Map of values by key
568
+ */
254
569
  export const indexBy = (f, xs) => {
255
570
  const r = new Map();
256
571
  for (const x of xs) {
@@ -258,6 +573,12 @@ export const indexBy = (f, xs) => {
258
573
  }
259
574
  return r;
260
575
  };
576
+ /**
577
+ * Creates array of specified length using generator function
578
+ * @param n - Length of array
579
+ * @param f - Function to generate each element
580
+ * @returns Generated array
581
+ */
261
582
  export const initArray = (n, f) => {
262
583
  const result = [];
263
584
  for (let i = 0; i < n; i++) {
@@ -265,6 +586,12 @@ export const initArray = (n, f) => {
265
586
  }
266
587
  return result;
267
588
  };
589
+ /**
590
+ * Splits array into chunks of specified length
591
+ * @param chunkLength - Maximum length of each chunk
592
+ * @param xs - Array to split
593
+ * @returns Array of chunks
594
+ */
268
595
  export const chunk = (chunkLength, xs) => {
269
596
  const result = [];
270
597
  const current = [];
@@ -281,6 +608,12 @@ export const chunk = (chunkLength, xs) => {
281
608
  }
282
609
  return result;
283
610
  };
611
+ /**
612
+ * Splits array into specified number of chunks
613
+ * @param n - Number of chunks
614
+ * @param xs - Array to split
615
+ * @returns Array of n chunks
616
+ */
284
617
  export const chunks = (n, xs) => {
285
618
  const result = initArray(n, () => []);
286
619
  for (let i = 0; i < xs.length; i++) {
@@ -288,6 +621,11 @@ export const chunks = (n, xs) => {
288
621
  }
289
622
  return result;
290
623
  };
624
+ /**
625
+ * Creates function that only executes once
626
+ * @param f - Function to wrap
627
+ * @returns Function that executes f only on first call
628
+ */
291
629
  export const once = (f) => {
292
630
  let called = false;
293
631
  return (...args) => {
@@ -297,6 +635,11 @@ export const once = (f) => {
297
635
  }
298
636
  };
299
637
  };
638
+ /**
639
+ * Memoizes function results based on arguments
640
+ * @param f - Function to memoize
641
+ * @returns Memoized function
642
+ */
300
643
  export const memoize = (f) => {
301
644
  let prevArgs;
302
645
  let result;
@@ -308,6 +651,12 @@ export const memoize = (f) => {
308
651
  return result;
309
652
  };
310
653
  };
654
+ /**
655
+ * Creates throttled version of function
656
+ * @param ms - Minimum time between calls
657
+ * @param f - Function to throttle
658
+ * @returns Throttled function
659
+ */
311
660
  export const throttle = (ms, f) => {
312
661
  if (ms === 0) {
313
662
  return f;
@@ -332,6 +681,12 @@ export const throttle = (ms, f) => {
332
681
  }
333
682
  };
334
683
  };
684
+ /**
685
+ * Creates throttled function that returns cached value
686
+ * @param ms - Minimum time between updates
687
+ * @param f - Function to throttle
688
+ * @returns Function returning latest value
689
+ */
335
690
  export const throttleWithValue = (ms, f) => {
336
691
  let value;
337
692
  const update = throttle(ms, () => {
@@ -342,6 +697,12 @@ export const throttleWithValue = (ms, f) => {
342
697
  return value;
343
698
  };
344
699
  };
700
+ /**
701
+ * Creates batching function that collects items
702
+ * @param t - Time window for batching
703
+ * @param f - Function to process batch
704
+ * @returns Function that adds items to batch
705
+ */
345
706
  export const batch = (t, f) => {
346
707
  const xs = [];
347
708
  const cb = throttle(t, () => xs.length > 0 && f(xs.splice(0)));
@@ -350,6 +711,12 @@ export const batch = (t, f) => {
350
711
  cb();
351
712
  };
352
713
  };
714
+ /**
715
+ * Creates batching function that returns results
716
+ * @param t - Time window for batching
717
+ * @param execute - Function to process batch
718
+ * @returns Function that returns promise of result
719
+ */
353
720
  export const batcher = (t, execute) => {
354
721
  const queue = [];
355
722
  const _execute = async () => {
@@ -367,39 +734,99 @@ export const batcher = (t, execute) => {
367
734
  queue.push({ request, resolve });
368
735
  });
369
736
  };
737
+ /**
738
+ * Adds value to Set at key in object
739
+ * @param m - Object mapping keys to Sets
740
+ * @param k - Key to add to
741
+ * @param v - Value to add
742
+ */
370
743
  export const addToKey = (m, k, v) => {
371
744
  const s = m[k] || new Set();
372
745
  s.add(v);
373
746
  m[k] = s;
374
747
  };
748
+ /**
749
+ * Pushes value to array at key in object
750
+ * @param m - Object mapping keys to arrays
751
+ * @param k - Key to push to
752
+ * @param v - Value to push
753
+ */
375
754
  export const pushToKey = (m, k, v) => {
376
755
  const a = m[k] || [];
377
756
  a.push(v);
378
757
  m[k] = a;
379
758
  };
759
+ /**
760
+ * Adds value to Set at key in Map
761
+ * @param m - Map of Sets
762
+ * @param k - Key to add to
763
+ * @param v - Value to add
764
+ */
380
765
  export const addToMapKey = (m, k, v) => {
381
766
  const s = m.get(k) || new Set();
382
767
  s.add(v);
383
768
  m.set(k, s);
384
769
  };
770
+ /**
771
+ * Pushes value to array at key in Map
772
+ * @param m - Map of arrays
773
+ * @param k - Key to push to
774
+ * @param v - Value to push
775
+ */
385
776
  export const pushToMapKey = (m, k, v) => {
386
777
  const a = m.get(k) || [];
387
778
  a.push(v);
388
779
  m.set(k, a);
389
780
  };
781
+ /**
782
+ * Switches on key in object, with default fallback
783
+ * @param k - Key to look up
784
+ * @param m - Object with values and optional default
785
+ * @returns Value at key or default value
786
+ */
390
787
  export const switcher = (k, m) => m[k] === undefined ? m.default : m[k];
391
- // Time
788
+ /** One minute in seconds */
392
789
  export const MINUTE = 60;
790
+ /** One hour in seconds */
393
791
  export const HOUR = 60 * MINUTE;
792
+ /** One day in seconds */
394
793
  export const DAY = 24 * HOUR;
794
+ /** One week in seconds */
395
795
  export const WEEK = 7 * DAY;
796
+ /** One month in seconds (approximate) */
396
797
  export const MONTH = 30 * DAY;
798
+ /** One quarter in seconds (approximate) */
397
799
  export const QUARTER = 90 * DAY;
800
+ /** One year in seconds (approximate) */
398
801
  export const YEAR = 365 * DAY;
802
+ /**
803
+ * Multiplies time unit by count
804
+ * @param unit - Time unit in seconds
805
+ * @param count - Number of units
806
+ * @returns Total seconds
807
+ */
399
808
  export const int = (unit, count = 1) => unit * count;
809
+ /** Returns current Unix timestamp in seconds */
400
810
  export const now = () => Math.round(Date.now() / 1000);
811
+ /**
812
+ * Returns Unix timestamp from specified time ago
813
+ * @param unit - Time unit in seconds
814
+ * @param count - Number of units
815
+ * @returns Timestamp in seconds
816
+ */
401
817
  export const ago = (unit, count = 1) => now() - int(unit, count);
818
+ /**
819
+ * Converts seconds to milliseconds
820
+ * @param seconds - Time in seconds
821
+ * @returns Time in milliseconds
822
+ */
402
823
  export const ms = (seconds) => seconds * 1000;
824
+ /**
825
+ * Fetches JSON from URL with options
826
+ * @param url - URL to fetch from
827
+ * @param opts - Fetch options
828
+ * @returns Promise of parsed JSON response
829
+ */
403
830
  export const fetchJson = async (url, opts = {}) => {
404
831
  if (!opts.headers) {
405
832
  opts.headers = {};
@@ -409,6 +836,13 @@ export const fetchJson = async (url, opts = {}) => {
409
836
  const json = await res.json();
410
837
  return json;
411
838
  };
839
+ /**
840
+ * Posts JSON data to URL
841
+ * @param url - URL to post to
842
+ * @param data - Data to send
843
+ * @param opts - Additional fetch options
844
+ * @returns Promise of parsed JSON response
845
+ */
412
846
  export const postJson = async (url, data, opts = {}) => {
413
847
  if (!opts.method) {
414
848
  opts.method = "POST";
@@ -420,12 +854,28 @@ export const postJson = async (url, data, opts = {}) => {
420
854
  opts.body = JSON.stringify(data);
421
855
  return fetchJson(url, opts);
422
856
  };
423
- export const uploadFile = (url, fileObj) => {
857
+ /**
858
+ * Uploads file to URL
859
+ * @param url - Upload URL
860
+ * @param file - File to upload
861
+ * @returns Promise of parsed JSON response
862
+ */
863
+ export const uploadFile = (url, file) => {
424
864
  const body = new FormData();
425
- body.append("file", fileObj);
865
+ body.append("file", file);
426
866
  return fetchJson(url, { method: "POST", body });
427
867
  };
428
- // Random obscure stuff
429
- export const hexToBech32 = (prefix, url) => bech32.encode(prefix, bech32.toWords(utf8.decode(url)), false);
868
+ /**
869
+ * Converts hex string to bech32 format
870
+ * @param prefix - Bech32 prefix
871
+ * @param hex - Hex string to convert
872
+ * @returns Bech32 encoded string
873
+ */
874
+ export const hexToBech32 = (prefix, hex) => bech32.encode(prefix, bech32.toWords(utf8.decode(hex)), false);
875
+ /**
876
+ * Converts bech32 string to hex format
877
+ * @param b32 - Bech32 string to convert
878
+ * @returns Hex encoded string
879
+ */
430
880
  export const bech32ToHex = (b32) => utf8.encode(bech32.fromWords(bech32.decode(b32, false).words));
431
881
  //# sourceMappingURL=Tools.mjs.map