UNPKG

higher-order-logical-operators

Version:

Allows combination of lower order functions that evaluate to booleans. Extends Array prototype with 'one' function, which takes a callback and returns true if the callback returns true for one and only one member of the array.

26 lines (22 loc) 654 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Array.prototype.one = function (callback) { return this.reduce(function (p, v, i, a) { return p + (callback(v, i, a) ? 1 : 0); }, 0) === 1; }; var xor = exports.xor = function xor() { for (var _len = arguments.length, fns = Array(_len), _key = 0; _key < _len; _key++) { fns[_key] = arguments[_key]; } return function () { for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } return fns.one(function (fn) { return fn.apply(undefined, args); }); }; };