MCPcopy Create free account
hub / github.com/Superflows-AI/superflows / deduplicateArray

Function deduplicateArray

lib/utils.ts:183–222  ·  view source on GitHub ↗
(
  arr: ArrType,
)

Source from the content-addressed store, hash-verified

181}
182
183export function deduplicateArray<ArrType extends any[]>(
184 arr: ArrType,
185): Partial<ArrType | { items: Partial<ArrType>[] }> {
186 if (arr.length === 0) return {};
187 // Check for null, undefined, string, number, boolean or array
188 if (
189 arr.some((ele) => !ele || typeof ele !== "object" || Array.isArray(ele))
190 ) {
191 return arr;
192 }
193 const firstEle = arr[0];
194 let output: Record<string, any> = { items: [] };
195
196 Object.keys(firstEle).forEach((key) => {
197 const allValues = arr.map((ele) => ele[key]);
198 if (allValues.every((val) => val === allValues[0])) {
199 output[key] = allValues[0];
200 } else if (output.items.length === 0) {
201 output.items = arr.map((ele) => ({ [key]: ele[key] }));
202 } else {
203 output.items.forEach((item: any, i: number) => {
204 item[key] = arr[i][key];
205 });
206 }
207 });
208 if (output.items.length === 0) {
209 delete output.items;
210 } else {
211 output.items = output.items.map((item: any) => {
212 return deleteUndefined(item);
213 });
214 }
215
216 // If there's no duplication (everything in 'items') then keep it as an array
217 if (Object.keys(output).length === 1 && "items" in output) {
218 return output.items;
219 }
220
221 return output;
222}
223
224export function filterKeys<InputObject extends any>(
225 obj: InputObject,

Callers 2

processAPIoutputFunction · 0.90

Calls 1

deleteUndefinedFunction · 0.70

Tested by

no test coverage detected