@apideck/portman
Version:
Port OpenAPI Spec to Postman Collection, with contract & variation tests included
39 lines • 1.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getByPath = void 0;
var tslib_1 = require("tslib");
var dot_object_1 = tslib_1.__importDefault(require("dot-object"));
var isObject_1 = require("./isObject");
var getByPath = function (objectOrArray, path, defaultValue) {
var _a;
if (defaultValue === void 0) { defaultValue = undefined; }
if (!(0, isObject_1.isObject)(objectOrArray) && !Array.isArray(objectOrArray))
return objectOrArray;
if (path === '.')
return objectOrArray;
var dot = new dot_object_1.default();
var arrayPathCheck = path.endsWith(']');
var indexKey = (_a = path.match(/\d+/g)) === null || _a === void 0 ? void 0 : _a[0];
var flatInput = dot.dot(objectOrArray);
if (flatInput[path] && !arrayPathCheck)
return flatInput[path];
if (Array.isArray(objectOrArray)) {
if (indexKey && objectOrArray[indexKey] && !path.includes('.')) {
var res = objectOrArray.splice(Number(indexKey), 1);
return res[Object.keys(res)[0]];
}
if (indexKey && objectOrArray[indexKey] && path.includes('.')) {
var item = objectOrArray[indexKey];
var subPath = path.substring(path.indexOf('.') + 1);
if (subPath) {
return (0, exports.getByPath)(item, subPath);
}
}
}
var byPath = dot.pick(path, objectOrArray);
if (byPath)
return byPath;
return flatInput[path] || defaultValue;
};
exports.getByPath = getByPath;
//# sourceMappingURL=getByPath.js.map