@oscarpalmer/atoms 0.184.2 → 0.186.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 (156) hide show
  1. package/dist/array/difference.d.mts +29 -0
  2. package/dist/array/exists.d.mts +35 -0
  3. package/dist/array/filter.d.mts +72 -2
  4. package/dist/array/find.d.mts +70 -0
  5. package/dist/array/first.d.mts +77 -2
  6. package/dist/array/flatten.d.mts +6 -0
  7. package/dist/array/flatten.mjs +6 -0
  8. package/dist/array/from.d.mts +36 -0
  9. package/dist/array/get.d.mts +21 -13
  10. package/dist/array/group-by.d.mts +142 -0
  11. package/dist/array/index.d.mts +2 -2
  12. package/dist/array/index.mjs +2 -2
  13. package/dist/array/insert.d.mts +16 -0
  14. package/dist/array/intersection.d.mts +29 -0
  15. package/dist/array/last.d.mts +75 -2
  16. package/dist/array/{position.d.mts → match.d.mts} +168 -36
  17. package/dist/array/{position.mjs → match.mjs} +16 -16
  18. package/dist/array/move.d.mts +78 -8
  19. package/dist/array/move.mjs +11 -1
  20. package/dist/array/partition.d.mts +35 -0
  21. package/dist/array/push.d.mts +8 -0
  22. package/dist/array/push.mjs +8 -0
  23. package/dist/array/reverse.d.mts +1 -0
  24. package/dist/array/reverse.mjs +1 -0
  25. package/dist/array/select.d.mts +94 -8
  26. package/dist/array/single.d.mts +29 -0
  27. package/dist/array/slice.d.mts +106 -16
  28. package/dist/array/sort.d.mts +30 -4
  29. package/dist/array/sort.mjs +1 -1
  30. package/dist/array/splice.d.mts +48 -0
  31. package/dist/array/splice.mjs +2 -1
  32. package/dist/array/swap.d.mts +113 -8
  33. package/dist/array/swap.mjs +2 -1
  34. package/dist/array/to-map.d.mts +124 -0
  35. package/dist/array/to-record.d.mts +124 -0
  36. package/dist/array/to-set.d.mts +24 -0
  37. package/dist/array/toggle.d.mts +38 -3
  38. package/dist/array/union.d.mts +29 -0
  39. package/dist/array/unique.d.mts +24 -0
  40. package/dist/array/update.d.mts +38 -3
  41. package/dist/beacon.d.mts +12 -0
  42. package/dist/beacon.mjs +9 -0
  43. package/dist/color/instance.d.mts +8 -0
  44. package/dist/color/instance.mjs +3 -0
  45. package/dist/color/models.d.mts +30 -0
  46. package/dist/function/assert.d.mts +29 -8
  47. package/dist/function/assert.mjs +29 -8
  48. package/dist/function/memoize.d.mts +3 -0
  49. package/dist/function/memoize.mjs +3 -0
  50. package/dist/function/retry.d.mts +3 -0
  51. package/dist/function/retry.mjs +3 -0
  52. package/dist/function/work.mjs +1 -1
  53. package/dist/index.d.mts +2158 -288
  54. package/dist/index.mjs +294 -181
  55. package/dist/internal/array/chunk.d.mts +6 -0
  56. package/dist/internal/array/chunk.mjs +6 -0
  57. package/dist/internal/array/compact.d.mts +12 -0
  58. package/dist/internal/array/index-of.d.mts +70 -0
  59. package/dist/internal/math/aggregate.d.mts +29 -0
  60. package/dist/internal/value/compare.d.mts +2 -1
  61. package/dist/internal/value/equal.d.mts +5 -0
  62. package/dist/internal/value/get.d.mts +27 -5
  63. package/dist/internal/value/has.d.mts +7 -7
  64. package/dist/internal/value/has.mjs +1 -1
  65. package/dist/internal/value/misc.d.mts +2 -2
  66. package/dist/internal/value/misc.mjs +10 -4
  67. package/dist/logger.d.mts +11 -0
  68. package/dist/logger.mjs +11 -0
  69. package/dist/models.d.mts +14 -1
  70. package/dist/promise/helpers.mjs +1 -1
  71. package/dist/promise/index.d.mts +0 -6
  72. package/dist/promise/models.d.mts +36 -0
  73. package/dist/promise/models.mjs +6 -0
  74. package/dist/queue.d.mts +13 -1
  75. package/dist/queue.mjs +9 -0
  76. package/dist/result/index.d.mts +0 -8
  77. package/dist/result/index.mjs +0 -8
  78. package/dist/result/match.d.mts +4 -4
  79. package/dist/result/work/flow.d.mts +12 -36
  80. package/dist/result/work/pipe.d.mts +11 -33
  81. package/dist/sized/set.d.mts +3 -2
  82. package/dist/sized/set.mjs +3 -2
  83. package/dist/value/collection.d.mts +1 -1
  84. package/dist/value/handle.mjs +1 -1
  85. package/dist/value/merge.d.mts +28 -25
  86. package/dist/value/merge.mjs +29 -18
  87. package/dist/value/shake.d.mts +3 -0
  88. package/dist/value/smush.d.mts +3 -0
  89. package/dist/value/transform.d.mts +10 -1
  90. package/dist/value/unsmush.d.mts +2 -3
  91. package/package.json +5 -5
  92. package/src/array/difference.ts +33 -0
  93. package/src/array/exists.ts +35 -0
  94. package/src/array/filter.ts +72 -2
  95. package/src/array/find.ts +70 -0
  96. package/src/array/first.ts +77 -3
  97. package/src/array/flatten.ts +6 -0
  98. package/src/array/from.ts +40 -0
  99. package/src/array/get.ts +21 -15
  100. package/src/array/group-by.ts +142 -0
  101. package/src/array/index.ts +1 -1
  102. package/src/array/insert.ts +16 -2
  103. package/src/array/intersection.ts +33 -0
  104. package/src/array/last.ts +75 -2
  105. package/src/array/{position.ts → match.ts} +197 -65
  106. package/src/array/move.ts +87 -13
  107. package/src/array/partition.ts +35 -0
  108. package/src/array/push.ts +8 -2
  109. package/src/array/reverse.ts +5 -0
  110. package/src/array/select.ts +96 -13
  111. package/src/array/single.ts +29 -0
  112. package/src/array/slice.ts +114 -24
  113. package/src/array/sort.ts +30 -4
  114. package/src/array/splice.ts +52 -4
  115. package/src/array/swap.ts +122 -13
  116. package/src/array/to-map.ts +124 -0
  117. package/src/array/to-record.ts +124 -0
  118. package/src/array/to-set.ts +24 -0
  119. package/src/array/toggle.ts +42 -3
  120. package/src/array/union.ts +33 -0
  121. package/src/array/unique.ts +24 -0
  122. package/src/array/update.ts +38 -3
  123. package/src/beacon.ts +12 -0
  124. package/src/color/index.ts +0 -3
  125. package/src/color/instance.ts +9 -1
  126. package/src/color/models.ts +30 -0
  127. package/src/function/assert.ts +66 -7
  128. package/src/function/memoize.ts +3 -0
  129. package/src/function/once.ts +5 -1
  130. package/src/function/retry.ts +3 -0
  131. package/src/internal/array/chunk.ts +6 -0
  132. package/src/internal/array/compact.ts +12 -0
  133. package/src/internal/array/index-of.ts +70 -0
  134. package/src/internal/math/aggregate.ts +29 -0
  135. package/src/internal/string.ts +0 -2
  136. package/src/internal/value/compare.ts +2 -1
  137. package/src/internal/value/equal.ts +5 -0
  138. package/src/internal/value/get.ts +27 -5
  139. package/src/internal/value/has.ts +10 -10
  140. package/src/internal/value/misc.ts +24 -13
  141. package/src/logger.ts +11 -0
  142. package/src/models.ts +18 -0
  143. package/src/promise/index.ts +0 -6
  144. package/src/promise/models.ts +36 -0
  145. package/src/queue.ts +13 -1
  146. package/src/result/index.ts +0 -8
  147. package/src/result/match.ts +4 -4
  148. package/src/result/work/flow.ts +12 -36
  149. package/src/result/work/pipe.ts +11 -33
  150. package/src/sized/set.ts +4 -3
  151. package/src/value/collection.ts +1 -1
  152. package/src/value/merge.ts +88 -66
  153. package/src/value/shake.ts +3 -0
  154. package/src/value/smush.ts +3 -0
  155. package/src/value/transform.ts +10 -1
  156. package/src/value/unsmush.ts +2 -8
