UNPKG

model-layer

Version:
142 lines 4.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.clone = exports.value2json = exports.equal = exports.AnyType = void 0; const Type_1 = require("./Type"); const Model_1 = require("../Model"); const utils_1 = require("../utils"); const errors_1 = require("../errors"); class AnyType extends Type_1.Type { toJSON(value, stack) { return value2json(value, stack); } clone(value, stack) { return clone(value, stack); } equal(selfValue, otherValue, stack) { return equal(selfValue, otherValue, stack); } } exports.AnyType = AnyType; function equal(selfValue, otherValue, stack) { if (selfValue instanceof Date && otherValue instanceof Date) { return +selfValue === +otherValue; } if (selfValue instanceof RegExp && otherValue instanceof RegExp) { return selfValue.toString() === otherValue.toString(); } if (Array.isArray(selfValue) && Array.isArray(otherValue)) { if (selfValue.length !== otherValue.length) { return false; } // stop circular recursion const stacked = stack.get(selfValue); if (stacked) { return stacked === otherValue; } stack.add(selfValue, otherValue); for (let i = 0, n = selfValue.length; i < n; i++) { const selfItem = selfValue[i]; const otherItem = otherValue[i]; const isEqualItem = equal(selfItem, otherItem, stack); if (!isEqualItem) { return false; } } return true; } if (utils_1.isPlainObject(selfValue) && utils_1.isPlainObject(otherValue)) { // stop circular recursion const stacked = stack.get(selfValue); if (stacked) { return true; } stack.add(selfValue, otherValue); const selfObj = selfValue; const otherObj = otherValue; for (const key in selfObj) { const myValue = selfObj[key]; const himValue = otherObj[key]; const isEqual = equal(myValue, himValue, stack); if (!isEqual) { return false; } } // check additional keys from otherObj for (const key in otherObj) { if (key in selfObj) { continue; } // exists unknown property for selfObj return false; } return true; } if (selfValue instanceof Model_1.Model && otherValue instanceof Model_1.Model) { const stacked = stack.get(selfValue); if (stacked) { return true; } stack.add(selfValue, otherValue); return selfValue.equal(otherValue, stack); } if (utils_1.isNaN(selfValue) && utils_1.isNaN(otherValue)) { return true; } return selfValue === otherValue; } exports.equal = equal; function value2json(value, stack) { if (value instanceof Date) { return value.toISOString(); } if (value && typeof value.toJSON === "function") { if (stack.includes(value)) { throw new errors_1.CircularStructureToJSONError({}); } stack.push(value); return value.toJSON([...stack]); } if (Array.isArray(value)) { if (stack.includes(value)) { throw new errors_1.CircularStructureToJSONError({}); } stack.push(value); return value.map((item) => value2json(item, [...stack])); } if (utils_1.isObject(value)) { if (stack.includes(value)) { throw new errors_1.CircularStructureToJSONError({}); } stack.push(value); const json = {}; for (const key in value) { const item = value[key]; json[key] = value2json(item, [...stack]); } return json; } return value; } exports.value2json = value2json; function clone(value, stack) { if (value instanceof Date) { return new Date(+value); } if (value instanceof Model_1.Model) { return value.clone(stack); } if (Array.isArray(value)) { return value.map((item) => clone(item, stack)); } if (utils_1.isObject(value)) { const cloneObj = {}; for (const key in value) { const item = value[key]; cloneObj[key] = clone(item, stack); } return cloneObj; } return value; } exports.clone = clone; //# sourceMappingURL=AnyType.js.map