@welshman/lib 0.1.1 → 0.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.
Files changed (40) hide show
  1. package/LICENSE +21 -0
  2. package/{build/src → dist}/Deferred.d.ts +1 -0
  3. package/dist/Deferred.d.ts.map +1 -0
  4. package/dist/Deferred.js.map +1 -0
  5. package/{build/src → dist}/Emitter.d.ts +1 -0
  6. package/dist/Emitter.d.ts.map +1 -0
  7. package/dist/Emitter.js.map +1 -0
  8. package/{build/src → dist}/LRUCache.d.ts +1 -0
  9. package/dist/LRUCache.d.ts.map +1 -0
  10. package/dist/LRUCache.js.map +1 -0
  11. package/{build/src → dist}/TaskQueue.d.ts +3 -0
  12. package/dist/TaskQueue.d.ts.map +1 -0
  13. package/{build/src → dist}/TaskQueue.js +10 -0
  14. package/dist/TaskQueue.js.map +1 -0
  15. package/{build/src → dist}/Tools.d.ts +428 -338
  16. package/dist/Tools.d.ts.map +1 -0
  17. package/{build/src → dist}/Tools.js +716 -500
  18. package/dist/Tools.js.map +1 -0
  19. package/{build/src → dist}/index.d.ts +1 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/normalize-url/index.d.ts +286 -0
  23. package/dist/normalize-url/index.d.ts.map +1 -0
  24. package/{build/src → dist}/normalize-url/index.js +53 -51
  25. package/dist/normalize-url/index.js.map +1 -0
  26. package/package.json +14 -17
  27. package/README.md +0 -13
  28. package/build/src/Deferred.js.map +0 -1
  29. package/build/src/Emitter.js.map +0 -1
  30. package/build/src/LRUCache.js.map +0 -1
  31. package/build/src/TaskQueue.js.map +0 -1
  32. package/build/src/Tools.js.map +0 -1
  33. package/build/src/index.js.map +0 -1
  34. package/build/src/normalize-url/index.d.ts +0 -285
  35. package/build/src/normalize-url/index.js.map +0 -1
  36. package/build/tsconfig.tsbuildinfo +0 -1
  37. /package/{build/src → dist}/Deferred.js +0 -0
  38. /package/{build/src → dist}/Emitter.js +0 -0
  39. /package/{build/src → dist}/LRUCache.js +0 -0
  40. /package/{build/src → dist}/index.js +0 -0
@@ -1,38 +1,10 @@
1
- /** Type representing null or undefined */
1
+ type Obj<T = any> = Record<string, T>;
2
2
  export type Nil = null | undefined;
3
- /** Checks if a value is null or undefined */
4
- export declare const isNil: (x: any) => boolean;
5
- /** Type representing an optional value */
6
- export type Maybe<T> = T | undefined;
7
- /** Type that is shorthand for Record<string, T> */
8
- export type Obj<T = any> = Record<string, T>;
9
- /**
10
- * Executes a function if the value is defined
11
- * @param x - The value to check
12
- * @param f - Function to execute if x is defined
13
- * @returns Result of f(x) if x is defined, undefined otherwise
14
- */
15
- export declare const ifLet: <T>(x: T | undefined, f: (x: T) => void) => void;
3
+ export declare const isNil: <T>(x: T, ...args: unknown[]) => boolean;
4
+ export declare const isNotNil: <T>(x: T, ...args: unknown[]) => x is T & {};
5
+ export declare const assertNotNil: <T>(x: T, ...args: unknown[]) => NonNullable<T>;
16
6
  /** Function that does nothing and returns undefined */
17
7
  export declare const noop: (...args: unknown[]) => undefined;
18
- /**
19
- * Returns the first element of an array
20
- * @param xs - The array
21
- * @returns First element or undefined
22
- */
23
- export declare const first: <T>(xs: T[], ...args: unknown[]) => T;
24
- /**
25
- * Returns the first element of the first array in a nested array
26
- * @param xs - Array of arrays
27
- * @returns First element of first array or undefined
28
- */
29
- export declare const ffirst: <T>(xs: T[][], ...args: unknown[]) => T;
30
- /**
31
- * Returns the last element of an array
32
- * @param xs - The array
33
- * @returns Last element or undefined
34
- */
35
- export declare const last: <T>(xs: T[], ...args: unknown[]) => T;
36
8
  /**
37
9
  * Returns the input value unchanged
38
10
  * @param x - Any value
@@ -51,174 +23,194 @@ export declare const always: <T>(x: T, ...args: unknown[]) => () => T;
51
23
  * @returns !x
52
24
  */
53
25
  export declare const not: (x: any, ...args: unknown[]) => boolean;
54
- /** Returns a function that returns the boolean negation of the given function */
55
- export declare const complement: <T extends unknown[]>(f: (...args: T) => any) => (...args: T) => boolean;
56
- /** Converts a `Maybe<number>` to a number, defaulting to 0 */
57
- export declare const num: (x: Maybe<number>) => number;
26
+ /**
27
+ * Deep equality comparison
28
+ * @param a - First value
29
+ * @param b - Second value
30
+ * @returns True if values are deeply equal
31
+ */
32
+ export declare const equals: (a: any, b: any) => boolean;
33
+ /** Converts string or number to number */
34
+ export declare const ensureNumber: (x: number | string) => number;
35
+ /** Converts a `number | undefined` to a number, defaulting to 0 */
36
+ export declare const num: (x: number | undefined) => number;
58
37
  /** Adds two numbers, handling undefined values */
59
- export declare const add: (x: Maybe<number>, y: Maybe<number>) => number;
38
+ export declare const add: (x: number | undefined, y: number | undefined) => number;
60
39
  /** Subtracts two numbers, handling undefined values */
