@dereekb/util 13.0.7 → 13.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 (143) hide show
  1. package/fetch/package.json +2 -2
  2. package/index.cjs.js +4107 -1516
  3. package/index.esm.js +4107 -1516
  4. package/package.json +1 -2
  5. package/src/lib/array/array.d.ts +92 -47
  6. package/src/lib/array/array.find.d.ts +14 -18
  7. package/src/lib/array/array.index.d.ts +43 -14
  8. package/src/lib/array/array.indexed.d.ts +66 -17
  9. package/src/lib/array/array.limit.d.ts +11 -0
  10. package/src/lib/array/array.make.d.ts +9 -3
  11. package/src/lib/array/array.map.d.ts +20 -8
  12. package/src/lib/array/array.number.d.ts +63 -5
  13. package/src/lib/array/array.random.d.ts +12 -9
  14. package/src/lib/array/array.set.d.ts +24 -1
  15. package/src/lib/array/array.string.d.ts +104 -0
  16. package/src/lib/array/array.unique.d.ts +60 -8
  17. package/src/lib/array/array.value.d.ts +20 -11
  18. package/src/lib/assertion/assertion.d.ts +30 -2
  19. package/src/lib/assertion/assertion.generic.d.ts +8 -0
  20. package/src/lib/assertion/assertion.number.d.ts +16 -0
  21. package/src/lib/auth/auth.role.claims.d.ts +14 -8
  22. package/src/lib/boolean.d.ts +54 -1
  23. package/src/lib/contact/domain.d.ts +22 -4
  24. package/src/lib/contact/random.d.ts +24 -0
  25. package/src/lib/date/date.d.ts +8 -13
  26. package/src/lib/date/hour.d.ts +51 -32
  27. package/src/lib/date/minute.d.ts +4 -4
  28. package/src/lib/date/time.d.ts +12 -3
  29. package/src/lib/date/week.d.ts +77 -10
  30. package/src/lib/error/error.d.ts +54 -3
  31. package/src/lib/error/error.server.d.ts +25 -2
  32. package/src/lib/filter/filter.d.ts +12 -7
  33. package/src/lib/function/function.boolean.d.ts +4 -4
  34. package/src/lib/function/function.d.ts +2 -5
  35. package/src/lib/function/function.forward.d.ts +19 -3
  36. package/src/lib/getter/getter.cache.d.ts +9 -5
  37. package/src/lib/getter/getter.d.ts +39 -22
  38. package/src/lib/getter/getter.map.d.ts +10 -4
  39. package/src/lib/getter/getter.util.d.ts +3 -3
  40. package/src/lib/grouping.d.ts +98 -31
  41. package/src/lib/hash.d.ts +15 -8
  42. package/src/lib/iterable/iterable.d.ts +62 -50
  43. package/src/lib/iterable/iterable.map.d.ts +4 -4
  44. package/src/lib/iterate.d.ts +17 -1
  45. package/src/lib/key.d.ts +32 -10
  46. package/src/lib/lifecycle.d.ts +10 -1
  47. package/src/lib/map/map.d.ts +17 -16
  48. package/src/lib/map/map.intersection.d.ts +5 -4
  49. package/src/lib/map/map.key.d.ts +16 -15
  50. package/src/lib/misc/host.d.ts +6 -3
  51. package/src/lib/model/id.batch.d.ts +7 -3
  52. package/src/lib/model/id.factory.d.ts +16 -1
  53. package/src/lib/model/model.conversion.d.ts +49 -6
  54. package/src/lib/model/model.conversion.field.d.ts +13 -3
  55. package/src/lib/model/model.copy.d.ts +11 -0
  56. package/src/lib/model/model.d.ts +174 -1
  57. package/src/lib/model/model.modify.d.ts +24 -5
  58. package/src/lib/nodejs/stream.d.ts +8 -6
  59. package/src/lib/number/bitwise.dencoder.d.ts +43 -3
  60. package/src/lib/number/bound.d.ts +34 -5
  61. package/src/lib/number/dollar.d.ts +13 -6
  62. package/src/lib/number/factory.d.ts +11 -3
  63. package/src/lib/number/number.d.ts +55 -32
  64. package/src/lib/number/random.d.ts +13 -3
  65. package/src/lib/number/round.d.ts +47 -26
  66. package/src/lib/number/sort.d.ts +7 -1
  67. package/src/lib/number/transform.d.ts +16 -0
  68. package/src/lib/object/object.array.d.ts +16 -2
  69. package/src/lib/object/object.array.delta.d.ts +6 -2
  70. package/src/lib/object/object.d.ts +33 -5
  71. package/src/lib/object/object.empty.d.ts +4 -2
  72. package/src/lib/object/object.equal.d.ts +21 -4
  73. package/src/lib/object/object.filter.tuple.d.ts +105 -3
  74. package/src/lib/object/object.key.d.ts +13 -6
  75. package/src/lib/object/object.map.d.ts +30 -19
  76. package/src/lib/page/page.d.ts +21 -0
  77. package/src/lib/page/page.filter.d.ts +16 -9
  78. package/src/lib/path/path.d.ts +101 -14
  79. package/src/lib/path/path.tree.d.ts +16 -0
  80. package/src/lib/promise/callback.d.ts +18 -0
  81. package/src/lib/promise/is.d.ts +10 -3
  82. package/src/lib/promise/map.d.ts +5 -3
  83. package/src/lib/promise/poll.d.ts +14 -4
  84. package/src/lib/promise/promise.d.ts +84 -18
  85. package/src/lib/promise/promise.factory.d.ts +18 -2
  86. package/src/lib/promise/promise.limit.d.ts +22 -2
  87. package/src/lib/promise/promise.loop.d.ts +69 -1
  88. package/src/lib/promise/promise.ref.d.ts +14 -2
  89. package/src/lib/promise/promise.task.d.ts +8 -4
  90. package/src/lib/promise/promise.type.d.ts +4 -4
  91. package/src/lib/promise/use.d.ts +6 -4
  92. package/src/lib/promise/wait.d.ts +5 -2
  93. package/src/lib/relation/relation.d.ts +98 -0
  94. package/src/lib/service/handler.config.d.ts +47 -9
  95. package/src/lib/service/handler.d.ts +43 -1
  96. package/src/lib/service/typed.service.d.ts +21 -2
  97. package/src/lib/set/set.allowed.d.ts +6 -4
  98. package/src/lib/set/set.d.ts +174 -46
  99. package/src/lib/set/set.decision.d.ts +4 -2
  100. package/src/lib/set/set.delta.d.ts +16 -3
  101. package/src/lib/set/set.hashset.d.ts +43 -0
  102. package/src/lib/set/set.selection.d.ts +7 -3
  103. package/src/lib/sort.d.ts +36 -16
  104. package/src/lib/string/char.d.ts +48 -34
  105. package/src/lib/string/dencoder.d.ts +84 -13
  106. package/src/lib/string/factory.d.ts +13 -4
  107. package/src/lib/string/html.d.ts +7 -6
  108. package/src/lib/string/mimetype.d.ts +65 -1
  109. package/src/lib/string/prefix.d.ts +21 -7
  110. package/src/lib/string/record.d.ts +7 -4
  111. package/src/lib/string/replace.d.ts +77 -33
  112. package/src/lib/string/search.d.ts +19 -9
  113. package/src/lib/string/sort.d.ts +10 -1
  114. package/src/lib/string/string.d.ts +74 -21
  115. package/src/lib/string/transform.d.ts +7 -0
  116. package/src/lib/string/tree.d.ts +91 -29
  117. package/src/lib/string/url.d.ts +148 -63
  118. package/src/lib/tree/tree.explore.d.ts +84 -17
  119. package/src/lib/tree/tree.flatten.d.ts +44 -7
  120. package/src/lib/type.d.ts +15 -12
  121. package/src/lib/value/address.d.ts +66 -17
  122. package/src/lib/value/bound.d.ts +253 -17
  123. package/src/lib/value/build.d.ts +30 -5
  124. package/src/lib/value/comparator.d.ts +53 -25
  125. package/src/lib/value/cron.d.ts +14 -6
  126. package/src/lib/value/decision.d.ts +57 -13
  127. package/src/lib/value/equal.d.ts +44 -13
  128. package/src/lib/value/indexed.d.ts +253 -82
  129. package/src/lib/value/label.d.ts +16 -5
  130. package/src/lib/value/map.d.ts +77 -20
  131. package/src/lib/value/maybe.d.ts +47 -49
  132. package/src/lib/value/modifier.d.ts +82 -27
  133. package/src/lib/value/point.d.ts +248 -54
  134. package/src/lib/value/url.d.ts +10 -3
  135. package/src/lib/value/use.d.ts +99 -8
  136. package/src/lib/value/vector.d.ts +71 -9
  137. package/test/index.cjs.js +86 -23
  138. package/test/index.esm.js +86 -23
  139. package/test/package.json +2 -2
  140. package/test/src/lib/shared/shared.d.ts +31 -5
  141. package/test/src/lib/shared/shared.fail.d.ts +72 -9
  142. package/test/src/lib/shared/shared.function.d.ts +40 -2
  143. package/test/src/lib/shared/shared.wrap.d.ts +20 -1
