@bryandbor/selector-utils
Version:
Utilities for selectors
393 lines (344 loc) • 13.2 kB
JavaScript
/**
* Selector which sums all values together
* @param {...number} values The values which should be summed
* @returns {number} Sum of all values
*/
var sum = function sum() {
for (var _len = arguments.length, values = Array(_len), _key = 0; _key < _len; _key++) {
values[_key] = arguments[_key];
}
return values.reduce(function (sum, val) {
return sum + val;
}, 0);
};
/**
* Selector which subtracts all other values from the first
* @param {number} initialValue The value from which all other values will be subtracted
* @param {...number} values The values to be subtracted from the initial value
* @returns {number} The difference between the initial values and all others
*/
var difference = function difference(initialValue) {
for (var _len2 = arguments.length, values = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
values[_key2 - 1] = arguments[_key2];
}
return initialValue - sum.apply(undefined, values);
};
/**
* Selector which multiplies all values together and returns the result
* @param {...number} values The values which will be multplied together
* @returns {number} The product of all values
*/
var product = function product() {
for (var _len3 = arguments.length, values = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
values[_key3] = arguments[_key3];
}
return values.reduce(function (product, val) {
return product * val;
}, 1);
};
/**
* Selector which subtracts all other values from the first
* @param {number} initialValue The value from which all other values will be subtracted
* @param {...number} values The values to be subtracted from the initial value
* @returns {number} The quotient of the initial values divided by all subsequent values
*/
var quotient = function quotient(initialValue) {
for (var _len4 = arguments.length, values = Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) {
values[_key4 - 1] = arguments[_key4];
}
return initialValue / product.apply(undefined, values);
};
/**
* Selector which checks if value is greater than expected value
* @param {number} expectedResult The value with which the result will be compared
* @returns {boolean} True if value is greater than expected, false otherwise
*/
var getIsGreaterThan = function getIsGreaterThan(expectedResult) {
return function (value) {
return value > expectedResult;
};
};
/**
* Selector which checks if value is greater than expected value
* @param {number} expectedResult The value with which the result will be compared
* @returns {boolean} True if value is greater than or equal to expected, false otherwise
*/
var getIsGreaterThanOrEqual = function getIsGreaterThanOrEqual(expectedResult) {
return function (value) {
return value >= expectedResult;
};
};
/**
* Selector which checks if value is less than expected value
* @param {number} expectedResult The value with which the result will be compared
* @returns {boolean} True if value is less than expected, false otherwise
*/
var getIsLessThan = function getIsLessThan(expectedResult) {
return function (value) {
return value < expectedResult;
};
};
/**
* Selector which checks if value is less than expected value
* @param {number} expectedResult The value with which the result will be compared
* @returns {boolean} True if value is less than or equal to expected, false otherwise
*/
var getIsLessThanOrEqual = function getIsLessThanOrEqual(expectedResult) {
return function (value) {
return value <= expectedResult;
};
};
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
var keys = createCommonjsModule(function (module, exports) {
exports = module.exports = typeof Object.keys === 'function'
? Object.keys : shim;
exports.shim = shim;
function shim (obj) {
var keys = [];
for (var key in obj) keys.push(key);
return keys;
}
});
var keys_1 = keys.shim;
var is_arguments = createCommonjsModule(function (module, exports) {
var supportsArgumentsClass = (function(){
return Object.prototype.toString.call(arguments)
})() == '[object Arguments]';
exports = module.exports = supportsArgumentsClass ? supported : unsupported;
exports.supported = supported;
function supported(object) {
return Object.prototype.toString.call(object) == '[object Arguments]';
}
exports.unsupported = unsupported;
function unsupported(object){
return object &&
typeof object == 'object' &&
typeof object.length == 'number' &&
Object.prototype.hasOwnProperty.call(object, 'callee') &&
!Object.prototype.propertyIsEnumerable.call(object, 'callee') ||
false;
}});
var is_arguments_1 = is_arguments.supported;
var is_arguments_2 = is_arguments.unsupported;
var deepEqual_1 = createCommonjsModule(function (module) {
var pSlice = Array.prototype.slice;
var deepEqual = module.exports = function (actual, expected, opts) {
if (!opts) opts = {};
// 7.1. All identical values are equivalent, as determined by ===.
if (actual === expected) {
return true;
} else if (actual instanceof Date && expected instanceof Date) {
return actual.getTime() === expected.getTime();
// 7.3. Other pairs that do not both pass typeof value == 'object',
// equivalence is determined by ==.
} else if (!actual || !expected || typeof actual != 'object' && typeof expected != 'object') {
return opts.strict ? actual === expected : actual == expected;
// 7.4. For all other Object pairs, including Array objects, equivalence is
// determined by having the same number of owned properties (as verified
// with Object.prototype.hasOwnProperty.call), the same set of keys
// (although not necessarily the same order), equivalent values for every
// corresponding key, and an identical 'prototype' property. Note: this
// accounts for both named and indexed properties on Arrays.
} else {
return objEquiv(actual, expected, opts);
}
};
function isUndefinedOrNull(value) {
return value === null || value === undefined;
}
function isBuffer (x) {
if (!x || typeof x !== 'object' || typeof x.length !== 'number') return false;
if (typeof x.copy !== 'function' || typeof x.slice !== 'function') {
return false;
}
if (x.length > 0 && typeof x[0] !== 'number') return false;
return true;
}
function objEquiv(a, b, opts) {
var i, key;
if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
return false;
// an identical 'prototype' property.
if (a.prototype !== b.prototype) return false;
//~~~I've managed to break Object.keys through screwy arguments passing.
// Converting to array solves the problem.
if (is_arguments(a)) {
if (!is_arguments(b)) {
return false;
}
a = pSlice.call(a);
b = pSlice.call(b);
return deepEqual(a, b, opts);
}
if (isBuffer(a)) {
if (!isBuffer(b)) {
return false;
}
if (a.length !== b.length) return false;
for (i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
try {
var ka = keys(a),
kb = keys(b);
} catch (e) {//happens when one is a string literal and the other isn't
return false;
}
// having the same number of owned properties (keys incorporates
// hasOwnProperty)
if (ka.length != kb.length)
return false;
//the same set of keys (although not necessarily the same order),
ka.sort();
kb.sort();
//~~~cheap key test
for (i = ka.length - 1; i >= 0; i--) {
if (ka[i] != kb[i])
return false;
}
//equivalent values for every corresponding key, and
//~~~possibly expensive deep test
for (i = ka.length - 1; i >= 0; i--) {
key = ka[i];
if (!deepEqual(a[key], b[key], opts)) return false;
}
return typeof a === typeof b;
}
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
/**
* Selector which checks if value matches expected value
* @param {any} expectedResult The value with which the result will be compared
* @returns {boolean} True if values match, false otherwise
*/
var getIsEqual = function getIsEqual(expectedResult) {
return function (value) {
return (typeof expectedResult === 'undefined' ? 'undefined' : _typeof(expectedResult)) !== 'object' ? value === expectedResult : deepEqual_1(value, expectedResult, { strict: true });
};
};
/**
* Selector which checks that all values are defined
* @param {...any} values All values being checked for definition
* @returns {boolean} True if all values are defined, false if any value is undefined
*/
var getIsDefined = function getIsDefined() {
for (var _len2 = arguments.length, values = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
values[_key2] = arguments[_key2];
}
return values.findIndex(function (value) {
return value === undefined;
}) === -1;
};
/**
* Selector which returns true if, and only if, all arguments are truthy
* @returns {boolean} True if condition is met, false otherwise
*/
var trueIfAllAreTrue = function trueIfAllAreTrue() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return args.findIndex(function (result) {
return !result;
}) === -1;
};
/**
* Selector which false if any arguments are falsy
* @returns {boolean} True if condition is met, false otherwise
*/
var falseIfAnyAreFalse = trueIfAllAreTrue; // pseudonym
/**
* Selector which returns false if, and only if, all arguments are falsy
* @returns {boolean} True if condition is met, false otherwise
*/
var falseIfAllAreTrue = function falseIfAllAreTrue() {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return args.findIndex(function (result) {
return !result;
}) !== -1;
};
/**
* Selector which returns true if any arguments are falsy
* @returns {boolean} True if condition is met, false otherwise
*/
var trueIfAnyAreFalse = falseIfAllAreTrue; // pseudonym
/**
* Selector which returns true if, and only if, all arguments are falsy
* @returns {boolean} True if condition is met, false otherwise
*/
var trueIfAllAreFalse = function trueIfAllAreFalse() {
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
}
return args.findIndex(function (result) {
return Boolean(result);
}) === -1;
};
/**
* Selector which returns false if any arguments are truthy
* @returns {boolean} True if condition is met, false otherwise
*/
var falseIfAnyAreTrue = trueIfAllAreFalse; // pseudonym
/**
* Selector which returns false if, and only if, all arguments are false
* @returns {boolean} True if condition is met, false otherwise
*/
var falseIfAllAreFalse = function falseIfAllAreFalse() {
for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
args[_key4] = arguments[_key4];
}
return args.findIndex(function (result) {
return Boolean(result);
}) !== -1;
};
/**
* Selector which returns true if any arguments are truthy
* @returns {boolean} True if condition is met, false otherwise
*/
var trueIfAnyAreTrue = falseIfAllAreFalse; // pseudonym
/**
* Returns the (boolean) opposite of the wrapped selectors' result
* @param {Function} selector - selector whose result will be negated
* @returns {boolean} - returns the opposite of the result of the wrapped selector when called with any/all arguments
*/
var not = function not(selector) {
return function () {
return !selector.apply(undefined, arguments);
};
};
/* eslint-enable eqeqeq */
/**
* Selector which joins the values by a given string
* @param {string} [ajoiningString = ' '] The string which will join each value
* @returns {Function} The selector which will join all values using the provided joining string
*/
var joinStrings = function joinStrings() {
var ajoiningString = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ' ';
return function () {
for (var _len = arguments.length, values = Array(_len), _key = 0; _key < _len; _key++) {
values[_key] = arguments[_key];
}
return values.join(ajoiningString);
};
};
/**
* Selector which checks if the result string contains a substring
* @param {string} expectedSubstring The substring to be checked for
* @returns {Function} Selector which returns true if resulting string contains provided substring
*/
var includesSubstring = function includesSubstring(expectedSubstring) {
return function (value) {
return value.includes(expectedSubstring);
};
};
export { sum, difference, product, quotient, getIsGreaterThan, getIsGreaterThanOrEqual, getIsLessThan, getIsLessThanOrEqual, getIsEqual, getIsDefined, trueIfAllAreTrue, falseIfAnyAreFalse, falseIfAllAreTrue, trueIfAnyAreFalse, trueIfAllAreFalse, falseIfAnyAreTrue, falseIfAllAreFalse, trueIfAnyAreTrue, not, joinStrings, includesSubstring };
//# sourceMappingURL=index.es.js.map