61
- export declare const sub: (x: Maybe<number>, y: Maybe<number>) => number;
40
+ export declare const sub: (x: number | undefined, y: number | undefined) => number;
62
41
  /** Multiplies two numbers, handling undefined values */
63
- export declare const mul: (x: Maybe<number>, y: Maybe<number>) => number;
42
+ export declare const mul: (x: number | undefined, y: number | undefined) => number;
64
43
  /** Divides two numbers, handling undefined values */
65
- export declare const div: (x: Maybe<number>, y: number) => number;
44
+ export declare const div: (x: number | undefined, y: number) => number;
66
45
  /** Increments a number by 1, handling undefined values */
67
- export declare const inc: (x: Maybe<number>) => number;
46
+ export declare const inc: (x: number | undefined) => number;
68
47
  /** Decrements a number by 1, handling undefined values */
69
- export declare const dec: (x: Maybe<number>) => number;
48
+ export declare const dec: (x: number | undefined) => number;
70
49
  /** Less than comparison, handling undefined values */
71
- export declare const lt: (x: Maybe<number>, y: Maybe<number>) => boolean;
50
+ export declare const lt: (x: number | undefined, y: number | undefined) => boolean;
72
51
  /** Less than or equal comparison, handling undefined values */
73
- export declare const lte: (x: Maybe<number>, y: Maybe<number>) => boolean;
52
+ export declare const lte: (x: number | undefined, y: number | undefined) => boolean;
74
53
  /** Greater than comparison, handling undefined values */
75
- export declare const gt: (x: Maybe<number>, y: Maybe<number>) => boolean;
54
+ export declare const gt: (x: number | undefined, y: number | undefined) => boolean;
76
55
  /** Greater than or equal comparison, handling undefined values */
77
- export declare const gte: (x: Maybe<number>, y: Maybe<number>) => boolean;
56
+ export declare const gte: (x: number | undefined, y: number | undefined) => boolean;
78
57
  /** Returns maximum value in array, handling undefined values */
79
- export declare const max: (xs: Maybe<number>[]) => number;
58
+ export declare const max: (xs: (number | undefined)[]) => number;
80
59
  /** Returns minimum value in array, handling undefined values */
81
- export declare const min: (xs: Maybe<number>[]) => number;
60
+ export declare const min: (xs: (number | undefined)[]) => number;
82
61
  /** Returns sum of array values, handling undefined values */
83
- export declare const sum: (xs: Maybe<number>[]) => number;
62
+ export declare const sum: (xs: (number | undefined)[]) => number;
84
63
  /** Returns average of array values, handling undefined values */
85
- export declare const avg: (xs: Maybe<number>[]) => number;
86
- /**
87
- * Returns array with first n elements removed
88
- * @param n - Number of elements to drop
89
- * @param xs - Input array
90
- * @returns Array with first n elements removed
91
- */
92
- export declare const drop: <T>(n: number, xs: T[]) => T[];
64
+ export declare const avg: (xs: (number | undefined)[]) => number;
93
65
  /**
94
- * Returns first n elements of array
95
- * @param n - Number of elements to take
96
- * @param xs - Input array
97
- * @returns Array of first n elements
66
+ * Checks if a number is between two values (exclusive)
67
+ * @param bounds - Lower and upper bounds
68
+ * @param n - Number to check
69
+ * @returns True if n is between low and high
98
70
  */
99
- export declare const take: <T>(n: number, xs: T[]) => T[];
71
+ export declare const between: ([low, high]: [number, number], n: number) => boolean;
100
72
  /**
101
- * Creates new object with specified keys removed
102
- * @param ks - Keys to remove
103
- * @param x - Source object
104
- * @returns New object without specified keys
73
+ * Checks if a number is between two values (inclusive)
74
+ * @param bounds - Lower and upper bounds
75
+ * @param n - Number to check
76
+ * @returns True if n is between low and high
105
77
  */
106
- export declare const omit: <T extends Obj>(ks: string[], x: T) => T;
78
+ export declare const within: ([low, high]: [number, number], n: number) => boolean;
107
79
  /**
108
- * Creates new object excluding entries with specified values
109
- * @param xs - Values to exclude
110
- * @param x - Source object
111
- * @returns New object without entries containing specified values
80
+ * Constrains number between min and max values
81
+ * @param bounds - Minimum and maximum allowed values
82
+ * @param n - Number to clamp
83
+ * @returns Clamped value
112
84
  */
113
- export declare const omitVals: <T extends Obj>(xs: any[], x: T) => T;
85
+ export declare const clamp: ([min, max]: [number, number], n: number) => number;
114
86
  /**
115
- * Creates new object with only specified keys
116
- * @param ks - Keys to keep
117
- * @param x - Source object
118
- * @returns New object with only specified keys
87
+ * Round a number to the nearest float precision
88
+ * @param precision - Number of decimal places
89
+ * @param x - Number to round
90
+ * @returns Formatted number
119
91
  */
120
- export declare const pick: <T extends Obj>(ks: string[], x: T) => T;
92
+ export declare const round: (precision: number, x: number) => number;
93
+ /** One minute in seconds */
94
+ export declare const MINUTE = 60;
95
+ /** One hour in seconds */
96
+ export declare const HOUR: number;
97
+ /** One day in seconds */
98
+ export declare const DAY: number;
99
+ /** One week in seconds */
100
+ export declare const WEEK: number;
101
+ /** One month in seconds (approximate) */
102
+ export declare const MONTH: number;
103
+ /** One quarter in seconds (approximate) */
104
+ export declare const QUARTER: number;
105
+ /** One year in seconds (approximate) */
106
+ export declare const YEAR: number;
107
+ /** User's default locale */
108
+ export declare const LOCALE: string;
109
+ /** User's default timezone */
110
+ export declare const TIMEZONE: string;
121
111
  /**
122
- * Generates sequence of numbers from a to b
123
- * @param a - Start number (inclusive)
124
- * @param b - End number (exclusive)
125
- * @param step - Increment between numbers
126
- * @yields Numbers in sequence
112
+ * Multiplies time unit by count
113
+ * @param unit - Time unit in seconds
114
+ * @param count - Number of units
115
+ * @returns Total seconds
127
116
  */
