@edgeguideab/expect
Version:
Check for user input in a consistent way and generate error messages for missings
51 lines (50 loc) • 1.55 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDeepOptions = exports.getDeep = void 0;
var isRecord_1 = __importDefault(require("./isRecord"));
var validation_1 = require("./validation");
function getDeep(chain, values) {
if (!(0, isRecord_1.default)(values))
return values;
if ((0, validation_1.isUnsafe)(chain)) {
return undefined;
}
if (typeof chain === "string") {
return values[chain];
}
if (!Array.isArray(chain))
return undefined;
if (!chain.length)
return values;
var key = chain[0];
if ((0, validation_1.isUnsafe)(key)) {
return undefined;
}
return getDeep(chain.slice(1), values[key]);
}
exports.getDeep = getDeep;
function getDeepOptions(chain, options) {
if ((0, validation_1.isUnsafe)(chain)) {
return undefined;
}
if (options === undefined)
return undefined;
if (!Array.isArray(chain))
return options[chain];
if (!chain.length)
return options;
var key = chain[0];
if ((0, validation_1.isUnsafe)(key)) {
return undefined;
}
var nextOptions = options[key];
if ("items" in options)
nextOptions = options.items[key];
if ("keys" in options)
nextOptions = options.keys[key];
return getDeepOptions(chain.slice(1), nextOptions);
}
exports.getDeepOptions = getDeepOptions;