@oscarpalmer/atoms 0.185.0 → 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 (94) 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/insert.d.mts +16 -0
  12. package/dist/array/intersection.d.mts +29 -0
  13. package/dist/array/last.d.mts +75 -2
  14. package/dist/array/match.d.mts +161 -32
  15. package/dist/array/move.d.mts +78 -8
  16. package/dist/array/move.mjs +10 -0
  17. package/dist/array/partition.d.mts +35 -0
  18. package/dist/array/push.d.mts +8 -0
  19. package/dist/array/push.mjs +8 -0
  20. package/dist/array/reverse.d.mts +1 -0
  21. package/dist/array/reverse.mjs +1 -0
  22. package/dist/array/select.d.mts +94 -8
  23. package/dist/array/single.d.mts +29 -0
  24. package/dist/array/slice.d.mts +106 -16
  25. package/dist/array/sort.d.mts +21 -0
  26. package/dist/array/splice.d.mts +48 -0
  27. package/dist/array/splice.mjs +2 -1
  28. package/dist/array/swap.d.mts +113 -8
  29. package/dist/array/swap.mjs +1 -0
  30. package/dist/array/to-map.d.mts +124 -0
  31. package/dist/array/to-record.d.mts +124 -0
  32. package/dist/array/to-set.d.mts +24 -0
  33. package/dist/array/toggle.d.mts +38 -3
  34. package/dist/array/union.d.mts +29 -0
  35. package/dist/array/unique.d.mts +24 -0
  36. package/dist/array/update.d.mts +38 -3
  37. package/dist/index.d.mts +1892 -135
  38. package/dist/index.mjs +64 -18
  39. package/dist/internal/array/chunk.d.mts +6 -0
  40. package/dist/internal/array/chunk.mjs +6 -0
  41. package/dist/internal/array/compact.d.mts +12 -0
  42. package/dist/internal/array/index-of.d.mts +70 -0
  43. package/dist/internal/math/aggregate.d.mts +29 -0
  44. package/dist/internal/value/get.d.mts +25 -3
  45. package/dist/internal/value/has.d.mts +4 -4
  46. package/dist/models.d.mts +14 -1
  47. package/dist/value/collection.d.mts +1 -1
  48. package/dist/value/merge.d.mts +28 -25
  49. package/dist/value/merge.mjs +29 -18
  50. package/dist/value/transform.d.mts +1 -1
  51. package/dist/value/unsmush.d.mts +1 -5
  52. package/package.json +5 -5
  53. package/src/array/difference.ts +29 -0
  54. package/src/array/exists.ts +35 -0
  55. package/src/array/filter.ts +72 -2
  56. package/src/array/find.ts +70 -0
  57. package/src/array/first.ts +77 -3
  58. package/src/array/flatten.ts +6 -0
  59. package/src/array/from.ts +36 -0
  60. package/src/array/get.ts +21 -15
  61. package/src/array/group-by.ts +142 -0
  62. package/src/array/insert.ts +16 -2
  63. package/src/array/intersection.ts +29 -0
  64. package/src/array/last.ts +75 -2
  65. package/src/array/match.ts +171 -42
  66. package/src/array/move.ts +82 -12
  67. package/src/array/partition.ts +35 -0
  68. package/src/array/push.ts +8 -2
  69. package/src/array/reverse.ts +1 -0
  70. package/src/array/select.ts +94 -13
  71. package/src/array/single.ts +29 -0
  72. package/src/array/slice.ts +114 -24
  73. package/src/array/sort.ts +21 -0
  74. package/src/array/splice.ts +52 -4
  75. package/src/array/swap.ts +117 -12
  76. package/src/array/to-map.ts +124 -0
  77. package/src/array/to-record.ts +124 -0
  78. package/src/array/to-set.ts +24 -0
  79. package/src/array/toggle.ts +38 -3
  80. package/src/array/union.ts +29 -0
  81. package/src/array/unique.ts +24 -0
  82. package/src/array/update.ts +38 -3
  83. package/src/internal/array/chunk.ts +6 -0
  84. package/src/internal/array/compact.ts +12 -0
  85. package/src/internal/array/index-of.ts +70 -0
  86. package/src/internal/math/aggregate.ts +29 -0
  87. package/src/internal/string.ts +0 -2
  88. package/src/internal/value/get.ts +25 -3
  89. package/src/internal/value/has.ts +4 -4
  90. package/src/models.ts +18 -0
  91. package/src/value/collection.ts +1 -1
  92. package/src/value/merge.ts +88 -66
  93. package/src/value/transform.ts +1 -1
  94. package/src/value/unsmush.ts +1 -10