128
- export declare function range(a: number, b: number, step?: number): Generator<number, void, unknown>;
117
+ export declare const int: (unit: number, count?: number) => number;
118
+ /** Returns current Unix timestamp in seconds */
119
+ export declare const now: () => number;
129
120
  /**
130
- * Yields indexed items
131
- * @param items - A collection of items
132
- * @yields tuples of [index, item]
121
+ * Returns Unix timestamp from specified time ago
122
+ * @param unit - Time unit in seconds
123
+ * @param count - Number of units
124
+ * @returns Timestamp in seconds
133
125
  */
134
- export declare function enumerate<T>(items: T[]): Generator<[number, T], void, unknown>;
126
+ export declare const ago: (unit: number, count?: number) => number;
135
127
  /**
136
- * Creates new object with transformed keys
137
- * @param f - Function to transform keys
138
- * @param x - Source object
139
- * @returns Object with transformed keys
128
+ * Converts seconds to milliseconds
129
+ * @param seconds - Time in seconds
130
+ * @returns Time in milliseconds
140
131
  */
141
- export declare const mapKeys: <T extends Obj>(f: (v: string) => string, x: T) => T;
132
+ export declare const ms: (seconds: number) => number;
142
133
  /**
143
- * Creates new object with transformed values
144
- * @param f - Function to transform values
145
- * @param x - Source object
146
- * @returns Object with transformed values
134
+ * Converts seconds to date
135
+ * @param seconds - Time in seconds
136
+ * @returns Date object
147
137
  */
148
- export declare const mapVals: <V, U>(f: (v: V) => U, x: Record<string, V>) => Record<string, U>;
138
+ export declare const secondsToDate: (seconds: number) => Date;
149
139
  /**
150
- * Merges two objects, with left object taking precedence
151
- * @param a - Left object
152
- * @param b - Right object
153
- * @returns Merged object with a"s properties overriding b"s
140
+ * Converts date object to seconds
141
+ * @param date - Date object
142
+ * @returns timestamp in seconds
154
143
  */
155
- export declare const mergeLeft: <T extends Obj>(a: T, b: T) => T;
144
+ export declare const dateToSeconds: (date: Date) => number;
156
145
  /**
157
- * Merges two objects, with right object taking precedence
158
- * @param a - Left object
159
- * @param b - Right object
160
- * @returns Merged object with b"s properties overriding a"s
146
+ * Creates a local date from a date string
147
+ * @param dateString - date string
148
+ * @param timezone - timezone string
149
+ * @returns timezone-aware Date object
161
150
  */
162
- export declare const mergeRight: <T extends Obj>(a: T, b: T) => T;
163
- /** Deep merge two objects, prioritizing the first argument. */
164
- export declare const deepMergeLeft: (a: Obj, b: Obj) => Obj<any>;
165
- /** Deep merge two objects, prioritizing the second argument. */
166
- export declare const deepMergeRight: (a: Obj, b: Obj) => Obj<any>;
151
+ export declare const createLocalDate: (dateString: any, timezone?: string) => Date;
152
+ /** Formatter for date+time */
153
+ export declare const dateTimeFormatter: Intl.DateTimeFormat;
167
154
  /**
168
- * Checks if a number is between two values (exclusive)
169
- * @param bounds - Lower and upper bounds
170
- * @param n - Number to check
171
- * @returns True if n is between low and high
155
+ * Formats seconds as a datetime
156
+ * @param seconds - timestamp in seconds
157
+ * @returns datetime string
172
158
  */
173
- export declare const between: ([low, high]: [number, number], n: number) => boolean;
159
+ export declare const formatTimestamp: (seconds: number) => string;
160
+ /** Formatter for date */
161
+ export declare const dateFormatter: Intl.DateTimeFormat;
174
162
  /**
175
- * Checks if a number is between two values (inclusive)
176
- * @param bounds - Lower and upper bounds
177
- * @param n - Number to check
178
- * @returns True if n is between low and high
163
+ * Formats seconds as a date
164
+ * @param seconds - timestamp in seconds
165
+ * @returns date string
179
166
  */
180
- export declare const within: ([low, high]: [number, number], n: number) => boolean;
167
+ export declare const formatTimestampAsDate: (ts: number) => string;
168
+ /** Formatter for time */
169
+ export declare const timeFormatter: Intl.DateTimeFormat;
181
170
  /**
182
- * Generates random integer between min and max (inclusive)
183
- * @param min - Minimum value
184
- * @param max - Maximum value
185
- * @returns Random integer
171
+ * Formats seconds as a time
172
+ * @param seconds - timestamp in seconds
173
+ * @returns time string
186
174
  */
187
- export declare const randomInt: (min?: number, max?: number) => number;
175
+ export declare const formatTimestampAsTime: (ts: number) => string;
188
176
  /**
189
- * Generates random string ID
190
- * @returns Random string suitable for use as an ID
177
+ * Formats seconds as a relative date (x minutes ago)
178
+ * @param seconds - timestamp in seconds
179
+ * @returns relative date string
191
180
  */
192
- export declare const randomId: () => string;
181
+ export declare const formatTimestampRelative: (ts: number) => string;
193
182
  /**
194
- * Removes protocol (http://, https://, etc) from URL
195
- * @param url - URL to process
196
- * @returns URL without protocol
183
+ * Returns the first element of an array
184
+ * @param xs - The array
185
+ * @returns First element or undefined
197
186
  */
