UNPKG

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

35 lines 1.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.plain = plain; const type_1 = require("../type"); const has_method_1 = require("../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 */ function plain(value, recursive = true) { if (value === null || typeof value === 'undefined') { return value; } else if ((0, has_method_1.hasMethod)(value, 'toJSON')) { value = value.toJSON(); } if (recursive) { if (Array.isArray(value)) { return value.map((item) => plain(item)); } else if ((0, type_1.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