@newdash/newdash
Version:
javascript/typescript utility library
36 lines (35 loc) • 1.3 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cond = void 0;
const getIteratee_1 = __importDefault(require("./.internal/getIteratee"));
const arrayMap_1 = __importDefault(require("./.internal/arrayMap"));
const baseRest_1 = __importDefault(require("./.internal/baseRest"));
const apply_1 = __importDefault(require("./.internal/apply"));
/**
* @ignore
* @private
*/
const FUNC_ERROR_TEXT = "Expected a function";
function cond(pairs) {
const length = pairs == null ? 0 : pairs.length, toIteratee = (0, getIteratee_1.default)();
pairs = !length ? [] : (0, arrayMap_1.default)(pairs, (pair) => {
if (typeof pair[1] != "function") {
throw new TypeError(FUNC_ERROR_TEXT);
}
return [toIteratee(pair[0]), pair[1]];
});
return (0, baseRest_1.default)(function (args) {
let index = -1;
while (++index < length) {
const pair = pairs[index];
if ((0, apply_1.default)(pair[0], this, args)) {
return (0, apply_1.default)(pair[1], this, args);
}
}
});
}
exports.cond = cond;
exports.default = cond;