Repository URL to install this package:
|
Version:
6.0.4 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var is_1 = require("../is");
var propSatisfies_1 = require("../fp/propSatisfies");
var getIsFunction = propSatisfies_1.default('get', is_1.isFunction);
/**
* @desc when the condition is true,
* trueBrancher is called,
* else, falseBrancher is called
*
* @memberOf Chainable
* @version 5.0.0 <- moved to fp from class
* @version 4.0.0 <- added string-as-has(condition)
* @since 2.0.0
*
* @param {boolean | string} condition when string, checks this.get
* @param {Function} [trueBrancher=Function] called when true
* @param {Function} [falseBrancher=Function] called when false
* @return {Chainable} @chainable
*
* @tests fp/when
*
* @example
*
*
* const prod = process.env.NODE_ENV === 'production'
* chains.when(prod, c => c.set('prod', true), c => c.set('prod', false))
*
*
*/
function when(condition, trueBrancher, falseBrancher) {
// truthy condition - could be string
if (condition) {
// ensure we have functions
if (is_1.isFunction(trueBrancher)) {
// if we have a .get function, and we use a string, use that
if (is_1.isString(condition) && getIsFunction(this)) {
if (this.get(condition)) {
trueBrancher(this);
}
}
else {
trueBrancher(this);
}
}
}
else if (is_1.isFunction(falseBrancher)) {
// ensure function, on else
falseBrancher(this);
}
// chainable
return this;
}
exports.default = when;
//# sourceMappingURL=when.js.map