@@ -153,6 +153,7 @@ export function sortedIndexBy<T>(
153
153
* R.sortedLastIndexBy(data, item, valueFunction)
154
154
* @example
155
155
* R.sortedLastIndexBy([{age:20},{age:22}],{age:21},prop('age')) // => 1
156
+ *
156
157
* MIT License | Copyright (c) 2018 remeda | https://remedajs.com/
157
158
*
158
159
* The implementation is copied from remeda package:
@@ -174,3 +175,106 @@ export function sortedLastIndexBy<T>(
174
175
( pivot , index ) => valueFunction ( pivot , index , array ) <= value ,
175
176
)
176
177
}
178
+
179
+ /**
180
+ * Creates a duplicate-free version of an array.
181
+ *
182
+ * This function takes an array and returns a new array containing only the unique values
183
+ * from the original array, preserving the order of first occurrence.
184
+ *
185
+ * @template T - The type of elements in the array.
186
+ * @param {T[] } arr - The array to process.
187
+ * @returns {T[] } A new array with only unique values from the original array.
188
+ *
189
+ * @example
190
+ * const array = [1, 2, 2, 3, 4, 4, 5];
191
+ * const result = uniq(array);
192
+ * // result will be [1, 2, 3, 4, 5]
193
+ *
194
+ * MIT © Viva Republica, Inc. | https://es-toolkit.dev/
195
+ *
196
+ * The implementation is copied from es-toolkit package:
197
+ * https://github.com/toss/es-toolkit/blob/16709839f131269b84cdd96e9645df52648ccedf/src/array/uniq.ts#L16
198
+ */
199
+ export function uniq < T > ( arr : readonly T [ ] ) : T [ ] {
200
+ return Array . from ( new Set ( arr ) )
201
+ }
202
+
203
+ /**
204
+ * Returns the intersection of multiple arrays.
205
+ *
206
+ * This function takes multiple arrays and returns a new array containing the elements that are
207
+ * present in all provided arrays. It effectively filters out any elements that are not found
208
+ * in every array.
209
+ *
210
+ * @template T - The type of elements in the arrays.
211
+ * @param {...(ArrayLike<T> | null | undefined) } arrays - The arrays to compare.
212
+ * @returns {T[] } A new array containing the elements that are present in all arrays.
213
+ *
214
+ * @example
215
+ * const array1 = [1, 2, 3, 4, 5];
216
+ * const array2 = [3, 4, 5, 6, 7];
217
+ * const result = intersection(array1, array2);
218
+ * // result will be [3, 4, 5] since these elements are in both arrays.
219
+ *
220
+ * MIT © Viva Republica, Inc. | https://es-toolkit.dev/
221
+ *
222
+ * The implementation is copied from es-toolkit package:
223
+ * https://github.com/toss/es-toolkit/blob/16709839f131269b84cdd96e9645df52648ccedf/src/compat/array/intersection.ts#L22
224
+ * https://github.com/toss/es-toolkit/blob/16709839f131269b84cdd96e9645df52648ccedf/src/array/intersection.ts#L19
225
+ */
226
+ export function intersection < T > ( ...arrays : ( T [ ] | null | undefined ) [ ] ) : T [ ] {
227
+ if ( arrays . length === 0 ) {
228
+ return [ ]
229
+ }
230
+
231
+ let result : T [ ] = uniq ( arrays [ 0 ] ! )
232
+
233
+ for ( let i = 1 ; i < arrays . length ; i ++ ) {
234
+ const array = arrays [ i ]
235
+ const secondSet = new Set ( array )
236
+
237
+ result = result . filter ( ( item ) => secondSet . has ( item ) )
238
+ }
239
+
240
+ return result
241
+ }
242
+
243
+ /**
244
+ * This function takes multiple arrays and returns a new array containing only the unique values
245
+ * from all input arrays, preserving the order of their first occurrence.
246
+ *
247
+ * @template T - The type of elements in the arrays.
248
+ * @param {Array<ArrayLike<T> | null | undefined> } arrays - The arrays to inspect.
249
+ * @returns {T[] } Returns the new array of combined unique values.
250
+ *
251
+ * @example
252
+ * // Returns [2, 1]
253
+ * union([2], [1, 2]);
254
+ *
255
+ * @example
256
+ * // Returns [2, 1, 3]
257
+ * union([2], [1, 2], [2, 3]);
258
+ *
259
+ * @example
260
+ * // Returns [1, 3, 2, [5], [4]] (does not deeply flatten nested arrays)
261
+ * union([1, 3, 2], [1, [5]], [2, [4]]);
262
+ *
263
+ * @example
264
+ * // Returns [0, 2, 1] (ignores non-array values like 3 and { '0': 1 })
265
+ * union([0], 3, { '0': 1 }, null, [2, 1]);
266
+ * @example
267
+ * // Returns [0, 'a', 2, 1] (treats array-like object { 0: 'a', length: 1 } as a valid array)
268
+ * union([0], { 0: 'a', length: 1 }, [2, 1]);
269
+ *
270
+ * MIT © Viva Republica, Inc. | https://es-toolkit.dev/
271
+ *
272
+ * The implementation is copied from es-toolkit package:
273
+ * https://github.com/toss/es-toolkit/blob/16709839f131269b84cdd96e9645df52648ccedf/src/compat/array/union.ts#L61
274
+ * https://github.com/toss/es-toolkit/blob/16709839f131269b84cdd96e9645df52648ccedf/src/compat/array/flattenDepth.ts#L21
275
+ */
276
+ export function union < T > ( ...arrays : T [ ] [ ] ) : T [ ] {
277
+ const flattened = arrays . flat ( )
278
+
279
+ return uniq ( flattened )
280
+ }
0 commit comments