merge-change
Version:
Advanced library for deep merging, patching, and immutable updates of data structures. Features declarative operations for specific merging behaviors, property management, custom type merging rules, and difference tracking. Supports complex data transform
37 lines • 1.49 kB
JavaScript
/**
* Retrieves the value at the specified path in a deeply nested data structure. If the path does not exist,
* the function returns the provided default value.
*
* @param data - The data object or array from which to retrieve the value.
* @param path - The path string to the desired value, with parts separated by the specified separator.
* @param [defaultValue=undefined] - The value to return if the path does not exist.
* @param [separator='.'] - The string used to separate path segments. Defaults to '.'.
* @return The value at the specified path in the data structure or the default value if the path is invalid.
*/
export function get(data, path, defaultValue = undefined, separator = '.') {
const parts = path.split(separator);
// If the path starts with a separator, the first element will be an empty string, remove it
if (parts[0] === '') {
parts.shift();
}
let current = data;
for (const part of parts) {
if (current && typeof current === 'object' && part in current) {
current = current[part];
}
else if (Array.isArray(current) && /^\d+$/.test(part)) {
const index = parseInt(part, 10);
if (index < current.length) {
current = current[index];
}
else {
return defaultValue;
}
}
else {
return defaultValue;
}
}
return current;
}
//# sourceMappingURL=index.js.map