198
- export declare const stripProtocol: (url: string) => string;
187
+ export declare const first: <T>(xs: Iterable<T>, ...args: unknown[]) => T | undefined;
199
188
  /**
200
- * Formats URL for display by removing protocol, www, and trailing slash
201
- * @param url - URL to format
202
- * @returns Formatted URL
189
+ * Returns the first element of the first array in a nested array
190
+ * @param xs - Array of arrays
191
+ * @returns First element of first array or undefined
203
192
  */
204
- export declare const displayUrl: (url: string) => string;
193
+ export declare const ffirst: <T>(xs: Iterable<Iterable<T>>, ...args: unknown[]) => T | undefined;
205
194
  /**
206
- * Extracts and formats domain from URL
207
- * @param url - URL to process
208
- * @returns Formatted domain name
195
+ * Returns the last element of an array
196
+ * @param xs - The array
197
+ * @returns Last element or undefined
209
198
  */
210
- export declare const displayDomain: (url: string) => string;
199
+ export declare const last: <T>(xs: Iterable<T>, ...args: unknown[]) => T;
211
200
  /**
212
- * Creates a promise that resolves after specified time
213
- * @param t - Time in milliseconds
214
- * @returns Promise that resolves after t milliseconds
201
+ * Returns array with first n elements removed
202
+ * @param n - Number of elements to drop
203
+ * @param xs - Input array
204
+ * @returns Array with first n elements removed
215
205
  */
216
- export declare const sleep: (t: number) => Promise<unknown>;
206
+ export declare const drop: <T>(n: number, xs: Iterable<T>) => T[];
217
207
  /**
218
- * Creates a microtask that yields to other tasks in the event loop
219
- * @returns Promise that resolves after yielding
208
+ * Returns first n elements of array
209
+ * @param n - Number of elements to take
210
+ * @param xs - Input array
211
+ * @returns Array of first n elements
220
212
  */
221
- export declare const yieldThread: () => any;
213
+ export declare const take: <T>(n: number, xs: Iterable<T>) => T[];
222
214
  /**
223
215
  * Concatenates multiple arrays, filtering out null/undefined
224
216
  * @param xs - Arrays to concatenate
@@ -260,6 +252,13 @@ export declare const difference: <T>(a: T[], b: T[]) => T[];
260
252
  * @returns New array with element removed
261
253
  */
262
254
  export declare const remove: <T>(a: T, xs: T[]) => T[];
255
+ /**
256
+ * Removes element at index
257
+ * @param i - Index to remove
258
+ * @param xs - Source array
259
+ * @returns New array with element removed
260
+ */
261
+ export declare const removeAt: <T>(i: number, xs: T[]) => T[];
263
262
  /**
264
263
  * Returns elements from second array not present in first
265
264
  * @param a - Array of elements to exclude
@@ -275,137 +274,46 @@ export declare const without: <T>(a: T[], b: T[]) => T[];
275
274
  */
276
275
  export declare const toggle: <T>(x: T, xs: T[]) => T[];
277
276
  /**
278
- * Constrains number between min and max values
279
- * @param bounds - Minimum and maximum allowed values
280
- * @param n - Number to clamp
281
- * @returns Clamped value
277
+ * Generates sequence of numbers from a to b
278
+ * @param a - Start number (inclusive)
279
+ * @param b - End number (exclusive)
280
+ * @param step - Increment between numbers
281
+ * @yields Numbers in sequence
282
282
  */
283
- export declare const clamp: ([min, max]: [number, number], n: number) => number;
283
+ export declare function range(a: number, b: number, step?: number): Generator<number, void, unknown>;
284
284
  /**
285
- * Round a number to the nearest float precision
286
- * @param precision - Number of decimal places
287
- * @param x - Number to round
288
- * @returns Formatted number
285
+ * Yields indexed items
286
+ * @param items - A collection of items
287
+ * @yields tuples of [index, item]
289
288
  */
290
- export declare const round: (precision: number, x: number) => number;
289
+ export declare function enumerate<T>(items: T[]): Generator<[number, T], void, unknown>;
290
+ /** Returns a function that gets property value from object */
291
+ export declare const pluck: <T>(k: string, xs: Record<string, unknown>[]) => T[];
291
292
  /**
292
- * Safely parses JSON string
293
- * @param json - JSON string to parse
294
- * @returns Parsed object or null if invalid
293
+ * Creates object from array of key-value pairs
294
+ * @param pairs - Array of [key, value] tuples
295
+ * @returns Object with keys and values from pairs
295
296
  */
296
- export declare const parseJson: (json: string | Nil) => any;
297
+ export declare const fromPairs: <T>(pairs: [k?: string, v?: T, ...args: unknown[]][]) => Record<string, T>;
297
298
  /**
298
- * Gets and parses JSON from localStorage
299
- * @param k - Storage key
300
- * @returns Parsed value or undefined if invalid/missing
299
+ * Flattens array of arrays into single array
300
+ * @param xs - Array of arrays to flatten
301
+ * @returns Flattened array
301
302
  */
302
- export declare const getJson: (k: string) => any;
303
+ export declare const flatten: <T>(xs: T[][]) => T[];
303
304
  /**
304
- * Stringifies and stores value in localStorage
305
- * @param k - Storage key
306
- * @param v - Value to store
305
+ * Splits array into two arrays based on predicate
306
+ * @param f - Function to test elements
307
+ * @param xs - Array to partition
308
+ * @returns Tuple of [matching, non-matching] arrays
307
309
  */
308
- export declare const setJson: (k: string, v: any) => void;
310
+ export declare const partition: <T>(f: (x: T) => boolean, xs: T[]) => T[][];
309
311
  /**
310
- * Safely executes function and handles errors
311
- * @param f - Function to execute
312
- * @param onError - Optional error handler
313
- * @returns Function result or undefined if error
312
+ * Returns array with duplicate elements removed
313
+ * @param xs - Array with possible duplicates
314
+ * @returns Array with unique elements
314
315
  */