@@ -10,18 +10,38 @@ export type ServerErrorResponseData = object;
10
10
  export interface ServerError<T = ServerErrorResponseData> extends ReadableDataError<T> {
11
11
  readonly status: number;
12
12
  }
13
+ /**
14
+ * Type guard that checks if the input is a ServerError (has both status and code properties).
15
+ *
16
+ * @param input - The value to check
17
+ * @returns True if the input is a ServerError
18
+ */
13
19
  export declare function isServerError(input: unknown): input is ServerError;
20
+ /**
21
+ * Union type for either a plain error message string or a partial server error object.
22
+ */
14
23
  export type ErrorMessageOrPartialServerError<T = ServerErrorResponseData> = string | Partial<ReadableDataError | ServerError<T>>;
15
24
  /**
16
- * Converts the input to a Partial ServerError
25
+ * Normalizes a string or partial error into a Partial ServerError object.
26
+ * If the input is a string, it becomes the message property.
17
27
  *
18
- * @param message
28
+ * @param messageOrError - A string message or partial server error object
29
+ * @returns A partial ServerError object
19
30
  */
20
31
  export declare function partialServerError<T = ServerErrorResponseData>(message: string): Partial<ServerError<T>>;
21
32
  export declare function partialServerError<T = ServerErrorResponseData>(serverError: Partial<ReadableDataError | ServerError<T>>): Partial<ServerError<T>>;
