MCPcopy Create free account
hub / github.com/clientdb/clientdb / getArraysCommonPart

Function getArraysCommonPart

core/utils/arrays.ts:30–51  ·  view source on GitHub ↗
(...arrays: Array<T[]>)

Source from the content-addressed store, hash-verified

28}
29
30export function getArraysCommonPart<T extends object>(...arrays: Array<T[]>) {
31 if (arrays.length === 0) return [];
32 if (arrays.length === 1) return arrays[0];
33
34 const fromShortest = sortBy(arrays, (array) => array.length);
35
36 const [first, ...arraysToCheck] = fromShortest;
37
38 const remainingItems = new Set(first);
39
40 for (const nextArray of arraysToCheck) {
41 for (const remainingItem of remainingItems) {
42 if (!nextArray.includes(remainingItem)) {
43 remainingItems.delete(remainingItem);
44
45 if (remainingItems.size === 0) return [];
46 }
47 }
48 }
49
50 return Array.from(remainingItems);
51}
52
53export function areArraysShallowEqual<T>(a: T[], b: T[]) {
54 if (a === b) return true;

Calls

no outgoing calls

Tested by

no test coverage detected