315
- export declare const tryCatch: <T>(f: () => T, onError?: (e: Error) => void) => T | undefined;
316
- /**
317
- * Truncates string to length, breaking at word boundaries
318
- * @param s - String to truncate
319
- * @param l - Maximum length
320
- * @param suffix - String to append if truncated
321
- * @returns Truncated string
322
- */
323
- export declare const ellipsize: (s: string, l: number, suffix?: string) => string;
324
- /**
325
- * Checks if value is a plain object
326
- * @param obj - Value to check
327
- * @returns True if value is a plain object
328
- */
329
- export declare const isPojo: (obj: any) => boolean;
330
- /**
331
- * Deep equality comparison
332
- * @param a - First value
333
- * @param b - Second value
334
- * @returns True if values are deeply equal
335
- */
336
- export declare const equals: (a: any, b: any) => boolean;
337
- /** Returns a function that gets the nth element of an array */
338
- export declare const nth: (i: number) => <T>(xs: T[], ...args: unknown[]) => T;
339
- /** Returns a function that checks if nth element equals value */
340
- export declare const nthEq: (i: number, v: any) => (xs: any[], ...args: unknown[]) => boolean;
341
- /** Returns a function that checks if nth element does not equal value */
342
- export declare const nthNe: (i: number, v: any) => (xs: any[], ...args: unknown[]) => boolean;
343
- /** Returns a function that checks if key/value pairs of x match all pairs in spec */
344
- export declare const spec: (values: Obj | Array<any>) => (x: Obj | Array<any>, ...args: unknown[]) => boolean;
345
- /** Returns a function that checks equality with value */
346
- export declare const eq: <T>(v: T) => (x: T) => boolean;
347
- /** Returns a function that checks inequality with value */
348
- export declare const ne: <T>(v: T) => (x: T) => boolean;
349
- /** Returns a function that gets property value from object */
350
- export declare const prop: <T>(k: string) => (x: Record<string, unknown>) => T;
351
- /** Returns a function that adds/updates a property on object */
352
- export declare const assoc: <K extends string, T, U>(k: K, v: T) => (o: U) => U & Record<K, T>;
353
- /** Returns a function that removes a property on object */
354
- export declare const dissoc: <K extends string, T extends Obj>(k: K) => (o: T) => T;
355
- /** Generates a hash string from input string */
356
- export declare const hash: (s: string) => string;
357
- /** Splits array into two parts at index */
358
- export declare const splitAt: <T>(n: number, xs: T[]) => T[][];
359
- /** Inserts element into array at index */
360
- export declare const insert: <T>(n: number, x: T, xs: T[]) => T[];
361
- /** Returns random element from array */
362
- export declare const choice: <T>(xs: T[]) => T;
363
- /** Returns shuffled copy of iterable */
364
- export declare const shuffle: <T>(xs: Iterable<T>) => T[];
365
- /** Returns n random elements from array */
366
- export declare const sample: <T>(n: number, xs: T[]) => T[];
367
- /** Checks if value is iterable */
368
- export declare const isIterable: (x: any) => boolean;
369
- /** Ensures value is iterable by wrapping in array if needed */
370
- export declare const toIterable: (x: any) => any;
371
- /** Ensures value is array by wrapping if needed */
372
- export declare const ensurePlural: <T>(x: T | T[]) => T[];
373
- /** Converts string or number to number */
374
- export declare const ensureNumber: (x: number | string) => number;
375
- /** Returns a function that gets property value from object */
376
- export declare const pluck: <T>(k: string, xs: Record<string, unknown>[]) => T[];
377
- /**
378
- * Creates object from array of key-value pairs
379
- * @param pairs - Array of [key, value] tuples
380
- * @returns Object with keys and values from pairs
381
- */
382
- export declare const fromPairs: <T>(pairs: [k?: string, v?: T, ...args: unknown[]][]) => Record<string, T>;
383
- /**
384
- * Filters object values based on predicate
385
- * @param f - Function to test values
386
- * @param x - Object to filter
387
- * @returns Object with only values that pass predicate
388
- */
389
- export declare const filterVals: <T extends Record<string, any>>(f: (v: any) => boolean, x: T) => T;
390
- /**
391
- * Flattens array of arrays into single array
392
- * @param xs - Array of arrays to flatten
393
- * @returns Flattened array
394
- */
395
- export declare const flatten: <T>(xs: T[][]) => T[];
396
- /**
397
- * Splits array into two arrays based on predicate
398
- * @param f - Function to test elements
399
- * @param xs - Array to partition
400
- * @returns Tuple of [matching, non-matching] arrays
401
- */
402
- export declare const partition: <T>(f: (x: T) => boolean, xs: T[]) => T[][];
403
- /**
404
- * Returns array with duplicate elements removed
405
- * @param xs - Array with possible duplicates
406
- * @returns Array with unique elements
407
- */
408
- export declare const uniq: <T>(xs: T[]) => T[];
316
+ export declare const uniq: <T>(xs: T[]) => T[];
409
317
  /**
410
318
  * Returns array with elements unique by key function
411
319
  * @param f - Function to generate key for each element
@@ -433,6 +341,13 @@ export declare const sortBy: <T>(f: (x: T) => any, xs: T[]) => T[];
433
341
  * @returns Map of groups
434
342
  */
435
343
  export declare const groupBy: <T, K>(f: (x: T) => K, xs: T[]) => Map<K, T[]>;
344
+ /**
345
+ * Counts array elements by key function
346
+ * @param f - Function to generate group key
347
+ * @param xs - Array to count entries
348
+ * @returns Map of counts
349
+ */
350
+ export declare const countBy: <T, K>(f: (x: T) => K, xs: T[]) => Map<K, number>;
436
351
  /**
437
352
  * Creates map from array using key function
438
353
  * @param f - Function to generate key
@@ -461,6 +376,108 @@ export declare const chunk: <T>(chunkLength: number, xs: T[]) => T[][];
461
376
  * @returns Array of n chunks
462
377
  */