@@ -5,9 +5,18 @@ import type {PlainObject} from '../models';
5
5
 
6
6
  /**
7
7
  * Get an array of unique items
8
+ *
8
9
  * @param array Original array
9
10
  * @param callback Callback to get an item's value
10
11
  * @returns Array of unique items
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * unique(
16
+ * [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
17
+ * item => item.id,
18
+ * ); // => [{id: 1}, {id: 2}, {id: 3}]
19
+ * ```
11
20
  */
12
21
  export function unique<Item>(
13
22
  array: Item[],
@@ -16,9 +25,18 @@ export function unique<Item>(
16
25
 
17
26
  /**
18
27
  * Get an array of unique items
28
+ *
19
29
  * @param array Original array
20
30
  * @param key Key to use for unique value
21
31
  * @returns Array of unique items
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * unique(
36
+ * [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
37
+ * 'id',
38
+ * ); // => [{id: 1}, {id: 2}, {id: 3}]
39
+ * ```
22
40
  */
23
41
  export function unique<Item extends PlainObject, ItemKey extends keyof Item>(
24
42
  array: Item[],
@@ -27,8 +45,14 @@ export function unique<Item extends PlainObject, ItemKey extends keyof Item>(
27
45
 
28
46
  /**
29
47
  * Get an array of unique items
48
+ *
30
49
  * @param array Original array
31
50
  * @returns Array of unique items
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * unique([1, 2, 3, 2]); // => [1, 2, 3]
55
+ * ```
32
56
  */
33
57
  export function unique<Item>(array: Item[]): Item[];
34
58
 
@@ -2,11 +2,23 @@ import {updateInArray} from '../internal/array/update';
2
2
  import type {PlainObject} from '../models';
3
3
 
4
4
  /**
5
- * Update an item in an array: if the item exists, it will be updated; if it doesn't, it will be added
5
+ * Update an item in an array
6
+ *
7
+ * If the item exists, it will be updated; if it doesn't, it will be added
8
+ *
6
9
  * @param destination Array to update within
7
10
  * @param updated Updated items
8
11
  * @param callback Callback to find existing item
9
12
  * @returns Original array
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * update(
17
+ * [{id: 1}, {id: 2}, {id: 3}],
18
+ * [{id: 2, name: 'Updated'}, {id: 4, name: 'New'}],
19
+ * item => item.id,
20
+ * ); // => [{id: 1}, {id: 2, name: 'Updated'}, {id: 3}, {id: 4, name: 'New'}]
21
+ * ```
10
22
  */
11
23
  export function update<Item>(
12
24
  destination: Item[],
@@ -15,11 +27,23 @@ export function update<Item>(
15
27
  ): Item[];
16
28
 
17
29
  /**
18
- * Update an item in an array: if the item exists, it will be updated; if it doesn't, it will be added
30
+ * Update an item in an array
31
+ *
32
+ * If the item exists, it will be updated; if it doesn't, it will be added
33
+ *
19
34
  * @param destination Array to update within
20
35
  * @param updated Updated items
21
36
  * @param key Key to find existing item
22
37
  * @returns Original array
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * update(
42
+ * [{id: 1}, {id: 2}, {id: 3}],
43
+ * [{id: 2, name: 'Updated'}, {id: 4, name: 'New'}],
44
+ * 'id',
45
+ * ); // => [{id: 1}, {id: 2, name: 'Updated'}, {id: 3}, {id: 4, name: 'New'}]
46
+ * ```
23
47
  */
24
48
  export function update<Item extends PlainObject, ItemKey extends keyof Item>(
25
49
  destination: Item[],
@@ -28,10 +52,21 @@ export function update<Item extends PlainObject, ItemKey extends keyof Item>(
28
52
  ): Item[];
29
53
 
30
54
  /**
31
- * Update an item in an array: if the item exists, it will be updated; if it doesn't, it will be added
55
+ * Update an item in an array
56
+ *
57
+ * If the item exists, it will be updated; if it doesn't, it will be added
58
+ *
32
59
  * @param destination Array to update within
33
60
  * @param updated Updated items
34
61
  * @returns Original array
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * update(
66
+ * [1, 2, 3],
67
+ * [2, 4],
68
+ * ); // => [1, 2, 3, 4]
69
+ * ```
35
70
  */
36
71
  export function update<Item>(destination: Item[], updated: Item[]): Item[];
37
72
 
package/src/beacon.ts CHANGED
@@ -4,6 +4,9 @@ import type {PlainObject} from './models';
4
4
 
5
5
  // #region Types
6
6
 
7
+ /**
8
+ * A beacon is a lighthouse, holding an observable value that can be subscribed to and emitted from
9
+ */
7
10
  class Beacon<Value> {
8
11
  readonly #options: Options;
9
12
  readonly #state: BeaconState<Value>;
@@ -122,6 +125,9 @@ type BeaconState<Value> = {
122
125
  value: Value;
123
126
  };
124
127
 
128
+ /**
129
+ * An observable holds a value and allows observers to subscribe to changes in that value
130
+ */
125
131
  class Observable<Value> {
126
132
  readonly #state: ObservableState<Value>;
127
133
 
@@ -186,6 +192,9 @@ type ObservableState<Value> = {
186
192
  observers: Map<Subscription<Value>, Observer<Value>>;
187
193
  };
188
194
 
195
+ /**
196
+ * An observer receives notifications from an observable
197
+ */
189
198
  type Observer<Value> = {
190
199
  /**
191
200
  * Callback for when the observable is completed
@@ -203,6 +212,9 @@ type Observer<Value> = {
203
212
 
204
213
  type Options = Required<BeaconOptions<unknown>>;
205
214
 
215
+ /**
216
+ * A subscription represents an active subscription to an observable, holding its state and allowing it to be destroyed or unsubscribed from
217
+ */
206
218
  class Subscription<Value> {
207
219
  readonly #state: SubscriptionState<Value>;
208
220
 
@@ -39,11 +39,8 @@ export {
39
39
  } from './misc/is';
40
40
 
41
41
  export {getNormalizedHex, hexToHsl, hexToHsla, hexToRgb, hexToRgba} from './space/hex';
42
-
43
42
  export {hslToHex, hslToRgb, hslToRgba} from './space/hsl';
44
-
45
43
  export {rgbToHex, rgbToHsl, rgbToHsla} from './space/rgb';
46
-
47
44
  export type {Color, HSLAColor, HSLColor, RGBAColor, RGBColor};
48
45
 
49
46
  // #endregion
@@ -4,9 +4,17 @@ import {getAlpha} from './misc/alpha';
4
4
  import {getColorState, setHexColor, setHSLColor, setRGBColor} from './misc/state';
5
5
  import type {ColorState, HSLAColor, HSLColor, RGBAColor, RGBColor} from './models';
6
6
 
7
- // #region Classes
7
+ // #region Types
8
8
 
9
+ /**
10
+ * A color that is represented in multiple color formats
11
+ */
9
12
  export class Color {
13
+ /**
14
+ * A property to identify this as a Color instance, used for type checking
15
+ *
16
+ * @internal
17
+ */
10
18
  declare private readonly $color: boolean;
11
19
 
12
20
  readonly #state: ColorState;
@@ -9,19 +9,49 @@ type ColorWithAlpha = {
9
9
  alpha: number;
10
10
  };
11
11
 
12
+ /**
13
+ * An _HSL_-color with an alpha channel
14
+ */
12
15
  export type HSLAColor = HSLColor & ColorWithAlpha;
13
16
 
17
+ /**
18
+ * An _HSL_-color
19
+ */
14
20
  export type HSLColor = {
21
+ /**
22
+ * Hue of the color _(in degrees; 0-360)_
23
+ */
15
24
  hue: number;
25
+ /**
26
+ * Lightness of the color _(in percentage; 0-100)_
27
+ */
16
28
  lightness: number;
29
+ /**
30
+ * Saturation of the color _(in percentage; 0-100)_
31
+ */
17
32
  saturation: number;
18
33
  };
19
34
 
35
+ /**
36
+ * An _RGB_-color with an alpha channel
37
+ */
20
38
  export type RGBAColor = RGBColor & ColorWithAlpha;
21
39
 
40
+ /**
41
+ * An _RGB_-color
42
+ */
22
43
  export type RGBColor = {
44
+ /**
45
+ * Blue channel of the color _(in hexadecimal; 0-255)_
46
+ */
23
47
  blue: number;
48
+ /**
49
+ * Green channel of the color _(in hexadecimal; 0-255)_
50
+ */
24
51
  green: number;
52
+ /**
53
+ * Red channel of the color _(in hexadecimal; 0-255)_
54
+ */
25
55
  red: number;
26
56
  };
27
57
 
@@ -1,9 +1,32 @@
1
- import type {Constructor} from '../models';
1
+ import {hasValueResult} from '../internal/value/has';
2
+ import type {Constructor, NestedKeys, NestedValue, PlainObject} from '../models';
2
3
 
3
4
  // #region Types
4
5
 
6
+ /**
7
+ * Asserter for a nested property of a value
8
+ */
9
+ export type AssertProperty<
10
+ Value extends PlainObject,
11
+ Path extends NestedKeys<Value>,
12
+ Asserted extends NestedPick<Value, Path> = NestedPick<Value, Path>,
13
+ > = Asserter<Asserted>;
14
+
15
+ /**
16
+ * A function that asserts a value is of a specific type, throwing an error if it is not
17
+ */
5
18
  export type Asserter<Value> = (value: unknown) => asserts value is Value;
6
19
 
20
+ type NestedPick<Value, Path extends string> = Value extends PlainObject
21
+ ? Path extends `${infer Head}.${infer Rest}`
22
+ ? Head extends keyof Value
23
+ ? {[Key in Head]: NestedPick<Value[Key], Rest>}
24
+ : never
25
+ : Path extends keyof Value
26
+ ? {[Key in Path]: Value[Key]}
27
+ : never
28
+ : never;
29
+
7
30
  // #endregion
8
31
 
9
32
  // #region Functions
@@ -12,7 +35,7 @@ export type Asserter<Value> = (value: unknown) => asserts value is Value;
12
35
  * Asserts that a condition is true, throwing an error if it is not
13
36
  * @param condition Condition to assert
14
37
  * @param message Error message
15
- * @param error Error constructor
38
+ * @param error Error constructor _(defaults to `Error`)_
16
39
  */
17
40
  export function assert<Condition extends () => boolean>(
18
41
  condition: Condition,
@@ -28,6 +51,7 @@ assert.condition = assertCondition;
28
51
  assert.defined = assertDefined;
29
52
  assert.instanceOf = assertInstanceOf;
30
53
  assert.is = assertIs;
54
+ assert.property = assertProperty;
31
55
 
32
56
  /**
33
57
  * Creates an asserter that asserts a condition is true, throwing an error if it is not
@@ -35,7 +59,7 @@ assert.is = assertIs;
35
59
  * Available as `assertCondition` and `assert.condition`
36
60
  * @param condition Condition to assert
37
61
  * @param message Error message
38
- * @param error Error constructor
62
+ * @param error Error constructor _(defaults to `Error`)_
39
63
  * @returns Asserter
40
64
  */
41
65
  export function assertCondition<Value>(
@@ -49,17 +73,19 @@ export function assertCondition<Value>(
49
73
  }
50
74
 
51
75
  /**
52
- * Asserts that a value is defined throwing an error if it is not
76
+ * Asserts that a value is defined, throwing an error if it is not
53
77
  *
54
78
  * Available as `assertDefined` and `assert.defined`
55
79
  * @param value Value to assert
56
80
  * @param message Error message
81
+ * @param error Error constructor _(defaults to `Error`)_
57
82
  */
58
83
  export function assertDefined<Value>(
59
84
  value: unknown,
60
85
  message?: string,
86
+ error?: ErrorConstructor,
61
87
  ): asserts value is Exclude<Value, null | undefined> {
62
- assert(() => value != null, message ?? MESSAGE_VALUE_DEFINED);
88
+ assert(() => value != null, message ?? MESSAGE_VALUE_DEFINED, error);
63
89
  }
64
90
 
65
91
  /**
@@ -68,7 +94,7 @@ export function assertDefined<Value>(
68
94
  * Available as `assertInstanceOf` and `assert.instanceOf`
69
95
  * @param constructor Constructor to check against
70
96
  * @param message Error message
71
- * @param error Error constructor
97
+ * @param error Error constructor _(defaults to `Error`)_
72
98
  * @returns Asserter
73
99
  */
74
100
  export function assertInstanceOf<Value>(
@@ -87,7 +113,7 @@ export function assertInstanceOf<Value>(
87
113
  * Available as `assertIs` and `assert.is`
88
114
  * @param condition Type guard function to check the value
89
115
  * @param message Error message
90
- * @param error Error constructor
116
+ * @param error Error constructor _(defaults to `Error`)_
91
117
  * @returns Asserter
92
118
  */
93
119
  export function assertIs<Value>(
@@ -100,6 +126,39 @@ export function assertIs<Value>(
100
126
  };
101
127
  }
102
128
 
129
+ /**
130
+ * Creates an asserter that asserts a property of a value exists and satisfies a condition, throwing an error if it does not
131
+ *
132
+ * Available as `assertProperty` and `assert.property`
133
+ * @param path Path to the property to check, e.g., `foo.bar.baz` for a nested property
134
+ * @param condition Condition to assert for the property
135
+ * @param message Error message
136
+ * @param error Error constructor _(defaults to `Error`)_
137
+ * @returns Asserter
138
+ */
139
+ export function assertProperty<
140
+ Value extends PlainObject,
141
+ Path extends NestedKeys<Value>,
142
+ Asserted = NestedPick<Value, Path>,
143
+ >(
144
+ path: Path,
145
+ condition: (value: NestedValue<Value, Path>) => boolean,
146
+ message: string,
147
+ error?: ErrorConstructor,
148
+ ): Asserter<Asserted> {
149
+ return (value: unknown): asserts value is Asserted => {
150
+ assert(
151
+ () => {
152
+ const result = hasValueResult(value as never, path, false);
153
+
154
+ return result.ok && condition(result.value as never);
155
+ },
156
+ message,
157
+ error,
158
+ );
159
+ };
160
+ }
161
+
103
162
  // #endregion
104
163
 
105
164
  // #region Variables
@@ -5,6 +5,9 @@ import {SizedMap} from '../sized/map';
5
5
 
6
6
  // #region Types
7
7
 
8
+ /**
9
+ * A memoized function, caching and retrieving results based on the its parameters _(or a custom cache key)_
10
+ */
8
11
  class Memoized<Callback extends GenericCallback> {
9
12
  readonly #state: MemoizedState<Callback>;
10
13
 
@@ -107,7 +107,11 @@ export function asyncOnce<Callback extends GenericAsyncCallback>(
107
107
  return fn as OnceAsyncCallback<Callback>;
108
108
  }
109
109
 
110
- function handleOnceResult<Value>(state: OnceAsyncState<Value>, value: unknown, error: boolean): void {
110
+ function handleOnceResult<Value>(
111
+ state: OnceAsyncState<Value>,
112
+ value: unknown,
113
+ error: boolean,
114
+ ): void {
111
115
  state.error = error;
112
116
  state.finished = true;
113
117
  state.value = value as Value;
@@ -4,6 +4,9 @@ import type {GenericAsyncCallback, GenericCallback} from '../models';
4
4
 
5
5
  // #region Types
6
6
 
7
+ /**
8
+ * An error thrown when a retry fails
9
+ */
7
10
  export class RetryError extends Error {
8
11
  constructor(
9
12
  message: string,
@@ -2,9 +2,15 @@
2
2
 
3
3
  /**
4
4
  * Chunk an array into smaller arrays
5
+ *
5
6
  * @param array Array to chunk
6
7
  * @param size Size of each chunk _(minimum is `1`, maximum is `5000`; defaults to `5000`)_
7
8
  * @returns Array of arrays
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * chunk([1, 2, 3, 4, 5], 2); // => [[1, 2], [3, 4], [5]]
13
+ * ```
8
14
  */
9
15
  export function chunk<Item>(array: Item[], size?: number): Item[][] {
10
16
  if (!Array.isArray(array)) {
@@ -2,9 +2,15 @@
2
2
 
3
3
  /**
4
4
  * Compact an array _(removing all false-y values)_
5
+ *
5
6
  * @param array Array to compact
6
7
  * @param strict True to remove all false-y values
7
8
  * @returns Compacted array
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * compact([0, 1, '', 'hello', false, true, null, undefined]); // => [1, 'hello', true]
13
+ * ```
8
14
  */
9
15
  export function compact<Item>(
10
16
  array: Item[],
@@ -13,8 +19,14 @@ export function compact<Item>(
13
19
 
14
20
  /**
15
21
  * Compact an array _(removing all `null` and `undefined` values)_
22
+ *
16
23
  * @param array Array to compact
17
24
  * @returns Compacted array
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * compact([0, 1, '', 'hello', false, true, null, undefined]); // => [0, 1, '', 'hello', false, true]
29
+ * ```
18
30
  */
19
31
  export function compact<Item>(array: Item[]): Exclude<Item, null | undefined>[];
20
32
 
@@ -5,10 +5,20 @@ import type {PlainObject} from '../../models';
5
5
 
6
6
  /**
7
7
  * Get the index of the first matching item by callback
8
+ *
8
9
  * @param array Array to search in
9
10
  * @param callback Callback to get an item's value
10
11
  * @param value Value to match against
11
12
  * @returns Index of the first matching item, or `-1` if no match is found
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * indexOf(
17
+ * [{id: 1}, {id: 2}, {id: 3}],
18
+ * item => item.id,
19
+ * 2,
20
+ * ); // => 1
21
+ * ```
12
22
  */
13
23
  export function indexOf<
14
24
  Item,
@@ -17,10 +27,20 @@ export function indexOf<
17
27
 
18
28
  /**
19
29
  * Get the index of the first matching item by key
30
+ *
20
31
  * @param array Array to search in
21
32
  * @param key Key to match items by
22
33
  * @param value Value to match against
23
34
  * @returns Index of the first matching item, or `-1` if no match is found
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * indexOf(
39
+ * [{id: 1}, {id: 2}, {id: 3}],
40
+ * 'id',
41
+ * 2,
42
+ * ); // => 1
43
+ * ```
24
44
  */
25
45
  export function indexOf<Item extends PlainObject, ItemKey extends keyof Item>(
26
46
  array: Item[],
@@ -30,9 +50,18 @@ export function indexOf<Item extends PlainObject, ItemKey extends keyof Item>(
30
50
 
31
51
  /**
32
52
  * Get the index of the first item matching the filter
53
+ *
33
54
  * @param array Array to search in
34
55
  * @param filter Filter callback to match items
35
56
  * @returns Index of the first matching item, or `-1` if no match is found
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * indexOf(
61
+ * [{id: 1}, {id: 2}, {id: 3}],
62
+ * item => item.id === 2,
63
+ * ); // => 1
64
+ * ```
36
65
  */
37
66
  export function indexOf<Item>(
38
67
  array: Item[],
@@ -41,9 +70,15 @@ export function indexOf<Item>(
41
70
 
42
71
  /**
43
72
  * Get the index of the first item matching the given item
73
+ *
44
74
  * @param array Array to search in
45
75
  * @param item Item to match against
46
76
  * @returns Index of the first matching item, or `-1` if no match is found
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * indexOf([1, 2, 3, 4, 5], 3); // => 2
81
+ * ```
47
82
  */
48
83
  export function indexOf<Item>(array: Item[], item: Item): number;
49
84
 
@@ -57,10 +92,20 @@ indexOf.last = lastIndexOf;
57
92
  * Get the index of the last matching item by callback
58
93
  *
59
94
  * Available as `lastIndexOf` and `indexOf.last`
95
+ *
60
96
  * @param array Array to search in
61
97
  * @param callback Callback to get an item's value
62
98
  * @param value Value to match against
63
99
  * @returns Index of the last matching item, or `-1` if no match is found
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * lastIndexOf(
104
+ * [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
105
+ * item => item.id,
106
+ * 2,
107
+ * ); // => 3
108
+ * ```
64
109
  */
65
110
  export function lastIndexOf<
66
111
  Item,
@@ -71,10 +116,20 @@ export function lastIndexOf<
71
116
  * Get the index of the last matching item by key
72
117
  *
73
118
  * Available as `lastIndexOf` and `indexOf.last`
119
+ *
74
120
  * @param array Array to search in
75
121
  * @param key Key to match items by
76
122
  * @param value Value to match against
77
123
  * @returns Index of the last matching item, or `-1` if no match is found
124
+ *
125
+ * @example
126
+ * ```typescript
127
+ * lastIndexOf(
128
+ * [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
129
+ * 'id',
130
+ * 2,
131
+ * ); // => 3
132
+ * ```
78
133
  */
79
134
  export function lastIndexOf<Item extends PlainObject, ItemKey extends keyof Item>(
80
135
  array: Item[],
@@ -86,9 +141,18 @@ export function lastIndexOf<Item extends PlainObject, ItemKey extends keyof Item
86
141
  * Get the index of the last item matching the filter
87
142
  *
88
143
  * Available as `lastIndexOf` and `indexOf.last`
144
+ *
89
145
  * @param array Array to search in
90
146
  * @param filter Filter callback to match items
91
147
  * @returns Index of the last matching item, or `-1` if no match is found
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * lastIndexOf(
152
+ * [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
153
+ * item => item.id === 2,
154
+ * ); // => 3
155
+ * ```
92
156
  */
93
157
  export function lastIndexOf<Item>(
94
158
  array: Item[],
@@ -99,9 +163,15 @@ export function lastIndexOf<Item>(
99
163
  * Get the index of the last item matching the given item
100
164
  *
101
165
  * Available as `lastIndexOf` and `indexOf.last`
166
+ *
102
167
  * @param array Array to search in
103
168
  * @param item Item to match against
104
169
  * @returns Index of the last matching item, or `-1` if no match is found
170
+ *
171
+ * @example
172
+ * ```typescript
173
+ * lastIndexOf([1, 2, 3, 2, 1], 2); // => 3
174
+ * ```
105
175
  */
106
176
  export function lastIndexOf<Item>(array: Item[], item: Item): number;
107
177
 
@@ -66,6 +66,17 @@ export function getAggregateCallback(key: unknown): Function | undefined {
66
66
 
67
67
  /**
68
68
  * Get the maximum value from a list of items
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * max(
73
+ * [{id: 1, value: 10}, {id: 2, value: 20}],
74
+ * item => item.value,
75
+ * ); // 20
76
+ *
77
+ * max([], item => item.value); // Number.NaN
78
+ * ```
79
+ *
69
80
  * @param items List of items
70
81
  * @param callback Callback to get an item's value
71
82
  * @returns Maximum value, or `NaN` if no maximum can be found
@@ -77,6 +88,17 @@ export function max<Item>(
77
88
 
78
89
  /**
79
90
  * Get the maximum value from a list of items
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * max(
95
+ * [{id: 1, value: 10}, {id: 2, value: 20}],
96
+ * 'value',
97
+ * ); // 20
98
+ *
99
+ * max([], 'value'); // Number.NaN
100
+ * ```
101
+ *
80
102
  * @param items List of items
81
103
  * @param key Key to use for value
82
104
  * @returns Maximum value, or `NaN` if no maximum can be found
@@ -88,6 +110,13 @@ export function max<Item extends PlainObject, ItemKey extends keyof NumericalVal
88
110
 
89
111
  /**
90
112
  * Get the maximum value from a list of numbers
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * max([10, 20]); // 20
117
+ * max([]); // Number.NaN
118
+ * ```
119
+ *
91
120
  * @param values List of numbers
92
121
  * @returns Maximum value, or `NaN` if no maximum can be found
93
122
  */
@@ -19,11 +19,9 @@ export function getString(value: unknown): string {
19
19
  }
20
20
 
21
21
  if (typeof value !== 'object') {
22
- // oxlint-disable-next-line typescript/no-base-to-string: the whole point of this function is to get a string representation of any value, so calling `String` on it is fine
23
22
  return String(value);
24
23
  }
25
24
 
26
- // oxlint-disable-next-line typescript/no-base-to-string: ditto
27
25
  const asString = String(value.valueOf?.() ?? value);
28
26
 
29
27
  return asString.startsWith('[object ') ? JSON.stringify(value) : asString;
@@ -45,6 +45,7 @@ export function compare(first: unknown, second: unknown): number {
45
45
  const firstParts = getComparisonParts(first);
46
46
  const secondParts = getComparisonParts(second);
47
47
  const length = max([firstParts.length, secondParts.length]);
48
+
48
49
  const lastIndex = length - 1;
49
50
 
50
51
  for (let index = 0; index < length; index += 1) {
@@ -147,7 +148,7 @@ function getComparisonParts(value: unknown): unknown[] {
147
148
  */
148
149
  export function registerComparator<Instance>(
149
150
  constructor: Constructor<Instance>,
150
- handler?: string | ((first: Instance, second: Instance) => number),
151
+ handler?: string | Comparator<Instance>,
151
152
  ): void {
152
153
  compare.handlers.register(constructor, handler);
153
154
  }