UNPKG

json-deep-compare

Version:

A powerful library for comparing JSON objects with support for deep comparison, regex validation, and customizable options

1 lines 729 B
class PathUtils{static getAllPaths(t,e="",s=[]){if(null===t||"object"!=typeof t)return s.push(e),s;if(Array.isArray(t))if(0===t.length)s.push(e);else for(let l=0;l<t.length;l++){const r=e?`${e}[${l}]`:`[${l}]`;PathUtils.getAllPaths(t[l],r,s)}else{const l=Object.keys(t);if(0===l.length)s.push(e);else for(const r of l){const l=e?`${e}.${r}`:r;PathUtils.getAllPaths(t[r],l,s)}}return s}static getValueAtPath(t,e){const s=e.replace(/\[(\d+)\]/g,".$1").split(".");let l=t;for(const t of s){if(null===l||"object"!=typeof l)return;l=l[t]}return l}static getKeyNameFromPath(t){return t.split(".").pop()?.split("[")[0]}static buildPath(t,e){return t?`${t}.${e}`:e}static buildArrayPath(t,e){return`${t}[${e}]`}}module.exports=PathUtils;