463
378
  export declare const chunks: <T>(n: number, xs: T[]) => T[][];
379
+ /** Splits array into two parts at index */
380
+ export declare const splitAt: <T>(n: number, xs: T[]) => T[][];
381
+ /** Inserts element into array at index */
382
+ export declare const insertAt: <T>(n: number, x: T, xs: T[]) => T[];
383
+ /** Replaces array element at index */
384
+ export declare const replaceAt: <T>(n: number, x: T, xs: T[]) => T[];
385
+ /** Returns random element from array */
386
+ export declare const choice: <T>(xs: T[]) => T;
387
+ /** Returns shuffled copy of iterable */
388
+ export declare const shuffle: <T>(xs: Iterable<T>) => T[];
389
+ /** Returns n random elements from array */
390
+ export declare const sample: <T>(n: number, xs: T[]) => T[];
391
+ /** Checks if value is iterable */
392
+ export declare const isIterable: (x: any) => boolean;
393
+ /** Ensures value is iterable by wrapping in array if needed */
394
+ export declare const toIterable: (x: any) => any;
395
+ /** Ensures value is array by wrapping if needed */
396
+ export declare const ensurePlural: <T>(x: T | T[]) => T[];
397
+ /** Ensures values are not undefined */
398
+ export declare const removeNil: <T>(xs: T[]) => NonNullable<T & {}>[];
399
+ /**
400
+ * Checks if value is a plain object
401
+ * @param obj - Value to check
402
+ * @returns True if value is a plain object
403
+ */
404
+ export declare const isPojo: (obj: any) => boolean;
405
+ /**
406
+ * Creates new object with only specified keys
407
+ * @param ks - Keys to keep
408
+ * @param x - Source object
409
+ * @returns New object with only specified keys
410
+ */
411
+ export declare const pick: <T extends Obj>(ks: string[], x: T) => T;
412
+ /**
413
+ * Creates new object with specified keys removed
414
+ * @param ks - Keys to remove
415
+ * @param x - Source object
416
+ * @returns New object without specified keys
417
+ */
418
+ export declare const omit: <T extends Obj>(ks: string[], x: T) => T;
419
+ /**
420
+ * Creates new object excluding entries with specified values
421
+ * @param xs - Values to exclude
422
+ * @param x - Source object
423
+ * @returns New object without entries containing specified values
424
+ */
425
+ export declare const omitVals: <T extends Obj>(xs: any[], x: T) => T;
426
+ /**
427
+ * Filters object values based on predicate
428
+ * @param f - Function to test values
429
+ * @param x - Object to filter
430
+ * @returns Object with only values that pass predicate
431
+ */
432
+ export declare const filterVals: <T extends Record<string, any>>(f: (v: any) => boolean, x: T) => T;
433
+ /**
434
+ * Creates new object with transformed keys
435
+ * @param f - Function to transform keys
436
+ * @param x - Source object
437
+ * @returns Object with transformed keys
438
+ */
439
+ export declare const mapKeys: <T extends Obj>(f: (v: string) => string, x: T) => T;
440
+ /**
441
+ * Creates new object with transformed values
442
+ * @param f - Function to transform values
443
+ * @param x - Source object
444
+ * @returns Object with transformed values
445
+ */
446
+ export declare const mapVals: <V, U>(f: (v: V) => U, x: Record<string, V>) => Record<string, U>;
447
+ /**
448
+ * Merges two objects, with left object taking precedence
449
+ * @param a - Left object
450
+ * @param b - Right object
451
+ * @returns Merged object with a"s properties overriding b"s
452
+ */
453
+ export declare const mergeLeft: <T extends Obj>(a: T, b: T) => T;
454
+ /**
455
+ * Merges two objects, with right object taking precedence
456
+ * @param a - Left object
457
+ * @param b - Right object
458
+ * @returns Merged object with b"s properties overriding a"s
459
+ */
460
+ export declare const mergeRight: <T extends Obj>(a: T, b: T) => T;
461
+ /** Deep merge two objects, prioritizing the first argument. */
462
+ export declare const deepMergeLeft: (a: Obj, b: Obj) => Obj<any>;
463
+ /** Deep merge two objects, prioritizing the second argument. */
464
+ export declare const deepMergeRight: (a: Obj, b: Obj) => Obj<any>;
465
+ /**
466
+ * Switches on key in object, with default fallback
467
+ * @param k - Key to look up
468
+ * @param m - Object with values and optional default
469
+ * @returns Value at key or default value
470
+ */
471
+ export declare const switcher: <T>(k: string, m: Record<string, T>) => T;
472
+ /** Returns a function that returns the boolean negation of the given function */
473
+ export declare const complement: <T extends unknown[]>(f: (...args: T) => any) => (...args: T) => boolean;
474
+ /**
475
+ * Safely executes function and handles errors
476
+ * @param f - Function to execute
477
+ * @param onError - Optional error handler
478
+ * @returns Function result or undefined if error
479
+ */
480
+ export declare const tryCatch: <T>(f: () => T, onError?: (e: Error) => void) => T | undefined;
464
481
  /**
465
482
  * Creates function that only executes once
466
483
  * @param f - Function to wrap
@@ -479,6 +496,47 @@ export declare const call: <T>(f: () => T, ...args: unknown[]) => T;
479
496
  * @returns Memoized function
480
497
  */
481
498
  export declare const memoize: <T>(f: (...args: any[]) => T) => (...args: any[]) => T;
