UNPKG

pddl-workspace

Version:
71 lines 2.46 kB
"use strict"; /* -------------------------------------------------------------------------------------------- * Copyright (c) Jan Dolejsi 2020. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. * ------------------------------------------------------------------------------------------ */ Object.defineProperty(exports, "__esModule", { value: true }); exports.makeSerializable = exports.objToStrMap = exports.strMapToObj = exports.asSerializable = void 0; // eslint-disable-next-line @typescript-eslint/no-explicit-any function asSerializable(obj) { if (obj === undefined || obj === null) { return obj; } else if (obj instanceof Map) { return strMapToObj(obj); } else if (obj instanceof Array) { return obj.map(o => asSerializable(o)); } else if (obj instanceof Set) { return [...obj].map(o => asSerializable(o)); } else if (obj instanceof Object) { const serObj = Object.create(null); Object.keys(obj).forEach(key => serObj[key] = asSerializable(obj[key])); return serObj; } else { return obj; } } exports.asSerializable = asSerializable; function strMapToObj(strMap) { const obj = Object.create(null); for (const [k, v] of strMap) { obj[k] = asSerializable(v); } return obj; } exports.strMapToObj = strMapToObj; // eslint-disable-next-line @typescript-eslint/no-explicit-any function objToStrMap(obj) { const strMap = new Map(); for (const k of Object.keys(obj)) { strMap.set(k, obj[k]); } return strMap; } exports.objToStrMap = objToStrMap; /** * Removes circular dependencies to make JSON-serialization safe. * @param orig original object */ function makeSerializable(orig) { // eslint-disable-next-line @typescript-eslint/no-explicit-any const getCircularReplacer = () => { const seen = new WeakSet(); // eslint-disable-next-line @typescript-eslint/no-explicit-any return (_key, value) => { if (typeof value === "object" && value !== null) { if (seen.has(value)) { return; } seen.add(value); } return value; }; }; return JSON.parse(JSON.stringify(orig, getCircularReplacer())); } exports.makeSerializable = makeSerializable; //# sourceMappingURL=serializationUtils.js.map