@@ -5,10 +5,20 @@ import type {PlainObject} from '../models';
5
5
 
6
6
  /**
7
7
  * Get the first item matching the given value
8
+ *
8
9
  * @param array Array to search in
9
10
  * @param callback Callback to get an item's value for matching
10
11
  * @param value Value to match against
11
12
  * @returns First item that matches the value, or `undefined` if no match is found
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * first(
17
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
18
+ * item => item.value,
19
+ * 10,
20
+ * ); // => {id: 1, value: 10}
21
+ * ```
12
22
  */
13
23
  export function first<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(
14
24
  array: Item[],
@@ -18,10 +28,20 @@ export function first<Item, Callback extends (item: Item, index: number, array:
18
28
 
19
29
  /**
20
30
  * Get the first item matching the given value by key
31
+ *
21
32
  * @param array Array to search in
22
33
  * @param key Key to get an item's value for matching
23
34
  * @param value Value to match against
24
35
  * @returns First item that matches the value, or `undefined` if no match is found
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * first(
40
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
41
+ * 'value',
42
+ * 10,
43
+ * ); // => {id: 1, value: 10}
44
+ * ```
25
45
  */
26
46
  export function first<Item extends PlainObject, ItemKey extends keyof Item>(
27
47
  array: Item[],
@@ -31,9 +51,18 @@ export function first<Item extends PlainObject, ItemKey extends keyof Item>(
31
51
 
32
52
  /**
33
53
  * Get the first item matching the filter
54
+ *
34
55
  * @param array Array to search in
35
56
  * @param filter Filter callback to match items
36
57
  * @returns First item that matches the filter, or `undefined` if no match is found
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * first(
62
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
63
+ * item => item.value === 10,
64
+ * ); // => {id: 1, value: 10}
65
+ * ```
37
66
  */
38
67
  export function first<Item>(
39
68
  array: Item[],
@@ -42,8 +71,16 @@ export function first<Item>(
42
71
 
43
72
  /**
44
73
  * Get the first item from an array
74
+ *
45
75
  * @param array Array to get from
46
- * @return First item from the array, or `undefined` if the array is empty
76
+ * @returns First item from the array, or `undefined` if the array is empty
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * first(
81
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
82
+ * ); // => {id: 1, value: 10}
83
+ * ```
47
84
  */
48
85
  export function first<Item>(array: Item[]): Item | undefined;
49
86
 
@@ -57,11 +94,22 @@ first.default = firstOrDefault;
57
94
  * Get the first item matching the given value, or a default value if no match is found
58
95
  *
59
96
  * Available as `firstOrDefault` and `first.default`
97
+ *
60
98
  * @param array Array to search in
61
99
  * @param defaultValue Default value to return if no match is found
62
100
  * @param callback Callback to get an item's value for matching
63
101
  * @param value Value to match against
64
102
  * @returns First item that matches the value, or the default value if no match is found
103
+ *
104
+ * @example
105
+ * ```typescript
106
+ * firstOrDefault(
107
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
108
+ * {id: -1, value: 30},
109
+ * item => item.value,
110
+ * 30,
111
+ * ); // => {id: -1, value: 30}
112
+ * ```
65
113
  */
66
114
  export function firstOrDefault<
67
115
  Item,
@@ -72,13 +120,23 @@ export function firstOrDefault<
72
120
  * Get the first item matching the given value by key, or a default value if no match is found
73
121
  *
74
122
  * Available as `firstOrDefault` and `first.default`
123
+ *
75
124
  * @param array Array to search in
76
125
  * @param defaultValue Default value to return if no match is found
77
126
  * @param key Key to get an item's value for matching
78
127
  * @param value Value to match against
79
128
  * @returns First item that matches the value, or the default value if no match is found
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * firstOrDefault(
133
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
134
+ * {id: -1, value: 30},
135
+ * 'value',
136
+ * 30,
137
+ * ); // => {id: -1, value: 30}
138
+ * ```
80
139
  */
81
-
82
140
  export function firstOrDefault<Item extends PlainObject, ItemKey extends keyof Item>(
83
141
  array: Item[],
84
142
  defaultValue: Item,
@@ -90,10 +148,20 @@ export function firstOrDefault<Item extends PlainObject, ItemKey extends keyof I
90
148
  * Get the first item matching the filter, or a default value if no match is found
91
149
  *
92
150
  * Available as `firstOrDefault` and `first.default`
151
+ *
93
152
  * @param array Array to search in
94
153
  * @param defaultValue Default value to return if no match is found
95
154
  * @param filter Filter callback to match items
96
155
  * @returns First item that matches the filter, or the default value if no match is found
156
+ *
157
+ * @example
158
+ * ```typescript
159
+ * firstOrDefault(
160
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
161
+ * {id: -1, value: 30},
162
+ * item => item.value === 30,
163
+ * ); // => {id: -1, value: 30}
164
+ * ```
97
165
  */
98
166
  export function firstOrDefault<Item>(
99
167
  array: Item[],
@@ -105,9 +173,15 @@ export function firstOrDefault<Item>(
105
173
  * Get the first item from an array, or a default value if the array is empty
106
174
  *
107
175
  * Available as `firstOrDefault` and `first.default`
176
+ *
108
177
  * @param array Array to get from
109
178
  * @param defaultValue Default value to return if the array is empty
110
- * @return First item from the array, or the default value if the array is empty
179
+ * @returns First item from the array, or the default value if the array is empty
180
+ *
181
+ * @example
182
+ * ```typescript
183
+ * firstOrDefault([], {id: -1, value: 30}); // => {id: -1, value: 30}
184
+ * ```
111
185
  */
112
186
  export function firstOrDefault<Item>(array: Item[], defaultValue: Item): Item;
113
187
 
@@ -4,8 +4,14 @@ import type {NestedArray} from '../models';
4
4
 
5
5
  /**
6
6
  * Flatten an array _(using native `flat` and maximum depth)_
7
+ *
7
8
  * @param array Array to flatten
8
9
  * @returns Flattened array
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * flatten([1, [2, [3, 4], 5], 6]); // => [1, 2, 3, 4, 5, 6]
14
+ * ```
9
15
  */
10
16
  export function flatten<Item>(array: Item[]): NestedArray<Item>[] {
11
17
  return (Array.isArray(array) ? array.flat(Number.POSITIVE_INFINITY) : []) as NestedArray<Item>[];
package/src/array/from.ts CHANGED
@@ -2,25 +2,43 @@
2
2
 
3
3
  /**
4
4
  * Get an array with a specified length, filled with indices
5
+ *
5
6
  * @param length Length of the array
6
7
  * @returns Array of indices
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * range(5); // => [0, 1, 2, 3, 4]
12
+ * ```
7
13
  */
8
14
  export function range(length: number): number[];
9
15
 
10
16
  /**
11
17
  * Get an array of numbers in a specified range
18
+ *
12
19
  * @param start Starting number _(inclusive)_
13
20
  * @param end Ending number _(exclusive)_
14
21
  * @returns Array of numbers in range
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * range(2, 5); // => [2, 3, 4]
26
+ * ```
15
27
  */
16
28
  export function range(start: number, end: number): number[];
17
29
 
18
30
  /**
19
31
  * Get an array of numbers in a specified range with a specified step
32
+ *
20
33
  * @param start Starting number _(inclusive)_
21
34
  * @param end Ending number _(exclusive)_
22
35
  * @param step Step between numbers
23
36
  * @returns Array of numbers in range
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * range(0, 10, 2); // => [0, 2, 4, 6, 8]
41
+ * ```
24
42
  */
25
43
  export function range(start: number, end: number, step: number): number[];
26
44
 
@@ -50,16 +68,28 @@ export function range(first: number, second?: number, third?: number): number[]
50
68
 
51
69
  /**
52
70
  * Get an array with a specified length, filled with indices
71
+ *
53
72
  * @param length Length of the array
54
73
  * @returns Array of indices
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * times(5); // => [0, 1, 2, 3, 4]
78
+ * ```
55
79
  */
56
80
  export function times(length: number): number[];
57
81
 
58
82
  /**
59
83
  * Get an array with a specified length, filled by values from a callback
84
+ *
60
85
  * @param length Length of the array
61
86
  * @param callback Callback function to generate values
62
87
  * @returns Array of values generated by the callback
88
+ *
89
+ * @example
90
+ * ```typescript
91
+ * times(5, index => index * 2); // => [0, 2, 4, 6, 8]
92
+ * ```
63
93
  */
64
94
  export function times<Callback extends (index: number) => unknown>(
65
95
  length: number,
@@ -68,9 +98,15 @@ export function times<Callback extends (index: number) => unknown>(
68
98
 
69
99
  /**
70
100
  * Get an array with a specified length, filled with a specified value
101
+ *
71
102
  * @param length Length of the array
72
103
  * @param value Value to fill the array with
73
104
  * @returns Array filled with the specified value
105
+ *
106
+ * @example
107
+ * ```typescript
108
+ * times(5, 'a'); // => ['a', 'a', 'a', 'a', 'a']
109
+ * ```
74
110
  */
75
111
  export function times<Value>(length: number, value: Value): Value[];
76
112
 
package/src/array/get.ts CHANGED
@@ -5,8 +5,15 @@ import type {NumericalKeys, PlainObject} from '../models';
5
5
 
6
6
  /**
7
7
  * Get an array from an object, where only values with numerical keys will be included
8
+ *
8
9
  * @param value Object to convert to an array
9
10
  * @returns Array holding the values of the object's numerical keys
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * getArray({0: 'a', 1: 'b', 2: 'c', d: 'd'}, true); // => ['a', 'b', 'c']
15
+ * getArray({a: 'a', b: 'b', c: 'c', d: 'd'}, true); // => []
16
+ * ```
10
17
  */
11
18
  export function getArray<Value extends PlainObject>(
12
19
  value: Value,
@@ -15,32 +22,31 @@ export function getArray<Value extends PlainObject>(
15
22
 
16
23
  /**
17
24
  * Get an array from an object
25
+ *
18
26
  * @param value Object to convert to an array
19
27
  * @returns Array holding the values of the object
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * getArray({0: 'a', 1: 'b', 2: 'c', d: 'd'}); // => ['a', 'b', 'c', 'd']
32
+ * getArray({a: 'a', b: 'b', c: 'c', d: 'd'}); // => ['a', 'b', 'c', 'd']
33
+ * ```
20
34
  */
21
35
  export function getArray<Value extends PlainObject>(value: Value): Value[keyof Value][];
22
36
 
23
37
  /**
24
- * Get an array
38
+ * Get an array from a value
39
+ *
25
40
  * @param value Original array
26
41
  * @returns Original array
42
+ *
43
+ * @example
44
+ * ```typescript
45
+ * getArray(123); // => [123]
46
+ * ```
27
47
  */
28
48
  export function getArray<Item>(value: Item[]): Item[];
29
49
 
30
- /**
31
- * Get an array from a value
32
- * @param value Value to convert to an array
33
- * @returns Array holding the value
34
- */
35
- export function getArray<Item>(value: Item): Item[];
36
-
37
- /**
38
- * Get an array from an unknown value
39
- * @param value Value to convert to an array
40
- * @returns Array of value
41
- */
42
- export function getArray(value: unknown): unknown[];
43
-
44
50
  export function getArray(value: unknown, indiced?: unknown): unknown[] {
45
51
  if (Array.isArray(value)) {
46
52
  return value;
@@ -7,10 +7,20 @@ import type {Key, KeyedValue, PlainObject, Simplify} from '../models';
7
7
  * Create a record from an array of items using a specific key and value
8
8
  *
9
9
  * If multiple items have the same key, the latest item's value will be used
10
+ *
10
11
  * @param array Array to group
11
12
  * @param key Callback to get an item's grouping key
12
13
  * @param value Callback to get an item's value
13
14
  * @returns Record of keyed values
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * groupBy(
19
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
20
+ * item => item.value,
21
+ * item => item,
22
+ * ); // => {10: {id: 3, value: 10}, 20: {id: 2, value: 20}}
23
+ * ```
14
24
  */
15
25
  export function groupBy<
16
26
  Item,
@@ -26,10 +36,20 @@ export function groupBy<
26
36
  * Create a record from an array of items using a specific key and value
27
37
  *
28
38
  * If multiple items have the same key, the latest item's value will be used
39
+ *
29
40
  * @param array Array to group
30
41
  * @param key Callback to get an item's grouping key
31
42
  * @param value Key to use for value
32
43
  * @returns Record of keyed values
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * groupBy(
48
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
49
+ * item => item.value,
50
+ * 'id'
51
+ * ); // => {10: 3, 20: 2}
52
+ * ```
33
53
  */
34
54
  export function groupBy<
35
55
  Item extends PlainObject,
@@ -45,10 +65,20 @@ export function groupBy<
45
65
  * Create a record from an array of items using a specific key and value
46
66
  *
47
67
  * If multiple items have the same key, the latest item's value will be used
68
+ *
48
69
  * @param array Array to group
49
70
  * @param key Key to use for grouping
50
71
  * @param value Callback to get an item's value
51
72
  * @returns Record of keyed values
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * groupBy(
77
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
78
+ * 'value',
79
+ * item => item,
80
+ * ); // => {10: {id: 3, value: 10}, 20: {id: 2, value: 20}}
81
+ * ```
52
82
  */
53
83
  export function groupBy<
54
84
  Item extends PlainObject,
@@ -64,10 +94,20 @@ export function groupBy<
64
94
  * Create a record from an array of items using a specific key and value
65
95
  *
66
96
  * If multiple items have the same key, the latest item's value will be used
97
+ *
67
98
  * @param array Array to group
68
99
  * @param key Key to use for grouping
69
100
  * @param value Key to use for value
70
101
  * @returns Record of keyed values
102
+ *
103
+ * @example
104
+ * ```typescript
105
+ * groupBy(
106
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
107
+ * 'value',
108
+ * 'id'
109
+ * ); // => {10: 3, 20: 2}
110
+ * ```
71
111
  */
72
112
  export function groupBy<
73
113
  Item extends PlainObject,
@@ -83,9 +123,18 @@ export function groupBy<
83
123
  * Create a record from an array of items using a specific key
84
124
  *
85
125
  * If multiple items have the same key, the latest item will be used
126
+ *
86
127
  * @param array Array to group
87
128
  * @param callback Callback to get an item's grouping key
88
129
  * @returns Record of keyed items
130
+ *
131
+ * @example
132
+ * ```typescript
133
+ * groupBy(
134
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
135
+ * item => item.value,
136
+ * ); // => {10: {id: 3, value: 10}, 20: {id: 2, value: 20}}
137
+ * ```
89
138
  */
90
139
  export function groupBy<Item, Callback extends (item: Item, index: number, array: Item[]) => Key>(
91
140
  array: Item[],
@@ -96,9 +145,18 @@ export function groupBy<Item, Callback extends (item: Item, index: number, array
96
145
  * Create a record from an array of items using a specific key
97
146
  *
98
147
  * If multiple items have the same key, the latest item will be used
148
+ *
99
149
  * @param array Array to group
100
150
  * @param key Key to use for grouping
101
151
  * @returns Record of keyed items
152
+ *
153
+ * @example
154
+ * ```typescript
155
+ * groupBy(
156
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
157
+ * 'value',
158
+ * ); // => {10: {id: 3, value: 10}, 20: {id: 2, value: 20}}
159
+ * ```
102
160
  */
103
161
  export function groupBy<Item extends PlainObject, ItemKey extends keyof Item>(
104
162
  array: Item[],
@@ -107,8 +165,16 @@ export function groupBy<Item extends PlainObject, ItemKey extends keyof Item>(
107
165
 
108
166
  /**
109
167
  * Create a record from an array of items _(using indices as keys)_
168
+ *
110
169
  * @param array Array to group
111
170
  * @returns Record of indiced items
171
+ *
172
+ * @example
173
+ * ```typescript
174
+ * groupBy(
175
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
176
+ * ); // => {0: {id: 1, value: 10}, 1: {id: 2, value: 20}, 2: {id: 3, value: 10}}
177
+ * ```
112
178
  */
113
179
  export function groupBy<Item>(array: Item[]): Record<number, Item>;
114
180
 
@@ -122,10 +188,23 @@ groupBy.arrays = groupArraysBy;
122
188
  * Create a record from an array of items using a specific key and value, grouping values into arrays
123
189
  *
124
190
  * Available as `groupArraysBy` and `groupBy.arrays`
191
+ *
125
192
  * @param array Array to group
126
193
  * @param key Callback to get an item's grouping key
127
194
  * @param value Callback to get an item's value
128
195
  * @returns Record of keyed values
196
+ *
197
+ * @example
198
+ * ```typescript
199
+ * groupArraysBy(
200
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
201
+ * item => item.value,
202
+ * item => item,
203
+ * ); // => {
204
+ * // 10: [{id: 1, value: 10}, {id: 3, value: 10}],
205
+ * // 20: [{id: 2, value: 20}],
206
+ * // }
207
+ * ```
129
208
  */
130
209
  export function groupArraysBy<
131
210
  Item,
@@ -141,10 +220,23 @@ export function groupArraysBy<
141
220
  * Create a record from an array of items using a specific key and value, grouping values into arrays
142
221
  *
143
222
  * Available as `groupArraysBy` and `groupBy.arrays`
223
+ *
144
224
  * @param array Array to group
145
225
  * @param key Callback to get an item's grouping key
146
226
  * @param value Key to use for value
147
227
  * @returns Record of keyed values
228
+ *
229
+ * @example
230
+ * ```typescript
231
+ * groupArraysBy(
232
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
233
+ * item => item.value,
234
+ * 'id',
235
+ * ); // => {
236
+ * // 10: [1, 3],
237
+ * // 20: [2],
238
+ * // }
239
+ * ```
148
240
  */
149
241
  export function groupArraysBy<
150
242
  Item extends PlainObject,
@@ -160,10 +252,23 @@ export function groupArraysBy<
160
252
  * Create a record from an array of items using a specific key and value, grouping values into arrays
161
253
  *
162
254
  * Available as `groupArraysBy` and `groupBy.arrays`
255
+ *
163
256
  * @param array Array to group
164
257
  * @param key Key to use for grouping
165
258
  * @param value Callback to get an item's value
166
259
  * @returns Record of keyed values
260
+ *
261
+ * @example
262
+ * ```typescript
263
+ * groupArraysBy(
264
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
265
+ * 'value',
266
+ * item => item,
267
+ * ); // => {
268
+ * // 10: [{id: 1, value: 10}, {id: 3, value: 10}],
269
+ * // 20: [{id: 2, value: 20}],
270
+ * // }
271
+ * ```
167
272
  */
168
273
  export function groupArraysBy<
169
274
  Item extends PlainObject,
@@ -179,10 +284,23 @@ export function groupArraysBy<
179
284
  * Create a record from an array of items using a specific key and value, grouping values into arrays
180
285
  *
181
286
  * Available as `groupArraysBy` and `groupBy.arrays`
287
+ *
182
288
  * @param array Array to group
183
289
  * @param key Key to use for grouping
184
290
  * @param value Key to use for value
185
291
  * @returns Record of keyed values
292
+ *
293
+ * @example
294
+ * ```typescript
295
+ * groupArraysBy(
296
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
297
+ * 'value',
298
+ * 'id',
299
+ * ); // => {
300
+ * // 10: [1, 3],
301
+ * // 20: [2],
302
+ * // }
303
+ * ```
186
304
  */
187
305
  export function groupArraysBy<
188
306
  Item extends PlainObject,
@@ -198,9 +316,21 @@ export function groupArraysBy<
198
316
  * Create a record from an array of items using a specific key, grouping items into arrays
199
317
  *
200
318
  * Available as `groupArraysBy` and `groupBy.arrays`
319
+ *
201
320
  * @param array Array to group
202
321
  * @param callback Callback to get an item's grouping key
203
322
  * @returns Record of keyed items
323
+ *
324
+ * @example
325
+ * ```typescript
326
+ * groupArraysBy(
327
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
328
+ * item => item.value,
329
+ * ); // => {
330
+ * // 10: [{id: 1, value: 10}, {id: 3, value: 10}],
331
+ * // 20: [{id: 2, value: 20}],
332
+ * // }
333
+ * ```
204
334
  */
205
335
  export function groupArraysBy<
206
336
  Item,
@@ -211,9 +341,21 @@ export function groupArraysBy<
211
341
  * Create a record from an array of items using a specific key, grouping items into arrays
212
342
  *
213
343
  * Available as `groupArraysBy` and `groupBy.arrays`
344
+ *
214
345
  * @param array Array to group
215
346
  * @param key Key to use for grouping
216
347
  * @returns Record of keyed items
348
+ *
349
+ * @example
350
+ * ```typescript
351
+ * groupArraysBy(
352
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
353
+ * 'value',
354
+ * ); // => {
355
+ * // 10: [{id: 1, value: 10}, {id: 3, value: 10}],
356
+ * // 20: [{id: 2, value: 20}],
357
+ * // }
358
+ * ```
217
359
  */
218
360
  export function groupArraysBy<Item extends PlainObject, ItemKey extends keyof Item>(
219
361
  array: Item[],
@@ -2,22 +2,36 @@ import {INSERT_TYPE_INSERT, insertValues} from '../internal/array/insert';
2
2
 
3
3
  // #region Functions
4
4
 
5
- // Uses chunking to avoid call stack size being exceeded
6
-
7
5
  /**
8
6
  * Insert items into an array at a specified index
7
+ *
8
+ * _(Uses chunking to avoid call stack size being exceeded)_
9
+ *
9
10
  * @param array Original array
10
11
  * @param index Index to insert at
11
12
  * @param items Inserted items
12
13
  * @returns Updated array
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * insert([1, 2, 3], 1, [4, 5]); // => [1, 4, 5, 2, 3]
18
+ * ```
13
19
  */
14
20
  export function insert<Item>(array: Item[], index: number, items: Item[]): Item[];
15
21
 
16
22
  /**
17
23
  * Insert items into an array _(at the end)_
24
+ *
25
+ * _(Uses chunking to avoid call stack size being exceeded)_
26
+ *
18
27
  * @param array Original array
19
28
  * @param items Inserted items
20
29
  * @returns Updated array
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * insert([1, 2, 3], [4, 5]); // => [1, 2, 3, 4, 5]
34
+ * ```
21
35
  */
22
36
  export function insert<Item>(array: Item[], items: Item[]): Item[];
23
37
 
@@ -4,10 +4,20 @@ import {COMPARE_SETS_INTERSECTION, compareSets} from '../internal/array/sets';
4
4
 
5
5
  /**
6
6
  * Get the common values between two arrays
7
+ *
7
8
  * @param first First array
8
9
  * @param second Second array
9
10
  * @param callback Callback to get an item's value for comparison
10
11
  * @returns Common values from both arrays
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * intersection(
16
+ * [{id: 1}, {id: 2}, {id: 3}],
17
+ * [{id: 2}, {id: 3}, {id: 4}],
18
+ * item => item.id,
19
+ * ); // => [{id: 2}, {id: 3}]
20
+ * ```
11
21
  */
12
22
  export function intersection<First, Second>(
13
23
  first: First[],
@@ -17,10 +27,20 @@ export function intersection<First, Second>(
17
27
 
18
28
  /**
19
29
  * Get the common values between two arrays
30
+ *
20
31
  * @param first First array
21
32
  * @param second Second array
22
33
  * @param key Key to get an item's value for comparison
23
34
  * @returns Common values from both arrays
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * intersection(
39
+ * [{id: 1}, {id: 2}, {id: 3}],
40
+ * [{id: 2}, {id: 3}, {id: 4}],
41
+ * 'id',
42
+ * ); // => [{id: 2}, {id: 3}]
43
+ * ```
24
44
  */
25
45
  export function intersection<
26
46
  First extends Record<string, unknown>,
@@ -30,9 +50,18 @@ export function intersection<
30
50
 
31
51
  /**
32
52
  * Get the common values between two arrays
53
+ *
33
54
  * @param first First array
34
55
  * @param second Second array
35
56
  * @returns Common values from both arrays
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * intersection(
61
+ * [1, 2, 3],
62
+ * [2, 3, 4],
63
+ * ); // => [2, 3]
64
+ * ```
36
65
  */
37
66
  export function intersection<First, Second>(first: First[], second: Second[]): First[];
38
67