499
+ /**
500
+ * Executes a function if the value is defined
501
+ * @param x - The value to check
502
+ * @param f - Function to execute if x is defined
503
+ * @returns Result of f(x) if x is defined, undefined otherwise
504
+ */
505
+ export declare const ifLet: <T>(x: T | undefined, f: (x: T) => void) => void;
506
+ /**
507
+ * Generates random integer between min and max (inclusive)
508
+ * @param min - Minimum value
509
+ * @param max - Maximum value
510
+ * @returns Random integer
511
+ */
512
+ export declare const randomInt: (min?: number, max?: number) => number;
513
+ /**
514
+ * Generates random string ID
515
+ * @returns Random string suitable for use as an ID
516
+ */
517
+ export declare const randomId: () => string;
518
+ /**
519
+ * Creates a promise that resolves after specified time
520
+ * @param t - Time in milliseconds
521
+ * @returns Promise that resolves after t milliseconds
522
+ */
523
+ export declare const sleep: (t: number) => Promise<unknown>;
524
+ export type PollOptions = {
525
+ signal: AbortSignal;
526
+ condition: () => boolean;
527
+ interval?: number;
528
+ };
529
+ /**
530
+ * Creates a promise that resolves after the condition completes or timeout
531
+ * @param options - PollOptions
532
+ * @returns void Promise
533
+ */
534
+ export declare const poll: ({ interval, condition, signal }: PollOptions) => Promise<void>;
535
+ /**
536
+ * Creates a microtask that yields to other tasks in the event loop
537
+ * @returns Promise that resolves after yielding
538
+ */
539
+ export declare const yieldThread: () => any;
482
540
  /**
483
541
  * Creates throttled version of function
484
542
  * @param ms - Minimum time between calls
@@ -510,89 +568,48 @@ export declare const batch: <T>(t: number, f: (xs: T[]) => void) => (x: T) => vo
510
568
  */
511
569
  export declare const batcher: <T, U>(t: number, execute: (request: T[]) => U[] | Promise<U[]>) => (request: T) => Promise<U>;
512
570
  /**
513
- * Adds value to Set at key in object
514
- * @param m - Object mapping keys to Sets
515
- * @param k - Key to add to
516
- * @param v - Value to add
571
+ * Returns a promise that resolves after some proportion of promises complete
572
+ * @param threshold - number between 0 and 1 for how many promises to wait for
573
+ * @param promises - array of promises
574
+ * @returns promise
517
575
  */
518
- export declare const addToKey: <T>(m: Record<string, Set<T>>, k: string, v: T) => void;
576
+ export declare const race: (threshold: number, promises: Promise<unknown>[]) => Promise<void>;
519
577
  /**
520
- * Pushes value to array at key in object
521
- * @param m - Object mapping keys to arrays
522
- * @param k - Key to push to
523
- * @param v - Value to push
524
- */
525
- export declare const pushToKey: <T>(m: Record<string, T[]>, k: string, v: T) => void;
526
- /**
527
- * Adds value to Set at key in Map
528
- * @param m - Map of Sets
529
- * @param k - Key to add to
530
- * @param v - Value to add
531
- */
532
- export declare const addToMapKey: <K, T>(m: Map<K, Set<T>>, k: K, v: T) => void;
533
- /**
534
- * Pushes value to array at key in Map
535
- * @param m - Map of arrays
536
- * @param k - Key to push to
537
- * @param v - Value to push
578
+ * Removes protocol (http://, https://, etc) from URL
579
+ * @param url - URL to process
580
+ * @returns URL without protocol
538
581
  */
539
- export declare const pushToMapKey: <K, T>(m: Map<K, T[]>, k: K, v: T) => void;
582
+ export declare const stripProtocol: (url: string) => string;
540
583
  /**
541
- * A generic type-safe event listener function that auto-detects the appropriate methods
542
- * for adding and removing event listeners.
543
- *
544
- * @param target - The event target object with add/remove listener methods
545
- * @param eventName - The name of the event to listen for
546
- * @param callback - The callback function to execute when the event occurs
547
- * @returns A function that removes the event listener when called
584
+ * Formats URL for display by removing protocol, www, and trailing slash
585
+ * @param url - URL to format
586
+ * @returns Formatted URL
548
587
  */
549
- export declare const on: <EventName extends string, Args extends any[]>(target: {
550
- on: (event: EventName, handler: (...args: Args) => any, ...rest: any[]) => any;
551
- off: (event: EventName, handler: (...args: Args) => any, ...rest: any[]) => any;
552
- }, eventName: EventName, callback: (...args: Args) => void) => (() => void);
588
+ export declare const displayUrl: (url: string) => string;
553
589
  /**
554
- * Switches on key in object, with default fallback
555
- * @param k - Key to look up
556
- * @param m - Object with values and optional default
557
- * @returns Value at key or default value
590
+ * Extracts and formats domain from URL
591
+ * @param url - URL to process
592
+ * @returns Formatted domain name
558
593
  */
559
- export declare const switcher: <T>(k: string, m: Record<string, T>) => T;
560
- /** One minute in seconds */
561
- export declare const MINUTE = 60;
562
- /** One hour in seconds */
563
- export declare const HOUR: number;
564
- /** One day in seconds */
565
- export declare const DAY: number;
566
- /** One week in seconds */
567
- export declare const WEEK: number;
568
- /** One month in seconds (approximate) */
569
- export declare const MONTH: number;
570
- /** One quarter in seconds (approximate) */
571
- export declare const QUARTER: number;
572
- /** One year in seconds (approximate) */
573
- export declare const YEAR: number;
594
+ export declare const displayDomain: (url: string) => string;
574
595
  /**
575
- * Multiplies time unit by count
576
- * @param unit - Time unit in seconds
577
- * @param count - Number of units
578
- * @returns Total seconds
596
+ * Safely parses JSON string
597
+ * @param json - JSON string to parse
598
+ * @returns Parsed object or null if invalid
579
599
  */
