@dereekb/util 13.15.0 → 13.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/eslint/index.cjs.js +1 -1
- package/eslint/index.esm.js +1 -1
- package/eslint/package.json +2 -2
- package/fetch/index.cjs.js +15 -15
- package/fetch/index.esm.js +15 -15
- package/fetch/package.json +2 -2
- package/index.cjs.js +204 -87
- package/index.esm.js +200 -88
- package/package.json +1 -1
- package/src/lib/hash.d.ts +22 -0
- package/src/lib/string/string.d.ts +85 -0
- package/test/index.cjs.js +6 -6
- package/test/index.esm.js +6 -6
- package/test/package.json +2 -2
package/package.json
CHANGED
package/src/lib/hash.d.ts
CHANGED
|
@@ -58,3 +58,25 @@ export declare function makeHashDecodeMap(decodeValues: string[], hashFn: (value
|
|
|
58
58
|
* @returns Plaintext recovered via `decodeMap`, dropping any hash missing from it.
|
|
59
59
|
*/
|
|
60
60
|
export declare function decodeHashedValuesWithDecodeMap(hashedValues: string[], decodeMap: HashDecodeMap): string[];
|
|
61
|
+
/**
|
|
62
|
+
* Computes a stable, non-negative 32-bit integer hash for the input string using the FNV-1a algorithm.
|
|
63
|
+
*
|
|
64
|
+
* Deterministic and dependency-free (no `Math.random`): the same input always yields the same value,
|
|
65
|
+
* making it suitable for deterministically mapping a string onto a fixed-size set (e.g. picking a
|
|
66
|
+
* curated color for a name via `hashStringToNumber(value) % colors.length`).
|
|
67
|
+
*
|
|
68
|
+
* @param value - String to hash.
|
|
69
|
+
* @returns A non-negative integer in the range `[0, 2^32)`.
|
|
70
|
+
*
|
|
71
|
+
* @dbxUtil
|
|
72
|
+
* @dbxUtilCategory hash
|
|
73
|
+
* @dbxUtilTags hash, string, number, deterministic, fnv, bucket, index
|
|
74
|
+
* @dbxUtilRelated decode-hashed-values
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* hashStringToNumber('Michelle B'); // stable integer, same every call
|
|
79
|
+
* hashStringToNumber('Michelle B') % 12; // deterministic bucket index 0-11
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare function hashStringToNumber(value: string): number;
|
|
@@ -247,6 +247,91 @@ export type FirstNameLastNameTuple = [string, Maybe<string>];
|
|
|
247
247
|
* @dbxUtilRelated split-join-remainder
|
|
248
248
|
*/
|
|
249
249
|
export declare function splitJoinNameString(input: string): FirstNameLastNameTuple;
|
|
250
|
+
/**
|
|
251
|
+
* Default maximum number of initials produced by a {@link NameToInitialsFunction}.
|
|
252
|
+
*/
|
|
253
|
+
export declare const DEFAULT_NAME_TO_INITIALS_MAX_INITIALS = 2;
|
|
254
|
+
/**
|
|
255
|
+
* Default minimum number of initials produced by a {@link NameToInitialsFunction}.
|
|
256
|
+
*/
|
|
257
|
+
export declare const DEFAULT_NAME_TO_INITIALS_MIN_INITIALS = 1;
|
|
258
|
+
/**
|
|
259
|
+
* Configuration for creating a {@link NameToInitialsFunction}.
|
|
260
|
+
*/
|
|
261
|
+
export interface NameToInitialsConfig {
|
|
262
|
+
/**
|
|
263
|
+
* Maximum number of initials to produce.
|
|
264
|
+
*
|
|
265
|
+
* Caps the number of leading words used for multi-word inputs and the number of leading characters used for single-word inputs. Defaults to {@link DEFAULT_NAME_TO_INITIALS_MAX_INITIALS}.
|
|
266
|
+
*/
|
|
267
|
+
readonly maxInitials?: Maybe<number>;
|
|
268
|
+
/**
|
|
269
|
+
* Minimum number of initials to produce.
|
|
270
|
+
*
|
|
271
|
+
* For a single-word input this many leading characters are taken (capped by `maxInitials` and the word's length). Defaults to {@link DEFAULT_NAME_TO_INITIALS_MIN_INITIALS}.
|
|
272
|
+
*/
|
|
273
|
+
readonly minInitials?: Maybe<number>;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Derives display initials from a name or short character string.
|
|
277
|
+
*
|
|
278
|
+
* Multi-word inputs use the first character of each of the first `maxInitials` words; single-word
|
|
279
|
+
* inputs use the first `minInitials` characters verbatim. The result is always uppercased.
|
|
280
|
+
*/
|
|
281
|
+
export type NameToInitialsFunction = (name: string) => string;
|
|
282
|
+
/**
|
|
283
|
+
* Creates a {@link NameToInitialsFunction} that derives display initials from a name or short character string.
|
|
284
|
+
*
|
|
285
|
+
* Useful for avatar fallbacks where a name (e.g. `'Michelle B'`) or a literal token (e.g. `'BB'`)
|
|
286
|
+
* should collapse to a compact label. By default a single-word input yields a single initial; raise
|
|
287
|
+
* `minInitials` to pull more leading characters from a lone word.
|
|
288
|
+
*
|
|
289
|
+
* @param config - Configuration controlling the minimum and maximum number of initials.
|
|
290
|
+
* @returns A reusable function that derives initials from input names.
|
|
291
|
+
*
|
|
292
|
+
* @dbxUtil
|
|
293
|
+
* @dbxUtilCategory string
|
|
294
|
+
* @dbxUtilKind factory
|
|
295
|
+
* @dbxUtilTags string, name, initials, avatar, abbreviate, person, factory
|
|
296
|
+
* @dbxUtilRelated split-join-name-string, capitalize-first-letter
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```ts
|
|
300
|
+
* const toInitials = nameToInitialsFactory();
|
|
301
|
+
* toInitials('Michelle B'); // 'MB'
|
|
302
|
+
* toInitials('Michelle'); // 'M'
|
|
303
|
+
*
|
|
304
|
+
* const toPaddedInitials = nameToInitialsFactory({ minInitials: 2 });
|
|
305
|
+
* toPaddedInitials('Michelle'); // 'MI'
|
|
306
|
+
* toPaddedInitials('BB'); // 'BB'
|
|
307
|
+
* ```
|
|
308
|
+
*
|
|
309
|
+
* @__NO_SIDE_EFFECTS__
|
|
310
|
+
*/
|
|
311
|
+
export declare function nameToInitialsFactory(config?: Maybe<NameToInitialsConfig>): NameToInitialsFunction;
|
|
312
|
+
/**
|
|
313
|
+
* Derives display initials from a name or short character string using the default configuration.
|
|
314
|
+
*
|
|
315
|
+
* Multi-word inputs use the first character of each of the first two words; single-word inputs use
|
|
316
|
+
* the first character verbatim. The result is always uppercased.
|
|
317
|
+
*
|
|
318
|
+
* @param name - Name or character string to derive initials from.
|
|
319
|
+
* @returns Uppercased initials, or an empty string when the input is blank.
|
|
320
|
+
*
|
|
321
|
+
* @dbxUtil
|
|
322
|
+
* @dbxUtilCategory string
|
|
323
|
+
* @dbxUtilTags string, name, initials, avatar, abbreviate, person
|
|
324
|
+
* @dbxUtilRelated name-to-initials-factory, split-join-name-string, capitalize-first-letter
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```ts
|
|
328
|
+
* nameToInitials('Michelle B'); // 'MB'
|
|
329
|
+
* nameToInitials('A'); // 'A'
|
|
330
|
+
* nameToInitials('BB'); // 'B'
|
|
331
|
+
* nameToInitials('Michelle'); // 'M'
|
|
332
|
+
* ```
|
|
333
|
+
*/
|
|
334
|
+
export declare const nameToInitials: NameToInitialsFunction;
|
|
250
335
|
/**
|
|
251
336
|
* Creates a string that repeats the given string a specified number of times.
|
|
252
337
|
*
|
package/test/index.cjs.js
CHANGED
|
@@ -223,10 +223,10 @@ function _ts_generator$2(thisArg, body) {
|
|
|
223
223
|
* @param e - The error to pass to the callback; defaults to a generic error.
|
|
224
224
|
*/ function failWithTestDoneCallback(done) {
|
|
225
225
|
var e = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : new Error('failed test');
|
|
226
|
-
if (done.fail
|
|
227
|
-
done.fail(e);
|
|
228
|
-
} else {
|
|
226
|
+
if (done.fail == null) {
|
|
229
227
|
done(e);
|
|
228
|
+
} else {
|
|
229
|
+
done.fail(e);
|
|
230
230
|
}
|
|
231
231
|
}
|
|
232
232
|
/**
|
|
@@ -1314,10 +1314,10 @@ function expectSuccessfulFail(errorFn) {
|
|
|
1314
1314
|
return _async_to_generator(function() {
|
|
1315
1315
|
var _testDoneCallbackRef, done, _promise, result;
|
|
1316
1316
|
function handleError(e) {
|
|
1317
|
-
if (
|
|
1318
|
-
failWithTestDoneCallback(done, e);
|
|
1319
|
-
} else {
|
|
1317
|
+
if (_instanceof(e, ExpectedFailError)) {
|
|
1320
1318
|
done();
|
|
1319
|
+
} else {
|
|
1320
|
+
failWithTestDoneCallback(done, e);
|
|
1321
1321
|
}
|
|
1322
1322
|
}
|
|
1323
1323
|
return _ts_generator(this, function(_state) {
|
package/test/index.esm.js
CHANGED
|
@@ -221,10 +221,10 @@ function _ts_generator$2(thisArg, body) {
|
|
|
221
221
|
* @param e - The error to pass to the callback; defaults to a generic error.
|
|
222
222
|
*/ function failWithTestDoneCallback(done) {
|
|
223
223
|
var e = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : new Error('failed test');
|
|
224
|
-
if (done.fail
|
|
225
|
-
done.fail(e);
|
|
226
|
-
} else {
|
|
224
|
+
if (done.fail == null) {
|
|
227
225
|
done(e);
|
|
226
|
+
} else {
|
|
227
|
+
done.fail(e);
|
|
228
228
|
}
|
|
229
229
|
}
|
|
230
230
|
/**
|
|
@@ -1312,10 +1312,10 @@ function expectSuccessfulFail(errorFn) {
|
|
|
1312
1312
|
return _async_to_generator(function() {
|
|
1313
1313
|
var _testDoneCallbackRef, done, _promise, result;
|
|
1314
1314
|
function handleError(e) {
|
|
1315
|
-
if (
|
|
1316
|
-
failWithTestDoneCallback(done, e);
|
|
1317
|
-
} else {
|
|
1315
|
+
if (_instanceof(e, ExpectedFailError)) {
|
|
1318
1316
|
done();
|
|
1317
|
+
} else {
|
|
1318
|
+
failWithTestDoneCallback(done, e);
|
|
1319
1319
|
}
|
|
1320
1320
|
}
|
|
1321
1321
|
return _ts_generator(this, function(_state) {
|