@dereekb/util 13.15.0 → 13.17.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dereekb/util",
3
- "version": "13.15.0",
3
+ "version": "13.17.0",
4
4
  "sideEffects": false,
5
5
  "exports": {
6
6
  "./test": {
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/src/lib/type.d.ts CHANGED
@@ -4,6 +4,51 @@ import { type Maybe } from './value/maybe.type';
4
4
  * Boolean, string or number value.
5
5
  */
6
6
  export type PrimativeValue = boolean | string | number;
7
+ /**
8
+ * Phantom key used to nominally brand a primitive type.
9
+ *
10
+ * Declared (never defined) so it exists only in the type system and carries no
11
+ * runtime cost. Two `Brand<number, A>` / `Brand<number, B>` types are distinct
12
+ * whenever `A !== B`, which is what gives branded ids/keys their nominal identity
13
+ * despite sharing the same underlying primitive at runtime.
14
+ */
15
+ export declare const BRAND: unique symbol;
16
+ /**
17
+ * Nominally brands a primitive `TValue` with the compile-time-only tag `TBrand`.
18
+ *
19
+ * The brand is a zero-cost nominal type: the `[BRAND]` phantom key is erased at
20
+ * runtime, so a `Brand<number, 'UserId'>` is just a `number` once compiled, but
21
+ * the compiler refuses to mix it with a `Brand<number, 'PostId'>` or with a bare
22
+ * `number`. Use it to stop two structurally identical primitives (two id spaces,
23
+ * a raw count vs. a currency amount, ciphertext vs. plaintext, ...) from being
24
+ * accidentally interchanged.
25
+ *
26
+ * Branded values are minted at a trusted edge — a parser, a decoder, or an `as`
27
+ * assertion inside a constructor function — and then flow through the rest of the
28
+ * code as the branded type, so the "is this the right kind of value?" check is
29
+ * paid once rather than at every call site.
30
+ *
31
+ * @typeParam TValue - The underlying runtime representation (e.g. `number`, `string`).
32
+ * @typeParam TBrand - A unique string tag distinguishing this brand from others.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * type UserId = Brand<number, 'UserId'>;
37
+ * type PostId = Brand<number, 'PostId'>;
38
+ *
39
+ * function loadUser(id: UserId): User { ... }
40
+ *
41
+ * const userId = 10 as UserId;
42
+ * const postId = 10 as PostId;
43
+ *
44
+ * loadUser(userId); // ok
45
+ * loadUser(postId); // compile error: PostId is not assignable to UserId
46
+ * loadUser(10); // compile error: number is not assignable to UserId
47
+ * ```
48
+ */
49
+ export type Brand<TValue, TBrand extends string> = TValue & {
50
+ readonly [BRAND]: TBrand;
51
+ };
7
52
  /**
8
53
  * Open-ended union of known string literals `T` plus an arbitrary string fallback.
9
54
  *
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 != null) {
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 (!_instanceof(e, ExpectedFailError)) {
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 != null) {
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 (!_instanceof(e, ExpectedFailError)) {
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) {
package/test/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@dereekb/util/test",
3
- "version": "13.15.0",
3
+ "version": "13.17.0",
4
4
  "peerDependencies": {
5
- "@dereekb/util": "13.15.0",
5
+ "@dereekb/util": "13.17.0",
6
6
  "make-error": "^1.3.6"
7
7
  },
8
8
  "exports": {