580
- export declare const int: (unit: number, count?: number) => number;
581
- /** Returns current Unix timestamp in seconds */
582
- export declare const now: () => number;
600
+ export declare const parseJson: (json: string | undefined) => any;
583
601
  /**
584
- * Returns Unix timestamp from specified time ago
585
- * @param unit - Time unit in seconds
586
- * @param count - Number of units
587
- * @returns Timestamp in seconds
602
+ * Gets and parses JSON from localStorage
603
+ * @param k - Storage key
604
+ * @returns Parsed value or undefined if invalid/missing
588
605
  */
589
- export declare const ago: (unit: number, count?: number) => number;
606
+ export declare const getJson: (k: string) => any;
590
607
  /**
591
- * Converts seconds to milliseconds
592
- * @param seconds - Time in seconds
593
- * @returns Time in milliseconds
608
+ * Stringifies and stores value in localStorage
609
+ * @param k - Storage key
610
+ * @param v - Value to store
594
611
  */
595
- export declare const ms: (seconds: number) => number;
612
+ export declare const setJson: (k: string, v: any) => void;
596
613
  /** Options for fetch requests */
597
614
  type FetchOpts = {
598
615
  method?: string;
@@ -621,6 +638,78 @@ export declare const postJson: <T>(url: string, data: T, opts?: FetchOpts) => Pr
621
638
  * @returns Promise of parsed JSON response
622
639
  */
623
640
  export declare const uploadFile: (url: string, file: File) => Promise<any>;
641
+ /**
642
+ * A generic type-safe event listener function that works with event emitters.
643
+ *
644
+ * @param target - The event target object with add/remove listener methods
645
+ * @param eventName - The name of the event to listen for
646
+ * @param callback - The callback function to execute when the event occurs
647
+ * @returns A function that removes the event listener when called
648
+ */
649
+ export declare const on: <EventMap extends Record<string | symbol, any[]>, E extends keyof EventMap>(target: {
650
+ on(event: E, listener: (...args: EventMap[E]) => any): any;
651
+ off(event: E, listener: (...args: EventMap[E]) => any): any;
652
+ }, eventName: E, callback: (...args: EventMap[E]) => void) => (() => void);
653
+ /**
654
+ * Truncates string to length, breaking at word boundaries
655
+ * @param s - String to truncate
656
+ * @param l - Maximum length
657
+ * @param suffix - String to append if truncated
658
+ * @returns Truncated string
659
+ */
660
+ export declare const ellipsize: (s: string, l: number, suffix?: string) => string;
661
+ /** Displays a list of items with oxford commas and a chosen conjunction */
662
+ export declare const displayList: <T>(xs: T[], conj?: string, n?: number) => string;
663
+ /** Generates a hash string from input string */
664
+ export declare const hash: (s: string) => string;
665
+ /** Returns a function that gets the nth element of an array */
666
+ export declare const nth: (i: number) => <T>(xs: T[], ...args: unknown[]) => T;
667
+ /** Returns a function that checks if nth element equals value */
668
+ export declare const nthEq: (i: number, v: any) => (xs: any[], ...args: unknown[]) => boolean;
669
+ /** Returns a function that checks if nth element does not equal value */
670
+ export declare const nthNe: (i: number, v: any) => (xs: any[], ...args: unknown[]) => boolean;
671
+ /** Returns a function that checks if key/value pairs of x match all pairs in spec */
672
+ export declare const spec: (values: Obj | Array<any>) => (x: Obj | Array<any>, ...args: unknown[]) => boolean;
673
+ /** Returns a function that checks equality with value */
674
+ export declare const eq: <T>(v: T) => (x: T, ...args: unknown[]) => boolean;
675
+ /** Returns a function that checks inequality with value */
676
+ export declare const ne: <T>(v: T) => (x: T, ...args: unknown[]) => boolean;
677
+ /** Returns a function that gets property value from object */
678
+ export declare const prop: <T>(k: string) => (x: Record<string, unknown>) => T;
679
+ /** Returns a function that adds/updates a property on object */
680
+ export declare const assoc: <K extends string, T, U>(k: K, v: T) => (o: U) => U & Record<K, T>;
681
+ /** Returns a function that removes a property on object */
682
+ export declare const dissoc: <K extends string, T extends Obj>(k: K) => (o: T) => T;
683
+ /** Returns a function that checks whether a value is in the given sequence */
684
+ export declare const member: <T>(xs: Iterable<T>) => (x: T) => boolean;
685
+ /**
686
+ * Adds value to Set at key in object
687
+ * @param m - Object mapping keys to Sets
688
+ * @param k - Key to add to
689
+ * @param v - Value to add
690
+ */
691
+ export declare const addToKey: <T>(m: Record<string, Set<T>>, k: string, v: T) => void;
692
+ /**
693
+ * Pushes value to array at key in object
694
+ * @param m - Object mapping keys to arrays
695
+ * @param k - Key to push to
696
+ * @param v - Value to push
697
+ */
698
+ export declare const pushToKey: <T>(m: Record<string, T[]>, k: string, v: T) => void;
699
+ /**
700
+ * Adds value to Set at key in Map
701
+ * @param m - Map of Sets
702
+ * @param k - Key to add to
703
+ * @param v - Value to add
704
+ */
705
+ export declare const addToMapKey: <K, T>(m: Map<K, Set<T>>, k: K, v: T) => void;
706
+ /**
707
+ * Pushes value to array at key in Map
708
+ * @param m - Map of arrays
709
+ * @param k - Key to push to
710
+ * @param v - Value to push
711
+ */
712
+ export declare const pushToMapKey: <K, T>(m: Map<K, T[]>, k: K, v: T) => void;
624
713
  /**
625
714
  * Converts hex string to bech32 format
626
715
  * @param prefix - Bech32 prefix
@@ -635,3 +724,4 @@ export declare const hexToBech32: (prefix: string, hex: string) => `${Lowercase<
635
724
  */
636
725
  export declare const bech32ToHex: (b32: string) => string;
637
726
  export {};
727
+ //# sourceMappingURL=Tools.d.ts.map