22
33
  export declare function partialServerError<T = ServerErrorResponseData>(messageOrError: Maybe<ErrorMessageOrPartialServerError<T>>): Partial<ServerError<T>>;
34
+ /**
35
+ * Configuration for creating a ServerError, combining ServerError and optional CodedError properties.
36
+ */
23
37
  export interface ServerErrorMakeConfig<T> extends ServerError<T>, Partial<CodedError> {
24
38
  }
39
+ /**
40
+ * Creates a ServerError from the given configuration.
41
+ *
42
+ * @param config - The server error configuration
43
+ * @returns A ServerError object
44
+ */
25
45
  export declare function serverError<T>(config: ServerErrorMakeConfig<T>): ServerError<T>;
26
46
  /**
27
47
  * Base server-error class.
@@ -33,6 +53,9 @@ export declare class ServerErrorResponse<T extends ServerErrorResponseData = Ser
33
53
  readonly data?: T;
34
54
  constructor({ code, status, data, message }: ServerError<T>);
35
55
  }
56
+ /**
57
+ * Server error response with a 401 Unauthorized status.
58
+ */
36
59
  export declare class UnauthorizedServerErrorResponse extends ServerErrorResponse {
37
60
  constructor({ code, data, message }: Partial<ServerError>);
38
61
  }
@@ -5,23 +5,28 @@ import { type Maybe } from '../value/maybe.type';
5
5
  export interface Filter<F> {
6
6
  filter?: F;
7
7
  }
8
+ /**
9
+ * A filter reference where the filter is optional.
10
+ */
8
11
  export type OptionalFilter<F> = Partial<Filter<F>>;
9
12
  /**
10
13
  * Function used for filtering items that takes in a value and index.
11
14
  */
12
15
  export type FilterFunction<T = unknown> = (value: T, index: number) => boolean;
13
16
  /**
14
- * Merges the input FilterFunction values into a single FilterFunction.
17
+ * Merges multiple FilterFunction values into a single FilterFunction.
18
+ * The merged function returns true only if all individual filters pass (AND logic).
19
+ * Null/undefined filters are ignored.
15
20
  *
16
- * @param inputFilters
17
- * @returns
21
+ * @param inputFilters - The filter functions to merge
22
+ * @returns A single FilterFunction that applies all filters
18
23
  */
19
24
  export declare function mergeFilterFunctions<T>(...inputFilters: Maybe<FilterFunction<T>>[]): FilterFunction<T>;
20
25
  /**
21
- * Used to invert a filter function by returning the opposite of what it returns.
26
+ * Inverts a filter function so it returns the opposite boolean value.
22
27
  *
23
- * @param filterFn
24
- * @param invert whether or not to apply the inversion.
25
- * @returns
28
+ * @param filterFn - The filter function to invert
29
+ * @param invert - Whether to apply the inversion (defaults to true)
30
+ * @returns The inverted filter function, or the original if invert is false
26
31
  */
27
32
  export declare const invertFilter: <T = unknown, F extends FilterFunction<T> = FilterFunction<T>>(filterFn: F, invert?: boolean) => F;
@@ -3,10 +3,10 @@
3
3
  */
4
4
  export type BooleanReturnFunction = (...args: any[]) => boolean;
5
5
  /**
6
- * Inverts the output of an arbitrary boolean function.
6
+ * Inverts the output of an arbitrary boolean-returning function.
7
7
  *
8
- * @param decisionFn
9
- * @param invert
10
- * @returns
8
+ * @param decisionFn - The function whose boolean return value to invert
9
+ * @param invert - Whether to apply the inversion (defaults to true)
10
+ * @returns The inverted function, or the original if invert is false
11
11
  */
12
12
  export declare function invertBooleanReturnFunction<F extends BooleanReturnFunction>(decisionFn: F, invert?: boolean): F;
@@ -1,9 +1,6 @@
1
1
  /**
2
- * A function that returns the input value.
2
+ * Identity function that returns the input value unchanged.
3
3
  *
4
- * Is an alias of the mapIdentityFunction, so it will return true when passed to isMapIdentityFunction().
5
- *
6
- * @param input
7
- * @returns
4
+ * Alias of MAP_IDENTITY, so `isMapIdentityFunction()` will return true for this function.
8
5
  */
