MCPcopy Create free account
hub / github.com/LittleWhite-Hai/diff-viz / getObjectPathArrayMap

Function getObjectPathArrayMap

src/diff-algorithm.ts:211–234  ·  view source on GitHub ↗
(data: any)

Source from the content-addressed store, hash-verified

209 * @returns
210 */
211export function getObjectPathArrayMap(data: any) {
212 const mapResult: Record<string, Array<any>> = {};
213
214 function traverse(obj: any, path: string = "") {
215 if (typeof obj !== "object" || obj === null || obj === undefined) {
216 return;
217 }
218 if (Array.isArray(obj)) {
219 mapResult[path] = obj;
220 }
221 for (const [key, value] of Object.entries(obj)) {
222 const newPath = path ? `${path}.${key}` : key;
223 traverse(value, newPath);
224 }
225 }
226
227 traverse(data);
228 // 将结果转换成数组,并且按照字母顺序排序,格式是{path:string,value:BaseType}[]
229 const arrayResult = Array.from(Object.entries(mapResult))
230 .sort((a, b) => a[0].localeCompare(b[0]))
231 .map(([path, value]) => ({ path, value }));
232
233 return { mapResult, arrayResult };
234}
235
236type DataArrayType = {
237 path: string;

Callers 1

calcArrayAlignFunction · 0.85

Calls 1

traverseFunction · 0.85

Tested by

no test coverage detected