Skip to content

Commit faac1a2

Browse files
committed
refactor: drop union, intersection
1 parent 342f931 commit faac1a2

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

src/external/node-event-generator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import type EventEmitter from "events"
55
import type { ESQueryOptions, Selector } from "esquery"
66
import esquery from "esquery"
7-
import { union, intersection, memoize } from "lodash-es"
7+
import { memoize } from "lodash-es"
8+
import { union, intersection } from "../utils/utils"
89
import type { Node } from "../ast/index"
910

1011
interface NodeSelector {

src/utils/utils.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export function sortedIndexBy<T>(
153153
* R.sortedLastIndexBy(data, item, valueFunction)
154154
* @example
155155
* R.sortedLastIndexBy([{age:20},{age:22}],{age:21},prop('age')) // => 1
156+
*
156157
* MIT License | Copyright (c) 2018 remeda | https://remedajs.com/
157158
*
158159
* The implementation is copied from remeda package:
@@ -174,3 +175,106 @@ export function sortedLastIndexBy<T>(
174175
(pivot, index) => valueFunction(pivot, index, array) <= value,
175176
)
176177
}
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

Comments
 (0)