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
32 lines • 1.07 kB
JavaScript
import { type } from '../type';
import { hasMethod } from '../has-method';
/**
* Converts a data structure through recursive calls to toJSON methods if they exist for each value.
* If the method doesn't exist, the original value is returned.
* Values for which there is no call method will remain in their original value.
* @param value Value for conversion
* @param recursive Perform nested processing
* @returns The converted value
*/
export function plain(value, recursive = true) {
if (value === null || typeof value === 'undefined') {
return value;
}
else if (hasMethod(value, 'toJSON')) {
value = value.toJSON();
}
if (recursive) {
if (Array.isArray(value)) {
return value.map((item) => plain(item));
}
else if (type(value) === 'object') {
const result = {};
for (const [key, item] of Object.entries(value)) {
result[key] = plain(item);
}
return result;
}
}
return value;
}
//# sourceMappingURL=index.js.map