e2ed
Version:
E2E testing framework over Playwright
37 lines (36 loc) • 1.42 kB
JavaScript
;
/* eslint-disable no-param-reassign */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getReplacedObject = void 0;
const getReplacedFields_1 = require("./getReplacedFields");
const hasToJsonMethod_1 = require("./hasToJsonMethod");
const maxPathLength = 500;
/**
* Recursively get replaced object with replaced fields.
* @internal
*/
const getReplacedObject = (path, originalValue, replacer) => {
const pathLength = path.length;
if (pathLength > maxPathLength) {
return originalValue;
}
const value = ((0, hasToJsonMethod_1.hasToJsonMethod)(originalValue) ? originalValue.toJSON(path.at(-1)) : originalValue);
const replacedObject = (Array.isArray(value) ? [] : {});
for (const field of (0, getReplacedFields_1.getReplacedFields)(value)) {
path[pathLength] = field;
const fieldValue = value[field];
let newFieldValue = replacer(path, fieldValue, value);
if (newFieldValue === undefined) {
continue;
}
if (newFieldValue !== null &&
typeof newFieldValue === 'object' &&
!(newFieldValue instanceof String)) {
newFieldValue = (0, exports.getReplacedObject)(path, newFieldValue, replacer);
}
replacedObject[field] = newFieldValue;
}
path.length = pathLength;
return replacedObject;
};
exports.getReplacedObject = getReplacedObject;