UNPKG

mahler

Version:

A automated task composer and HTN based planner for building autonomous system agents

43 lines 1.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Pointer = exports.InvalidPointer = void 0; const path_1 = require("./path"); const is_array_index_1 = require("./is-array-index"); class InvalidPointer extends Error { constructor(path, obj) { super(`Path ${path} is not a valid pointer for object ${JSON.stringify(obj)}`); } } exports.InvalidPointer = InvalidPointer; function from(obj, path) { const parts = path_1.Path.split(path); // Save the last element of the path so we can delete it const last = parts.pop(); let o = obj; for (const p of parts) { if (!Array.isArray(o) && typeof o !== 'object') { throw new InvalidPointer(path, obj); } if (Array.isArray(o) && !(0, is_array_index_1.isArrayIndex)(p)) { throw new InvalidPointer(path, obj); } if (!(p in o)) { throw new InvalidPointer(path, obj); } o = o[p]; } if (last != null) { if (!Array.isArray(o) && typeof o !== 'object') { throw new InvalidPointer(path, obj); } if (Array.isArray(o) && !(0, is_array_index_1.isArrayIndex)(last)) { throw new InvalidPointer(path, obj); } return o[last]; } return o; } exports.Pointer = { from, }; //# sourceMappingURL=pointer.js.map