9
6
  export declare const passThrough: <T>(input: T) => T;
@@ -1,12 +1,28 @@
1
1
  import { type Getter } from '../getter/getter';
2
2
  import { type Maybe } from '../value/maybe.type';
3
+ /**
4
+ * A function type that forwards its call to another function retrieved lazily.
5
+ */
3
6
  export type ForwardFunction<I extends (...args: any[]) => O, O = unknown> = I;
4
7
  /**
5
- * Wraps a Getter that returns a function. When the function is invoked, the getter retrieves the function then calls it with the input arguments.
8
+ * Wraps a Getter that returns a function. When the returned function is invoked,
9
+ * it retrieves the target function from the getter and calls it with the provided arguments.
6
10
  *
7
- * @param getter
8
- * @returns
11
+ * Useful for late-binding or circular dependency resolution.
12
+ *
13
+ * @param getter - A Getter that provides the target function
14
+ * @returns A forwarding function with the same signature as the target
9
15
  */
10
16
  export declare function forwardFunction<I extends (...args: any[]) => O, O = unknown>(getter: Getter<I>): ForwardFunction<I>;
17
+ /**
18
+ * Factory that wraps an optional function with a forwarding function, falling back to a default.
19
+ */
11
20
  export type DefaultForwardFunctionFactory<I extends (...args: any[]) => O, O = unknown> = (fn: Maybe<I>) => ForwardFunction<I, O>;
21
+ /**
22
+ * Creates a factory that produces forwarding functions which use the provided function
23
+ * or fall back to the default function when not provided.
24
+ *
25
+ * @param defaultFn - The default function to use as fallback
26
+ * @returns A factory that wraps optional functions with a default fallback
27
+ */
12
28
  export declare function defaultForwardFunctionFactory<I extends (...args: any[]) => O, O = unknown>(defaultFn: I): DefaultForwardFunctionFactory<I, O>;
@@ -18,6 +18,10 @@ export type CachedGetter<T> = Getter<T> & {
18
18
  */
19
19
  init(): void;
20
20
  };
