conditional
Version:
A preconditions package based on Google's Preconditions library
104 lines (75 loc) • 2.45 kB
JavaScript
(function() {
var isArray, isBoolean, isEmptyString, isEqual, isNotArray, isNotUndefined, isNumeric, isObject, isPrimitive, isString, isUndefined, negate, toString, xor;
negate = function(fn) {
return function(value) {
return !fn(value);
};
};
isObject = function(value) {
return (value != null) && typeof value === 'object';
};
isArray = Array.isArray;
isNotArray = negate(isArray);
isNumeric = function(value) {
return !isArray(value) && (value - parseFloat(value) + 1) >= 0;
};
isBoolean = function(value) {
return typeof value === 'boolean';
};
toString = Object.prototype.toString;
isString = function(value) {
return toString.call(value) === '[object String]';
};
isEmptyString = function(value) {
return isString(value) && !value;
};
isUndefined = function(value) {
return typeof value === 'undefined';
};
isNotUndefined = negate(isUndefined);
isEqual = function(actual, expected) {
var i, key, value, _i, _ref;
switch (false) {
case !isArray(expected):
if (isNotArray(actual) || actual.length !== expected.length) {
return false;
}
for (i = _i = 0, _ref = expected.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
if (!(isEqual(actual[i], expected[i]) && isNotUndefined(actual[i]))) {
return false;
}
}
break;
case !isObject(expected):
for (key in expected) {
value = expected[key];
if (!isEqual(actual[key], value)) {
return false;
}
}
break;
case actual === expected:
return false;
}
return true;
};
isPrimitive = function(value) {
return isNumeric(value) || isBoolean(value);
};
xor = function(operand1, operand2) {
return !operand1 !== !operand2;
};
module.exports.negate = negate;
module.exports.isObject = isObject;
module.exports.isArray = isArray;
module.exports.isNotArray = isNotArray;
module.exports.isNumeric = isNumeric;
module.exports.isString = isString;
module.exports.isEmptyString = isEmptyString;
module.exports.isUndefined = isUndefined;
module.exports.isNotUndefined = isNotUndefined;
module.exports.isEqual = isEqual;
module.exports.isPrimitive = isPrimitive;
module.exports.isNotPrimitive = negate(isPrimitive);
module.exports.xor = xor;
}).call(this);