21
+ /**
22
+ * A cached factory that stores the result of the first call and returns it on subsequent calls.
23
+ * Supports optional input arguments for the initial factory call.
24
+ */
21
25
  export type CachedFactoryWithInput<T, A = unknown> = CachedGetter<T> & FactoryWithInput<T, A> & {
22
26
  /**
23
27
  * Re-initializes the cache using the factory function.
@@ -27,12 +31,12 @@ export type CachedFactoryWithInput<T, A = unknown> = CachedGetter<T> & FactoryWi
27
31
  init(input?: A): void;
28
32
  };
29
33
  /**
30
- * Creates a CachedGetter from the input Getter.
31
- *
32
- * The value will be retrieved once, then cached permenantly by this function.
34
+ * Creates a CachedGetter from the input factory function.
35
+ * The value is retrieved once on first call and cached permanently.
36
+ * Use `reset()` to clear the cache and `init()` to reload.
33
37
  *
34
- * @param getter
35
- * @returns
38
+ * @param factory - The factory function to cache
39
+ * @returns A CachedFactoryWithInput that caches the first result
36
40
  */
37
41
  export declare function cachedGetter<T>(getter: Getter<T>): CachedFactoryWithInput<T>;
38
42
  export declare function cachedGetter<T, A = unknown>(factory: FactoryWithInput<T, A>): CachedFactoryWithInput<T, A>;
@@ -45,17 +45,17 @@ export type GetterDistinctValue = boolean | string | number | object | symbol |
45
45
  export type GetterOrValueWithInput<T extends GetterDistinctValue, A> = GetterOrValue<T> | FactoryWithInput<T, A>;
46
46
  export type StringOrGetter = GetterOrValue<string>;
47
47
  /**
48
- * Returns true if the input object looks like a Getter (is a function).
48
+ * Returns true if the input value is a non-class function (i.e., likely a Getter).
49
49
  *
50
- * @param value
51
- * @returns
50
+ * @param value - The value to check
51
+ * @returns True if the value is a non-class function
52
52
  */
53
53
  export declare function isGetter<T = unknown>(value: unknown): value is Getter<T>;
54
54
  /**
55
- * If the input is a function, it is executed. Otherwise, the value is returned.
55
+ * If the input is a function, it is executed and the result returned. Otherwise, the value itself is returned.
56
56
  *
57
- * @param input
58
- * @returns
57
+ * @param input - A value or a getter/factory function
58
+ * @returns The resolved value
59
59
  */
60
60
  export declare function getValueFromGetter<T>(input: GetterOrValue<T>): T;
61
61
  export declare function getValueFromGetter<T>(this: unknown, input: GetterOrValue<T>): T;
@@ -63,10 +63,10 @@ export declare function getValueFromGetter<T, A>(this: unknown, input: FactoryWi
63
63
  export declare function getValueFromGetter<T, A>(this: unknown, input: GetterOrFactoryWithInput<T, A>, args?: A): T;
64
64
  export declare function getValueFromGetter<T extends GetterDistinctValue, A>(this: unknown, input: GetterOrValueWithInput<T, A>, args?: A): T;
65
65
  /**
66
- * Returns the input as a getter.
66
+ * Wraps the input as a Getter function. If it's already a function, returns it directly.
67
67
  *
68
- * @param input
69
- * @returns
68
+ * @param input - A value or getter function
69
+ * @returns A Getter function that returns the value
70
70
  */
71
71
  export declare function asGetter<T>(input: GetterOrValue<T>): Getter<T>;
72
72
  /**
@@ -74,38 +74,55 @@ export declare function asGetter<T>(input: GetterOrValue<T>): Getter<T>;
74
74
  */
75
75
  export type ObjectCopyFactory<T> = Factory<T>;
76
76
  /**
77
- * Creates a getter from the input value that returns a copy of that value.
77
+ * Creates a factory that returns a shallow copy of the input value on each call.
78
78
  *
79
- * @param value
79
+ * @param value - The object to copy
80
+ * @param copyFunction - Optional custom copy function (defaults to copyObject)
81
+ * @returns A factory that produces copies of the value
80
82
  */
81
83
  export declare function objectCopyFactory<T extends object>(value: T, copyFunction?: CopyObjectFunction<T>): ObjectCopyFactory<T>;
82
84
  /**
83
- * Returns a getter that will copy any input object values to a new object.
85
+ * Converts the input to an ObjectCopyFactory. If the input is an object, wraps it with objectCopyFactory.
86
+ * If it's already a function (Getter), it's returned directly.
84
87
  *
85
- * Any input Getters are considered ObjectCopyFactory values and passed through directly.
86
- *
87
- * @param input
88
- * @returns
88
+ * @param input - An object value or a getter function
89
+ * @param copyFunction - Optional custom copy function
90
+ * @returns An ObjectCopyFactory for the input
89
91
  */
90
92
  export declare function asObjectCopyFactory<T>(input: T | ObjectCopyFactory<T>, copyFunction?: CopyObjectFunction<T>): ObjectCopyFactory<T>;
91
93
  /**
92
- * Wraps the input and returns a Getter for that value.
94
+ * Wraps the input value in a Getter function that always returns it.
93
95
  *
94
- * @param input
95
- * @returns
96
+ * @param input - The value to wrap
97
+ * @returns A Getter that returns the input value
96
98
  */
97
99
  export declare function makeGetter<T>(input: T): Getter<T>;
98
100
  /**
99
101
  * A factory that can take in an index input optionally.
100
102
  */
101
103
  export type FactoryWithIndex<T> = FactoryWithInput<T, number> | FactoryWithRequiredInput<T, number>;
104
+ /**
105
+ * Calls a factory function the specified number of times and returns the results as an array.
106
+ *
107
+ * @param factory - The factory function to call (receives the current index as argument)
108
+ * @param count - The number of items to create
109
+ * @returns An array of produced values
110
+ */
102
111
  export declare function makeWithFactory<T>(factory: Factory<T> | FactoryWithIndex<T>, count: number): T[];
112
+ /**
113
+ * Maps an array of inputs through a factory function to produce an array of outputs.
114
+ *
115
+ * @param factory - The factory function to call with each input
116
+ * @param input - The array of inputs to pass to the factory
117
+ * @returns An array of produced values
118
+ */
103
119
  export declare function makeWithFactoryInput<T, A>(factory: FactoryWithInput<T, A>, input: Maybe<A>[]): T[];
104
120
  export declare function makeWithFactoryInput<T, A>(factory: FactoryWithRequiredInput<T, A>, input: A[]): T[];
105
121
  /**
106
- * Wraps the factory so that when executed no arguments are passed to the factory.
122
+ * Wraps a factory so that no arguments are forwarded when it's called.
123
+ * Useful for protecting a factory from accidentally receiving arguments.
107
124
  *
108
- * @param factory
109
- * @returns
125
+ * @param factory - The factory to wrap
126
+ * @returns A new factory that calls the original with no arguments
110
127
  */
111
128
  export declare function protectedFactory<T>(factory: Factory<T>): Factory<T>;
@@ -1,15 +1,21 @@
1
1
  import { type MapFunction } from '../value/map';
2
2
  import { type Getter } from './getter';
3
+ /**
4
+ * Factory that transforms a Getter of type I into a Getter of type O.
5
+ */
3
6
  export type MapGetterFactory<I, O> = (input: Getter<I>) => Getter<O>;
4
7
  /**
5
- * Maps the input getter.
8
+ * Creates a new Getter that applies a mapping function to the result of the input Getter.
6
9
  *
7
- * @param input
10
+ * @param input - The source Getter
11
+ * @param mapFn - The mapping function to apply to the getter's value
12
+ * @returns A new Getter that returns the mapped value
8
13
  */
9
14
  export declare function mapGetter<I, O>(input: Getter<I>, mapFn: MapFunction<I, O>): Getter<O>;
10
15
  /**
11
- * Creates a MapGetter
16
+ * Creates a factory that wraps Getters with a mapping function.
12
17
  *
13
- * @param input
18
+ * @param mapFn - The mapping function to apply
19
+ * @returns A factory that transforms Getters of type I to Getters of type O
14
20
  */
15
21
  export declare function mapGetterFactory<I, O>(mapFn: MapFunction<I, O>): MapGetterFactory<I, O>;
@@ -4,9 +4,9 @@ import { type Factory } from './getter';
4
4
  */
5
5
  export type RandomFromArrayFactory<T> = Factory<T>;
6
6
  /**
7
- * Makes a RandomFromArrayFactory
7
+ * Creates a factory that returns a random element from the given array on each call.
8
8
  *
9
- * @param config
10
- * @returns
9
+ * @param values - The array of values to randomly select from
10
+ * @returns A factory that returns a random element from the array
11
11
  */
12
12
  export declare function randomFromArrayFactory<T>(values: T[]): RandomFromArrayFactory<T>;
@@ -3,22 +3,39 @@ import { type IndexRef } from './value/indexed';
3
3
  import { type EqualityComparatorFunction } from './value/comparator';
4
4
  import { type DecisionFunction } from './value/decision';
5
5
  import { type Maybe } from './value/maybe.type';
6
+ /**
7
+ * Result of separating values into two groups based on an inclusion check.
8
+ */
6
9
  export interface SeparateResult<T> {
7
10
  included: T[];
8
11
  excluded: T[];
9
12
  }
13
+ /**
14
+ * A plain object where each key maps to an array of grouped values.
15
+ */
10
16
  export interface GroupingResult<T> {
11
17
  [key: string]: T[];
12
18
  }
19
+ /**
20
+ * A typed grouping result where keys are constrained to the keys of a known object type.
21
+ */
13
22
  export type KeyedGroupingResult<T, O> = {
14
23
  [K in keyof O]: T[];
15
24
  };
25
+ /**
26
+ * Result of pairing values by their key. Values that share a key form a pair; values with unique keys are unpaired.
27
+ */
16
28
  export interface PairsGroupingResult<T> {
17
29
  pairs: T[][];
18
30
  unpaired: T[];
19
31
  }
32
+ /**
33
+ * Configuration for comparing two arrays to determine if their contents differ.
34
+ */
20
35
  export interface ArrayContentsDifferentParams<T, K extends PrimativeKey = PrimativeKey> {
36
+ /** Extracts a unique key from each item for pairing items across arrays. */
21
37
  groupKeyFn: ReadKeyFunction<T, K>;
38
+ /** Compares two paired items for equality. */
22
39
  isEqual: EqualityComparatorFunction<T>;
23
40
  }
24
41
  /**
@@ -37,13 +54,23 @@ export interface RestoreOrderParams<T, K extends number | string = number | stri
37
54
  */
38
55
  excludeNewItems?: boolean;
39
56
  }
57
+ /**
58
+ * An array batch that carries its zero-based batch index via {@link IndexRef}.
59
+ */
40
60
  export type IndexedBatch<T> = T[] & Readonly<IndexRef>;
41
61
  /**
42
- * Batches items from the input array into several batches of a maximum size.
62
+ * Splits the input array into batches of a maximum size. Each batch carries its zero-based index as `.i`.
63
+ *
64
+ * @param input - The array to split into batches.
65
+ * @param batchSize - Maximum number of items per batch.
66
+ * @returns An array of {@link IndexedBatch} arrays.
43
67
  *
44
- * @param array
45
- * @param batchSize
46
- * @returns
68
+ * @example
69
+ * ```ts
70
+ * const result = batch(['a', 'b', 'c', 'd'], 2);
71
+ * // result[0] => ['a', 'b'], result[0].i => 0
72
+ * // result[1] => ['c', 'd'], result[1].i => 1
73
+ * ```
47
74
  */
48
75
  export declare function batch<T>(input: T[], batchSize: number): IndexedBatch<T>[];
49
76
  export interface BatchCount {
@@ -68,67 +95,107 @@ export interface BatchCalc extends BatchCount {
68
95
  remainder: number;
69
96
  }
70
97
  /**
71
- * Calculates a BatchCount given the input.
98
+ * Calculates batch metrics (count, full batches, remainder) from a {@link BatchCount} configuration.
72
99
  *
73
- * @param input
74
- * @returns
100
+ * @param input - The total items and items-per-batch configuration.
101
+ * @returns A {@link BatchCalc} with computed batch counts and remainder.
75
102
  */
76
103
  export declare function batchCalc(input: BatchCount): BatchCalc;
104
+ /**
105
+ * Returns how many items are in the batch at the given index, accounting for a possible smaller remainder batch at the end.
106
+ *
107
+ * @param index - Zero-based batch index.
108
+ * @param calc - Pre-computed batch calculation from {@link batchCalc}.
109
+ * @returns The number of items in that batch.
110
+ */
77
111
  export declare function itemCountForBatchIndex(index: number, calc: BatchCalc): number;
78
112
  /**
79
- * Convenience function for calling restoreOrder with two arrays of values, instead of an array of keys and array of values.
113
+ * Convenience wrapper for {@link restoreOrder} that derives order keys from a reference array of values
114
+ * instead of requiring a separate keys array.
80
115
  *
81
- * @param orderValues
82
- * @param values
83
- * @param params
84
- * @returns
116
+ * @param orderValues - Values whose keys define the desired order.
117
+ * @param values - Values to reorder.
118
+ * @param params - Configuration including the key-reading function.
119
+ * @returns The reordered values array.
85
120
  */
86
121
  export declare function restoreOrderWithValues<T, K extends PrimativeKey = PrimativeKey>(orderValues: T[], values: T[], params: RestoreOrderParams<T, K>): T[];
87
122
  /**
88
- * Restores the order to the input values based on their keys.
123
+ * Reorders values to match a reference key ordering. Values not present in the order keys
124
+ * are appended at the end (unless `excludeNewItems` is true). Duplicates are resolved via
125
+ * the `chooseRetainedValue` function (defaults to keeping the first).
89
126
  *
90
- * Duplicate values are passed to the chooseRetainedValue function past. When no function is provided, duplicates are ignored.
127
+ * @param orderKeys - Keys defining the desired order.
128
+ * @param values - Values to reorder.
129
+ * @param params - Configuration including key reader, duplicate handling, and new-item behavior.
130
+ * @returns The reordered values array.
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * const items = [{ key: 'a' }, { key: 'b' }, { key: 'c' }];
135
+ * const order = ['c', 'a', 'b'];
136
+ * restoreOrder(order, items, { readKey: (x) => x.key });
137
+ * // [{ key: 'c' }, { key: 'a' }, { key: 'b' }]
138
+ * ```
91
139
  */
92
140
  export declare function restoreOrder<T, K extends PrimativeKey = PrimativeKey>(orderKeys: K[], values: T[], { readKey, chooseRetainedValue, excludeNewItems }: RestoreOrderParams<T, K>): T[];
93
141
  /**
94
- * Returns true if the input differs from eachother.
142
+ * Compares two arrays by pairing items with matching keys and checking equality.
143
+ * Returns `true` if lengths differ, any items are unpaired, or any paired items are not equal.
95
144
  *
96
- * Input items are uniquely keyed in some fashion. The same items are paired up.
145
+ * @param a - First array to compare.
146
+ * @param b - Second array to compare.
147
+ * @param params - Key extraction and equality functions.
148
+ * @returns `true` if the array contents differ.
97
149
  */
98
150
  export declare function arrayContentsDiffer<T, K extends PrimativeKey = PrimativeKey>(a: T[] | undefined, b: T[] | undefined, { groupKeyFn, isEqual }: ArrayContentsDifferentParams<T, K>): boolean;
151
+ /**
152
+ * Groups values by key, then separates them into pairs (values sharing a key) and unpaired (unique key).
153
+ *
154
+ * @param values - Values to group and pair.
155
+ * @param groupKeyFn - Extracts the grouping key from each value.
156
+ * @returns A {@link PairsGroupingResult} with paired and unpaired values.
157
+ */
99
158
  export declare function pairGroupValues<T, K extends PrimativeKey = PrimativeKey>(values: T[], groupKeyFn: ReadKeyFunction<T, K>): PairsGroupingResult<T>;
100
159
  /**
101
- * Creates a tuples array of key and value pairs.
160
+ * Creates an array of `[key, value]` tuples by extracting a key from each value.
102
161
  *
103
- * @param values
104
- * @param keyFn
105
- * @returns
162
+ * @param values - Values to create key pairs from.
163
+ * @param keyFn - Extracts the key from each value.
164
+ * @returns An array of `[key, value]` tuples.
106
165
  */
107
166
  export declare function makeKeyPairs<T, K extends string | number = string | number>(values: T[], keyFn: ReadKeyFunction<T, K>): [Maybe<K>, T][];
108
167
  /**
109
- * Separates the input values into an included and excluded group.
168
+ * Separates values into included and excluded groups based on a decision function.
110
169
  *
111
- * @param values
112
- * @param checkInclusion
113
- * @returns
170
+ * @param values - Values to separate.
171
+ * @param checkInclusion - Returns `true` for values that should be included.
172
+ * @returns A {@link SeparateResult} with included and excluded arrays.
114
173
  */
115
174
  export declare function separateValues<T>(values: T[], checkInclusion: DecisionFunction<T>): SeparateResult<T>;
116
175
  /**
117
- * Convenience function for makeValuesGroupMap that returns a POJO instead of a Map.
176
+ * Groups values by key into a plain object. Convenience wrapper around {@link makeValuesGroupMap} that returns a POJO instead of a Map.
118
177
  *
119
- * @param values
120
- * @param groupKeyFn
178
+ * @param values - Values to group.
179
+ * @param groupKeyFn - Extracts the grouping key from each value.
180
+ * @returns A plain object mapping each key to its array of values.
121
181
  */
122
182
  export declare function groupValues<T, R, K extends PrimativeKey & keyof R>(values: T[], groupKeyFn: ReadKeyFunction<T, K>): KeyedGroupingResult<T, R>;
123
183
  export declare function groupValues<T, K extends PrimativeKey = PrimativeKey>(values: T[], groupKeyFn: ReadKeyFunction<T, K>): GroupingResult<T>;
124
184
  export declare function groupValues<T, R, K extends PrimativeKey & keyof R>(values: Maybe<T[]>, groupKeyFn: ReadKeyFunction<T, K>): KeyedGroupingResult<T, R>;
125
185
  export declare function groupValues<T, K extends PrimativeKey = PrimativeKey>(values: Maybe<T[]>, groupKeyFn: ReadKeyFunction<T, K>): GroupingResult<T>;
126
186
  /**
127
- * Reads keys from the values in the arrays, and groups them together into a Map.
187
+ * Groups values by key into a Map. Each key maps to the array of values that produced that key.
188
+ *
189
+ * @param values - Values to group.
190
+ * @param groupKeyFn - Extracts the grouping key from each value.
191
+ * @returns A Map from each key to its array of values.
128
192
  *
129
- * @param values
130
- * @param groupKeyFn
131
- * @returns
193
+ * @example
194
+ * ```ts
195
+ * const items = [{ type: 'a', v: 1 }, { type: 'b', v: 2 }, { type: 'a', v: 3 }];
196
+ * const map = makeValuesGroupMap(items, (x) => x.type);
197
+ * // Map { 'a' => [{ type: 'a', v: 1 }, { type: 'a', v: 3 }], 'b' => [{ type: 'b', v: 2 }] }
198
+ * ```
132
199
  */
133
200
  export declare function makeValuesGroupMap<T, K extends PrimativeKey = PrimativeKey>(values: T[], groupKeyFn: ReadKeyFunction<T, K>): Map<Maybe<K>, T[]>;
134
201
  export declare function makeValuesGroupMap<T, K extends PrimativeKey = PrimativeKey>(values: Maybe<T[]>, groupKeyFn: ReadKeyFunction<T, K>): Map<Maybe<K>, T[]>;
package/src/lib/hash.d.ts CHANGED
@@ -12,26 +12,33 @@ export type HashDecodeMap<H extends string = string, V extends string = string>
12
12
  /**
13
13
  * Decodes a list of hashed string values using a provided list of potential original values and a hash function.
14
14
  *
15
- * @param hashedValues An array of hashed strings to decode.
16
- * @param decodeValues An array of potential original string values.
17
- * @param hashFn A function that takes a string and returns its hashed representation.
15
+ * @param hashedValues - An array of hashed strings to decode.
16
+ * @param decodeValues - An array of potential original string values.
17
+ * @param hashFn - A function that takes a string and returns its hashed representation.
18
18
  * @returns An array of decoded strings. Values that cannot be decoded are filtered out.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * const hashed = [hashFn('apple'), hashFn('banana')];
23
+ * decodeHashedValues(hashed, ['apple', 'banana', 'cherry'], hashFn);
24
+ * // ['apple', 'banana']
25
+ * ```
19
26
  */
20
27
  export declare function decodeHashedValues(hashedValues: string[], decodeValues: string[], hashFn: (value: string) => string): string[];
21
28
  /**
22
29
  * Creates a `HashDecodeMap` from a list of potential original string values and a hash function.
23
30
  * The map's keys are the hashed versions of the `decodeValues`, and the values are the original `decodeValues`.
24
31
  *
25
- * @param decodeValues An array of potential original string values.
26
- * @param hashFn A function that takes a string and returns its hashed representation.
27
- * @returns A `HashDecodeMap` for decoding hashed values.
32
+ * @param decodeValues - An array of potential original string values.
33
+ * @param hashFn - A function that takes a string and returns its hashed representation.
34
+ * @returns A {@link HashDecodeMap} for decoding hashed values.
28
35
  */
29
36
  export declare function makeHashDecodeMap(decodeValues: string[], hashFn: (value: string) => string): HashDecodeMap;
30
37
  /**
31
38
  * Decodes a list of hashed string values using a pre-built `HashDecodeMap`.
32
39
  *
33
- * @param hashedValues An array of hashed strings to decode.
34
- * @param decodeMap A `HashDecodeMap` to use for looking up original values.
40
+ * @param hashedValues - An array of hashed strings to decode.
41
+ * @param decodeMap - A {@link HashDecodeMap} to use for looking up original values.
35
42
  * @returns An array of decoded strings. Values that cannot be decoded are filtered out.
36
43
  */
37
44
  export declare function decodeHashedValuesWithDecodeMap(hashedValues: string[], decodeMap: HashDecodeMap): string[];