Beta 7 with some repo names changed.
This commit is contained in:
1 parent
0f7b40cca7
commit
963c19493a
3408 files changed
+462217
-15516
No files matched your search
+3
@@ -0,0 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = function (value) { return value; };
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
var naiveFallback = function () {
|
||||
if (typeof self === "object" && self) return self;
|
||||
if (typeof window === "object" && window) return window;
|
||||
throw new Error("Unable to resolve global `this`");
|
||||
};
|
||||
|
||||
module.exports = (function () {
|
||||
if (this) return this;
|
||||
|
||||
// Unexpected strict mode (may happen if e.g. bundled into ESM module)
|
||||
|
||||
// Thanks @mathiasbynens -> https://mathiasbynens.be/notes/globalthis
|
||||
// In all ES5+ engines global object inherits from Object.prototype
|
||||
// (if you approached one that doesn't please report)
|
||||
try {
|
||||
Object.defineProperty(Object.prototype, "__global__", {
|
||||
get: function () { return this; },
|
||||
configurable: true
|
||||
});
|
||||
} catch (error) {
|
||||
// Unfortunate case of Object.prototype being sealed (via preventExtensions, seal or freeze)
|
||||
return naiveFallback();
|
||||
}
|
||||
try {
|
||||
// Safari case (window.__global__ is resolved with global context, but __global__ does not)
|
||||
if (!__global__) return naiveFallback();
|
||||
return __global__;
|
||||
} finally {
|
||||
delete Object.prototype.__global__;
|
||||
}
|
||||
})();
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = require("./is-implemented")() ? globalThis : require("./implementation");
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = function () {
|
||||
if (typeof globalThis !== "object") return false;
|
||||
if (!globalThis) return false;
|
||||
return globalThis.Array === Array;
|
||||
};
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
// Credit:
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
|
||||
// #Decimal_rounding
|
||||
|
||||
"use strict";
|
||||
|
||||
var isValue = require("type/object/is")
|
||||
, ensureInteger = require("type/integer/ensure");
|
||||
|
||||
var split = String.prototype.split;
|
||||
|
||||
module.exports = function (type) {
|
||||
return function (value/*, exp*/) {
|
||||
value = Number(value);
|
||||
var exp = arguments[1];
|
||||
if (isValue(exp)) exp = ensureInteger(exp);
|
||||
if (!value) return value;
|
||||
if (!exp) return Math[type](value);
|
||||
if (!isFinite(value)) return value;
|
||||
|
||||
// Shift
|
||||
var tokens = split.call(value, "e");
|
||||
value = Math[type](tokens[0] + "e" + ((tokens[1] || 0) - exp));
|
||||
|
||||
// Shift back
|
||||
tokens = value.toString().split("e");
|
||||
return Number(tokens[0] + "e" + (Number(tokens[1] || 0) + exp));
|
||||
};
|
||||
};
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = require("../lib/private/decimal-adjust")("ceil");
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = require("../lib/private/decimal-adjust")("floor");
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = require("../lib/private/decimal-adjust")("round");
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
"use strict";
|
||||
|
||||
var coerceToSafeInteger = require("../safe-integer/coerce");
|
||||
|
||||
module.exports = function (value) {
|
||||
value = coerceToSafeInteger(value);
|
||||
if (!value) return value;
|
||||
if (value < 0) return null;
|
||||
return value;
|
||||
};
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, coerce = require("./coerce");
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
var coerced = coerce(value);
|
||||
if (coerced !== null) return coerced;
|
||||
var options = arguments[1];
|
||||
var errorMessage =
|
||||
options && options.name
|
||||
? "Expected an array length for %n, received %v"
|
||||
: "%v is not an array length";
|
||||
return resolveException(value, errorMessage, options);
|
||||
};
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, is = require("./is");
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
if (is(value, arguments[1])) return value;
|
||||
var options = arguments[1];
|
||||
var errorMessage =
|
||||
options && options.name
|
||||
? "Expected an array like for %n, received %v"
|
||||
: "%v is not an array like";
|
||||
return resolveException(value, errorMessage, options);
|
||||
};
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
"use strict";
|
||||
|
||||
var coerceToArrayLength = require("../array-length/coerce")
|
||||
, isObject = require("../object/is");
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
if (!isObject(value)) {
|
||||
var options = arguments[1];
|
||||
if (isObject(options) && options.allowString && typeof value === "string") return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (typeof value === "function") return false;
|
||||
|
||||
var length;
|
||||
try { length = value.length; }
|
||||
catch (error) { return false; }
|
||||
|
||||
if (coerceToArrayLength(length) === null) return false;
|
||||
return true;
|
||||
};
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, is = require("./is");
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
if (is(value)) return value;
|
||||
var options = arguments[1];
|
||||
var errorMessage =
|
||||
options && options.name ? "Expected an array for %n, received %v" : "%v is not an array";
|
||||
return resolveException(value, errorMessage, options);
|
||||
};
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
"use strict";
|
||||
|
||||
var isPrototype = require("../prototype/is");
|
||||
|
||||
var isArray;
|
||||
if (typeof Array.isArray === "function") {
|
||||
isArray = Array.isArray;
|
||||
} else {
|
||||
var objectToString = Object.prototype.toString, objectTaggedString = objectToString.call([]);
|
||||
isArray = function (value) { return objectToString.call(value) === objectTaggedString; };
|
||||
}
|
||||
|
||||
module.exports = function (value) {
|
||||
if (!isArray(value)) return false;
|
||||
|
||||
// Sanity check (reject objects which do not expose common Array interface)
|
||||
if (!hasOwnProperty.call(value, "length")) return false;
|
||||
try {
|
||||
if (typeof value.length !== "number") return false;
|
||||
if (typeof value.push !== "function") return false;
|
||||
if (typeof value.splice !== "function") return false;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !isPrototype(value);
|
||||
};
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, is = require("./is");
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
if (is(value)) return value;
|
||||
var options = arguments[1];
|
||||
var errorMessage =
|
||||
options && options.name ? "Expected a date for %n, received %v" : "%v is not a date";
|
||||
return resolveException(value, errorMessage, options);
|
||||
};
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
"use strict";
|
||||
|
||||
var isPrototype = require("../prototype/is");
|
||||
|
||||
var dateValueOf = Date.prototype.valueOf;
|
||||
|
||||
module.exports = function (value) {
|
||||
if (!value) return false;
|
||||
|
||||
try {
|
||||
// Sanity check (reject objects which do not expose common Date interface)
|
||||
if (typeof value.getFullYear !== "function") return false;
|
||||
if (typeof value.getTimezoneOffset !== "function") return false;
|
||||
if (typeof value.setFullYear !== "function") return false;
|
||||
|
||||
// Ensure its native Date object (has [[DateValue]] slot)
|
||||
dateValueOf.call(value);
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ensure it hosts valid date
|
||||
if (isNaN(value)) return false;
|
||||
|
||||
return !isPrototype(value);
|
||||
};
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
"use strict";
|
||||
|
||||
var isArray = require("./array/is")
|
||||
, toShortString = require("./lib/to-short-string");
|
||||
|
||||
var objPropertyIsEnumerable = Object.prototype.propertyIsEnumerable;
|
||||
|
||||
var assign = function (target, source) {
|
||||
for (var key in source) {
|
||||
if (objPropertyIsEnumerable.call(source, key)) target[key] = source[key];
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = function (validationDatum1/*, ...validationDatumN, options */) {
|
||||
var validationData = [validationDatum1];
|
||||
var globalOptions;
|
||||
if (arguments.length > 1) {
|
||||
var hasOptions = !isArray(arguments[arguments.length - 1]);
|
||||
if (hasOptions) globalOptions = arguments[arguments.length - 1];
|
||||
var lastDatumIndex = hasOptions ? arguments.length - 2 : arguments.length - 1;
|
||||
for (var i = 1; i <= lastDatumIndex; ++i) validationData.push(arguments[i]);
|
||||
}
|
||||
var result = [], errors;
|
||||
for (var j = 0; j < validationData.length; ++j) {
|
||||
var validationDatum = validationData[j];
|
||||
var options = { name: validationDatum[0] };
|
||||
if (globalOptions) assign(options, globalOptions);
|
||||
if (validationDatum[3]) assign(options, validationDatum[3]);
|
||||
var resultItem;
|
||||
if (typeof validationDatum[2] !== "function") {
|
||||
throw new TypeError(toShortString(validationDatum[2]) + " is not a function");
|
||||
}
|
||||
try {
|
||||
resultItem = validationDatum[2](validationDatum[1], options);
|
||||
} catch (error) {
|
||||
if (!errors) errors = [];
|
||||
errors.push(error);
|
||||
}
|
||||
if (errors) continue;
|
||||
result.push(resultItem);
|
||||
}
|
||||
if (!errors) return result;
|
||||
|
||||
if (errors.length === 1) throw errors[0];
|
||||
var ErrorConstructor = (globalOptions && globalOptions.Error) || TypeError;
|
||||
var errorMessage = "Approached following errors:";
|
||||
for (var k = 0; k < errors.length; ++k) {
|
||||
errorMessage += "\n - " + errors[k].message.split("\n").join("\n ");
|
||||
}
|
||||
throw new ErrorConstructor(errorMessage);
|
||||
};
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, is = require("./is");
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
if (is(value)) return value;
|
||||
var options = arguments[1];
|
||||
var errorMessage =
|
||||
options && options.name ? "Expected an error for %n, received %v" : "%v is not an error";
|
||||
return resolveException(value, errorMessage, options);
|
||||
};
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
"use strict";
|
||||
|
||||
var isPrototype = require("../prototype/is")
|
||||
, isPlainObject = require("../plain-object/is");
|
||||
|
||||
var objectToString = Object.prototype.toString;
|
||||
|
||||
// Recognize host specific errors (e.g. DOMException)
|
||||
var errorTaggedStringRe = /^\[object .*(?:Error|Exception)\]$/
|
||||
, errorNameRe = /^[^\s]*(?:Error|Exception)$/;
|
||||
|
||||
module.exports = function (value) {
|
||||
if (!value) return false;
|
||||
|
||||
var name;
|
||||
// Sanity check (reject objects which do not expose common Error interface)
|
||||
try {
|
||||
name = value.name;
|
||||
if (typeof name !== "string") return false;
|
||||
if (typeof value.message !== "string") return false;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ensure its a native-like Error object
|
||||
// (has [[ErrorData]] slot, or was created to resemble one)
|
||||
// Note: It's not a 100% bulletproof check of confirming that as:
|
||||
// - In ES2015+ string tag can be overriden via Symbol.toStringTag property
|
||||
// - Host errors do not share native error tag. Still we rely on assumption that
|
||||
// tag for each error will end either with `Error` or `Exception` string
|
||||
// - In pre ES2015 era, no custom errors will share the error tag.
|
||||
if (!errorTaggedStringRe.test(objectToString.call(value))) {
|
||||
// Definitely not an ES2015 error instance, but could still be an error
|
||||
// (created via e.g. CustomError.prototype = Object.create(Error.prototype))
|
||||
try {
|
||||
if (name !== value.constructor.name) return false;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
if (!errorNameRe.test(name)) return false;
|
||||
if (isPlainObject(value)) return false;
|
||||
}
|
||||
|
||||
return !isPrototype(value);
|
||||
};
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
var coerceToNumber = require("../number/coerce");
|
||||
|
||||
module.exports = function (value) {
|
||||
value = coerceToNumber(value);
|
||||
return isFinite(value) ? value : null;
|
||||
};
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, coerce = require("./coerce");
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
var coerced = coerce(value);
|
||||
if (coerced !== null) return coerced;
|
||||
var options = arguments[1];
|
||||
var errorMessage =
|
||||
options && options.name
|
||||
? "Expected a finite number for %n, received %v"
|
||||
: "%v is not a finite number";
|
||||
return resolveException(value, errorMessage, options);
|
||||
};
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, is = require("./is");
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
if (is(value)) return value;
|
||||
var options = arguments[1];
|
||||
var errorMessage =
|
||||
options && options.name
|
||||
? "Expected a function for %n, received %v"
|
||||
: "%v is not a function";
|
||||
return resolveException(value, errorMessage, options);
|
||||
};
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
"use strict";
|
||||
|
||||
var isPrototype = require("../prototype/is");
|
||||
|
||||
module.exports = function (value) {
|
||||
if (typeof value !== "function") return false;
|
||||
|
||||
if (!hasOwnProperty.call(value, "length")) return false;
|
||||
|
||||
try {
|
||||
if (typeof value.length !== "number") return false;
|
||||
if (typeof value.call !== "function") return false;
|
||||
if (typeof value.apply !== "function") return false;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !isPrototype(value);
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
var coerceToFinite = require("../finite/coerce");
|
||||
|
||||
var abs = Math.abs, floor = Math.floor;
|
||||
|
||||
module.exports = function (value) {
|
||||
value = coerceToFinite(value);
|
||||
if (!value) return value;
|
||||
return (value > 0 ? 1 : -1) * floor(abs(value));
|
||||
};
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, coerce = require("./coerce");
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
var coerced = coerce(value);
|
||||
if (coerced !== null) return coerced;
|
||||
var options = arguments[1];
|
||||
var errorMessage =
|
||||
options && options.name
|
||||
? "Expected an integer for %n, received %v"
|
||||
: "%v is not an integer";
|
||||
return resolveException(value, errorMessage, options);
|
||||
};
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, resolveErrorMessage = require("../lib/resolve-error-message")
|
||||
, toShortString = require("../lib/to-short-string")
|
||||
, ensurePlainFunction = require("../plain-function/ensure")
|
||||
, is = require("./is");
|
||||
|
||||
var invalidItemsLimit = 3;
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
var options = arguments[1];
|
||||
var mainErrorMessage =
|
||||
options && options.name
|
||||
? "Expected an iterable for %n, received %v"
|
||||
: "%v is not expected iterable";
|
||||
if (!is(value, options)) return resolveException(value, mainErrorMessage, options);
|
||||
if (!options) return value;
|
||||
|
||||
var ensureItem = ensurePlainFunction(options.ensureItem, { isOptional: true });
|
||||
if (ensureItem) {
|
||||
var coercedValue = [];
|
||||
var iterator = value[Symbol.iterator]();
|
||||
var item, invalidItems;
|
||||
while (!(item = iterator.next()).done) {
|
||||
var newItemValue;
|
||||
try {
|
||||
newItemValue = ensureItem(item.value);
|
||||
} catch (error) {
|
||||
if (!invalidItems) invalidItems = [];
|
||||
if (invalidItems.push(item.value) === invalidItemsLimit) break;
|
||||
}
|
||||
if (invalidItems) continue;
|
||||
coercedValue.push(newItemValue);
|
||||
}
|
||||
if (invalidItems) {
|
||||
var errorMessage =
|
||||
resolveErrorMessage(mainErrorMessage, value, options) +
|
||||
".\n Following items are invalid:";
|
||||
for (var i = 0; i < invalidItems.length; ++i) {
|
||||
errorMessage += "\n - " + toShortString(invalidItems[i]);
|
||||
}
|
||||
throw new TypeError(errorMessage);
|
||||
}
|
||||
return coercedValue;
|
||||
}
|
||||
|
||||
return value;
|
||||
};
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
// Polyfills friendly, therefore ES5 syntax
|
||||
|
||||
"use strict";
|
||||
|
||||
var isObject = require("../object/is");
|
||||
|
||||
var iteratorSymbol = Symbol.iterator;
|
||||
|
||||
if (!iteratorSymbol) {
|
||||
throw new Error("Cannot initialize iterator/is due to Symbol.iterator not being implemented");
|
||||
}
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
var options = arguments[1];
|
||||
if (!isObject(value)) {
|
||||
if (!isObject(options) || !options.allowString || typeof value !== "string") return false;
|
||||
}
|
||||
try {
|
||||
if (typeof value[iteratorSymbol] !== "function") return false;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
if (!options) return true;
|
||||
if (options.denyEmpty) {
|
||||
try {
|
||||
if (value[iteratorSymbol]().next().done) return false;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../resolve-exception");
|
||||
|
||||
module.exports = function (value, coerced, options) {
|
||||
if (coerced >= options.min) return coerced;
|
||||
var errorMessage =
|
||||
options && options.name
|
||||
? "Expected %n to be greater or equal " + options.min + ", received %v"
|
||||
: "%v is not greater or equal " + options.min;
|
||||
return resolveException(value, errorMessage, options);
|
||||
};
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol";
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
"use strict";
|
||||
|
||||
var stringCoerce = require("../string/coerce")
|
||||
, toShortString = require("./to-short-string");
|
||||
|
||||
module.exports = function (errorMessage, value, inputOptions) {
|
||||
if (inputOptions && inputOptions.errorMessage) {
|
||||
errorMessage = stringCoerce(inputOptions.errorMessage);
|
||||
}
|
||||
|
||||
var valueInsertIndex = errorMessage.indexOf("%v");
|
||||
var valueToken = valueInsertIndex > -1 ? toShortString(value) : null;
|
||||
if (inputOptions && inputOptions.name) {
|
||||
var nameInsertIndex = errorMessage.indexOf("%n");
|
||||
if (nameInsertIndex > -1) {
|
||||
if (valueInsertIndex > -1) {
|
||||
var firstToken, secondToken, firstInsertIndex, secondInsertIndex;
|
||||
if (nameInsertIndex > valueInsertIndex) {
|
||||
firstToken = valueToken;
|
||||
firstInsertIndex = valueInsertIndex;
|
||||
secondToken = inputOptions.name;
|
||||
secondInsertIndex = nameInsertIndex;
|
||||
} else {
|
||||
firstToken = inputOptions.name;
|
||||
firstInsertIndex = nameInsertIndex;
|
||||
secondToken = valueToken;
|
||||
secondInsertIndex = valueInsertIndex;
|
||||
}
|
||||
return (
|
||||
errorMessage.slice(0, firstInsertIndex) +
|
||||
firstToken +
|
||||
errorMessage.slice(firstInsertIndex + 2, secondInsertIndex) +
|
||||
secondToken +
|
||||
errorMessage.slice(secondInsertIndex + 2)
|
||||
);
|
||||
}
|
||||
return (
|
||||
errorMessage.slice(0, nameInsertIndex) +
|
||||
inputOptions.name +
|
||||
errorMessage.slice(nameInsertIndex + 2)
|
||||
);
|
||||
}
|
||||
}
|
||||
if (valueInsertIndex > -1) {
|
||||
return (
|
||||
errorMessage.slice(0, valueInsertIndex) +
|
||||
valueToken +
|
||||
errorMessage.slice(valueInsertIndex + 2)
|
||||
);
|
||||
}
|
||||
return errorMessage;
|
||||
};
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
var isValue = require("../value/is")
|
||||
, resolveErrorMessage = require("./resolve-error-message");
|
||||
|
||||
module.exports = function (value, defaultMessage, inputOptions) {
|
||||
if (inputOptions && !isValue(value)) {
|
||||
if ("default" in inputOptions) return inputOptions["default"];
|
||||
if (inputOptions.isOptional) return null;
|
||||
}
|
||||
var ErrorConstructor = (inputOptions && inputOptions.Error) || TypeError;
|
||||
throw new ErrorConstructor(resolveErrorMessage(defaultMessage, value, inputOptions));
|
||||
};
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = function (value) {
|
||||
try {
|
||||
return value.toString();
|
||||
} catch (error) {
|
||||
try { return String(value); }
|
||||
catch (error2) { return null; }
|
||||
}
|
||||
};
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
"use strict";
|
||||
|
||||
var safeToString = require("./safe-to-string");
|
||||
|
||||
var reNewLine = /[\n\r\u2028\u2029]/g;
|
||||
|
||||
module.exports = function (value) {
|
||||
var string = safeToString(value);
|
||||
if (string === null) return "<Non-coercible to string value>";
|
||||
// Trim if too long
|
||||
if (string.length > 100) string = string.slice(0, 99) + "…";
|
||||
// Replace eventual new lines
|
||||
string = string.replace(reNewLine, function (char) {
|
||||
switch (char) {
|
||||
case "\n":
|
||||
return "\\n";
|
||||
case "\r":
|
||||
return "\\r";
|
||||
case "\u2028":
|
||||
return "\\u2028";
|
||||
case "\u2029":
|
||||
return "\\u2029";
|
||||
/* istanbul ignore next */
|
||||
default:
|
||||
throw new Error("Unexpected character");
|
||||
}
|
||||
});
|
||||
return string;
|
||||
};
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
"use strict";
|
||||
|
||||
var coerceToInteger = require("../integer/coerce");
|
||||
|
||||
module.exports = function (value) {
|
||||
value = coerceToInteger(value);
|
||||
if (!value) return value;
|
||||
if (value < 0) return null;
|
||||
return value;
|
||||
};
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, ensureMin = require("../lib/ensure/min")
|
||||
, coerce = require("./coerce");
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
var coerced = coerce(value), options = arguments[1];
|
||||
if (coerced !== null) {
|
||||
if (options) {
|
||||
if (options.min) ensureMin(value, coerced, options);
|
||||
}
|
||||
return coerced;
|
||||
}
|
||||
|
||||
var errorMessage =
|
||||
options && options.name
|
||||
? "Expected a natural number for %n, received %v"
|
||||
: "%v is not a natural number";
|
||||
return resolveException(value, errorMessage, options);
|
||||
};
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
var isValue = require("../value/is");
|
||||
|
||||
module.exports = function (value) {
|
||||
if (!isValue(value)) return null;
|
||||
try {
|
||||
value = +value; // Ensure implicit coercion
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
if (isNaN(value)) return null;
|
||||
return value;
|
||||
};
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, coerce = require("./coerce");
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
var coerced = coerce(value);
|
||||
if (coerced !== null) return coerced;
|
||||
var options = arguments[1];
|
||||
var errorMessage =
|
||||
options && options.name ? "Expected a number for %n, received %v" : "%v is not a number";
|
||||
return resolveException(value, errorMessage, options);
|
||||
};
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, is = require("./is");
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
if (is(value)) return value;
|
||||
var options = arguments[1];
|
||||
var errorMessage =
|
||||
options && options.name ? "Expected an object for %n, received %v" : "%v is not an object";
|
||||
return resolveException(value, errorMessage, options);
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
var isValue = require("../value/is");
|
||||
|
||||
// prettier-ignore
|
||||
var possibleTypes = { "object": true, "function": true, "undefined": true /* document.all */ };
|
||||
|
||||
module.exports = function (value) {
|
||||
if (!isValue(value)) return false;
|
||||
return hasOwnProperty.call(possibleTypes, typeof value);
|
||||
};
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
{
|
||||
"name": "type",
|
||||
"version": "2.1.0",
|
||||
"description": "Runtime validation and processing of JavaScript types",
|
||||
"author": "Mariusz Nowak <medyk@medikoo.com> (https://www.medikoo.com/)",
|
||||
"keywords": [
|
||||
"type",
|
||||
"coercion"
|
||||
],
|
||||
"repository": "medikoo/type",
|
||||
"devDependencies": {
|
||||
"chai": "^4.2.0",
|
||||
"eslint": "^7.7.0",
|
||||
"eslint-config-medikoo": "^3.1.0",
|
||||
"git-list-updated": "^1.2.1",
|
||||
"husky": "^4.2.5",
|
||||
"lint-staged": "^10.2.11",
|
||||
"mocha": "^6.2.3",
|
||||
"nyc": "^15.1.0",
|
||||
"prettier-elastic": "^1.19.1"
|
||||
},
|
||||
"husky": {
|
||||
"hooks": {
|
||||
"pre-commit": "lint-staged"
|
||||
}
|
||||
},
|
||||
"lint-staged": {
|
||||
"*.js": [
|
||||
"eslint"
|
||||
],
|
||||
"*.{css,html,js,json,md,yaml,yml}": [
|
||||
"prettier -c"
|
||||
]
|
||||
},
|
||||
"eslintConfig": {
|
||||
"extends": "medikoo/es3",
|
||||
"root": true,
|
||||
"globals": {
|
||||
"Map": true,
|
||||
"Promise": true,
|
||||
"Set": true,
|
||||
"Symbol": true
|
||||
},
|
||||
"overrides": [
|
||||
{
|
||||
"files": "test/**/*.js",
|
||||
"env": {
|
||||
"mocha": true
|
||||
},
|
||||
"rules": {
|
||||
"no-eval": "off",
|
||||
"no-new-wrappers": "off"
|
||||
}
|
||||
},
|
||||
{
|
||||
"files": [
|
||||
"string/coerce.js",
|
||||
"number/coerce.js"
|
||||
],
|
||||
"rules": {
|
||||
"no-implicit-coercion": "off"
|
||||
}
|
||||
},
|
||||
{
|
||||
"files": "plain-object/is.js",
|
||||
"rules": {
|
||||
"no-proto": "off"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"prettier": {
|
||||
"printWidth": 100,
|
||||
"tabWidth": 4,
|
||||
"overrides": [
|
||||
{
|
||||
"files": [
|
||||
"*.md"
|
||||
],
|
||||
"options": {
|
||||
"tabWidth": 2
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"scripts": {
|
||||
"coverage": "nyc --reporter=lcov --reporter=html --reporter=text-summary npm test",
|
||||
"check-coverage": "npm run coverage && nyc check-coverage --statements 80 --function 80 --branches 80 --lines 80",
|
||||
"lint": "eslint --ignore-path=.gitignore .",
|
||||
"lint-updated": "pipe-git-updated --ext=js -- eslint --ignore-pattern '!*'",
|
||||
"prettier-check-updated": "pipe-git-updated --ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier -c",
|
||||
"prettify": "prettier --write --ignore-path .gitignore '**/*.{css,html,js,json,md,yaml,yml}'",
|
||||
"test": "mocha --recursive"
|
||||
},
|
||||
"license": "ISC"
|
||||
|
||||
,"_resolved": "https://registry.npmjs.org/type/-/type-2.1.0.tgz"
|
||||
,"_integrity": "sha512-G9absDWvhAWCV2gmF1zKud3OyC61nZDwWvBL2DApaVFogI07CprggiQAOOjvp2NRjYWFzPyu7vwtDrQFq8jeSA=="
|
||||
,"_from": "type@^2.0.0"
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, is = require("./is");
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
if (is(value)) return value;
|
||||
var options = arguments[1];
|
||||
var errorMessage =
|
||||
options && options.name
|
||||
? "Expected a plain function for %n, received %v"
|
||||
: "%v is not a plain function";
|
||||
return resolveException(value, errorMessage, options);
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
var isFunction = require("../function/is");
|
||||
|
||||
var classRe = /^\s*class[\s{/}]/, functionToString = Function.prototype.toString;
|
||||
|
||||
module.exports = function (value) {
|
||||
if (!isFunction(value)) return false;
|
||||
if (classRe.test(functionToString.call(value))) return false;
|
||||
return true;
|
||||
};
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, resolveErrorMessage = require("../lib/resolve-error-message")
|
||||
, ensurePlainFunction = require("../plain-function/ensure")
|
||||
, ensureArray = require("../array/ensure")
|
||||
, is = require("./is");
|
||||
|
||||
var objHasOwnProperty = Object.prototype.hasOwnProperty, invalidItemsLimit = 3;
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
var options = arguments[1];
|
||||
var mainErrorMessage =
|
||||
options && options.name
|
||||
? "Expected a plain object for %n, received %v"
|
||||
: "%v is not a plain object";
|
||||
if (!is(value)) return resolveException(value, mainErrorMessage, options);
|
||||
if (!options) return value;
|
||||
|
||||
var invalidKeys, key, errorMessage;
|
||||
var allowedKeys = ensureArray(options.allowedKeys, { isOptional: true });
|
||||
if (allowedKeys) {
|
||||
for (key in value) {
|
||||
if (!objHasOwnProperty.call(value, key)) continue;
|
||||
if (allowedKeys.indexOf(key) > -1) continue;
|
||||
if (!invalidKeys) invalidKeys = [];
|
||||
if (invalidKeys.push(key) === invalidItemsLimit) break;
|
||||
}
|
||||
if (invalidKeys) {
|
||||
errorMessage =
|
||||
resolveErrorMessage(mainErrorMessage, value, options) +
|
||||
".\n Following keys are unexpected: " +
|
||||
invalidKeys.join(", ");
|
||||
throw new TypeError(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
var ensurePropertyValue = ensurePlainFunction(options.ensurePropertyValue, {
|
||||
isOptional: true
|
||||
});
|
||||
if (ensurePropertyValue) {
|
||||
var coercedValue = {};
|
||||
for (key in value) {
|
||||
if (!objHasOwnProperty.call(value, key)) continue;
|
||||
var coercedPropertyValue;
|
||||
try {
|
||||
coercedPropertyValue = ensurePropertyValue(value[key]);
|
||||
} catch (error) {
|
||||
if (!invalidKeys) invalidKeys = [];
|
||||
if (invalidKeys.push(key) === invalidItemsLimit) break;
|
||||
}
|
||||
if (invalidKeys) continue;
|
||||
coercedValue[key] = coercedPropertyValue;
|
||||
}
|
||||
if (invalidKeys) {
|
||||
errorMessage =
|
||||
resolveErrorMessage(mainErrorMessage, value, options) +
|
||||
".\n Values for following keys are invalid: " +
|
||||
invalidKeys.join(", ");
|
||||
throw new TypeError(errorMessage);
|
||||
}
|
||||
|
||||
return coercedValue;
|
||||
}
|
||||
|
||||
return value;
|
||||
};
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
"use strict";
|
||||
|
||||
var isObject = require("../object/is")
|
||||
, isPrototype = require("../prototype/is");
|
||||
|
||||
var getPrototypeOf;
|
||||
if (typeof Object.getPrototypeOf === "function") {
|
||||
getPrototypeOf = Object.getPrototypeOf;
|
||||
} else if ({}.__proto__ === Object.prototype) {
|
||||
getPrototypeOf = function (object) { return object.__proto__; };
|
||||
}
|
||||
|
||||
module.exports = function (value) {
|
||||
if (!isObject(value)) return false;
|
||||
var prototype;
|
||||
if (getPrototypeOf) {
|
||||
prototype = getPrototypeOf(value);
|
||||
} else {
|
||||
try {
|
||||
var valueConstructor = value.constructor;
|
||||
if (valueConstructor) prototype = valueConstructor.prototype;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (prototype && !hasOwnProperty.call(prototype, "propertyIsEnumerable")) return false;
|
||||
return !isPrototype(value);
|
||||
};
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, is = require("./is");
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
if (is(value)) return value;
|
||||
var options = arguments[1];
|
||||
var errorMessage =
|
||||
options && options.name ? "Expected a promise for %n, received %v" : "%v is not a promise";
|
||||
return resolveException(value, errorMessage, options);
|
||||
};
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
"use strict";
|
||||
|
||||
var isPrototype = require("../prototype/is");
|
||||
|
||||
// In theory we could rely on Symbol.toStringTag directly,
|
||||
// still early native implementation (e.g. in FF) predated symbols
|
||||
var objectToString = Object.prototype.toString
|
||||
, objectTaggedString = objectToString.call(Promise.resolve());
|
||||
|
||||
module.exports = function (value) {
|
||||
if (!value) return false;
|
||||
|
||||
// Sanity check (reject objects which do not expose common Promise interface)
|
||||
try {
|
||||
if (typeof value.then !== "function") return false;
|
||||
if (typeof value["catch"] !== "function") return false;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ensure its native Promise object (has [[PromiseState]] slot)
|
||||
// Note: it's not 100% precise as string tag may be overriden
|
||||
// and other objects could be hacked to expose it
|
||||
if (objectToString.call(value) !== objectTaggedString) return false;
|
||||
|
||||
return !isPrototype(value);
|
||||
};
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
var isObject = require("../object/is");
|
||||
|
||||
module.exports = function (value) {
|
||||
if (!isObject(value)) return false;
|
||||
try {
|
||||
if (!value.constructor) return false;
|
||||
return value.constructor.prototype === value;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, is = require("./is");
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
if (is(value)) return value;
|
||||
var options = arguments[1];
|
||||
var errorMessage =
|
||||
options && options.name
|
||||
? "Expected a regular expression for %n, received %v"
|
||||
: "%v is not a regular expression";
|
||||
return resolveException(value, errorMessage, options);
|
||||
};
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
"use strict";
|
||||
|
||||
var isToStringTagSupported = require("../lib/is-to-string-tag-supported")
|
||||
, isPrototype = require("../prototype/is");
|
||||
|
||||
var regExpTest = RegExp.prototype.test
|
||||
, objectToString = Object.prototype.toString
|
||||
, objectTaggedString = objectToString.call(/a/);
|
||||
|
||||
module.exports = function (value) {
|
||||
if (!value) return false;
|
||||
|
||||
// Sanity check (reject objects which do not expose common RegExp interface)
|
||||
if (!hasOwnProperty.call(value, "lastIndex")) return false;
|
||||
try {
|
||||
if (typeof value.lastIndex !== "number") return false;
|
||||
if (typeof value.test !== "function") return false;
|
||||
if (typeof value.exec !== "function") return false;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ensure its native RegExp object (has [[RegExpMatcher]] slot)
|
||||
if (isToStringTagSupported && typeof value[Symbol.toStringTag] === "string") {
|
||||
// Edge case (possibly a regExp with custom Symbol.toStringTag)
|
||||
try {
|
||||
var lastIndex = value.lastIndex;
|
||||
regExpTest.call(value, "");
|
||||
if (value.lastIndex !== lastIndex) value.lastIndex = lastIndex;
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (objectToString.call(value) !== objectTaggedString) return false;
|
||||
return !isPrototype(value);
|
||||
};
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
var coerceToInteger = require("../integer/coerce");
|
||||
|
||||
var MAX_SAFE_INTEGER = 9007199254740991, MIN_SAFE_INTEGER = -9007199254740991;
|
||||
|
||||
module.exports = function (value) {
|
||||
value = coerceToInteger(value);
|
||||
if (!value) return value;
|
||||
if (value > MAX_SAFE_INTEGER) return null;
|
||||
if (value < MIN_SAFE_INTEGER) return null;
|
||||
return value;
|
||||
};
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, coerce = require("./coerce");
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
var coerced = coerce(value);
|
||||
if (coerced !== null) return coerced;
|
||||
var options = arguments[1];
|
||||
var errorMessage =
|
||||
options && options.name
|
||||
? "Expected a safe integer for %n, received %v"
|
||||
: "%v is not a safe integer";
|
||||
return resolveException(value, errorMessage, options);
|
||||
};
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
"use strict";
|
||||
|
||||
var isValue = require("../value/is")
|
||||
, isObject = require("../object/is");
|
||||
|
||||
var objectToString = Object.prototype.toString;
|
||||
|
||||
module.exports = function (value) {
|
||||
if (!isValue(value)) return null;
|
||||
if (isObject(value)) {
|
||||
// Reject Object.prototype.toString coercion
|
||||
var valueToString = value.toString;
|
||||
if (typeof valueToString !== "function") return null;
|
||||
if (valueToString === objectToString) return null;
|
||||
// Note: It can be object coming from other realm, still as there's no ES3 and CSP compliant
|
||||
// way to resolve its realm's Object.prototype.toString it's left as not addressed edge case
|
||||
}
|
||||
try {
|
||||
return "" + value; // Ensure implicit coercion
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
var resolveException = require("../lib/resolve-exception")
|
||||
, coerce = require("./coerce");
|
||||
|
||||
module.exports = function (value/*, options*/) {
|
||||
var coerced = coerce(value);
|
||||
if (coerced !== null) return coerced;
|
||||
var options = arguments[1];
|
||||
var errorMessage =
|
||||
options && options.name ? "Expected a string for %n, received %v" : "%v is not a string";
|
||||
return resolveException(value, errorMessage, options);
|
||||
};
|
||||
Generated
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
"use strict";
|
||||
|
||||
try { module.exports = eval("(() => {})"); }
|
||||
catch (error) {}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
"use strict";
|
||||
|
||||
try { module.exports = eval("(class {})"); }
|
||||
catch (error) {}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, coerceToArrayLength = require("../../array-length/coerce");
|
||||
|
||||
describe("array-length/coerce", function () {
|
||||
it("Should coerce float", function () {
|
||||
assert.equal(coerceToArrayLength(123.123), 123);
|
||||
assert.equal(coerceToArrayLength(123.823), 123);
|
||||
});
|
||||
it("Should coerce string", function () { assert.equal(coerceToArrayLength("12.123"), 12); });
|
||||
it("Should coerce booleans", function () { assert.equal(coerceToArrayLength(true), 1); });
|
||||
it("Should coerce number objects", function () {
|
||||
assert.equal(coerceToArrayLength(new Number(343)), 343);
|
||||
});
|
||||
it("Should coerce objects", function () {
|
||||
assert.equal(coerceToArrayLength({ valueOf: function () { return 23; } }), 23);
|
||||
});
|
||||
it("Should reject infinite number", function () {
|
||||
assert.equal(coerceToArrayLength(Infinity), null);
|
||||
});
|
||||
it("Should reject number beyond Number.MAX_SAFE_INTEGER", function () {
|
||||
assert.equal(coerceToArrayLength(9007199254740992), null);
|
||||
});
|
||||
it("Should reject negative number", function () {
|
||||
assert.equal(coerceToArrayLength(-9), null);
|
||||
});
|
||||
|
||||
it("Should reject NaN", function () { assert.equal(coerceToArrayLength(NaN), null); });
|
||||
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should not coerce objects with no number representation", function () {
|
||||
assert.equal(coerceToArrayLength(Object.create(null)), null);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should not coerce null", function () { assert.equal(coerceToArrayLength(null), null); });
|
||||
it("Should not coerce undefined", function () {
|
||||
assert.equal(coerceToArrayLength(undefined), null);
|
||||
});
|
||||
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should not coerce symbols", function () {
|
||||
assert.equal(coerceToArrayLength(Symbol("foo")), null);
|
||||
});
|
||||
}
|
||||
});
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensureArrayLength = require("../../array-length/ensure");
|
||||
|
||||
describe("array-length/ensure", function () {
|
||||
it("Should return coerced value", function () {
|
||||
assert.equal(ensureArrayLength("12.23"), 12);
|
||||
});
|
||||
it("Should crash on no value", function () {
|
||||
try {
|
||||
ensureArrayLength(-20);
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "-20 is not an array length");
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensureArrayLength(-20, { name: "foo" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected an array length for foo, received -20");
|
||||
}
|
||||
});
|
||||
});
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensureArrayLike = require("../../array-like/ensure");
|
||||
|
||||
describe("array-like/ensure", function () {
|
||||
it("Should return input value", function () {
|
||||
var value = [];
|
||||
assert.equal(ensureArrayLike(value), value);
|
||||
});
|
||||
it("Should allow strings with allowString option", function () {
|
||||
var value = "foo";
|
||||
assert.equal(ensureArrayLike(value, { allowString: true }), value);
|
||||
});
|
||||
it("Should crash on invalid value", function () {
|
||||
try {
|
||||
ensureArrayLike("foo");
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert(error.message.includes("is not an array like"));
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensureArrayLike("foo", { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected an array like for name, received foo");
|
||||
}
|
||||
});
|
||||
});
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, isArrayLike = require("../../array-like/is");
|
||||
|
||||
describe("array-like/is", function () {
|
||||
it("Should return true on array", function () { assert.equal(isArrayLike([]), true); });
|
||||
it("Should return true on array-like object", function () {
|
||||
assert.equal(isArrayLike({ length: 1 }), true);
|
||||
});
|
||||
it("Should by default return false on string", function () {
|
||||
assert.equal(isArrayLike("foo"), false);
|
||||
});
|
||||
it("Should accept strings if specified", function () {
|
||||
assert.equal(isArrayLike("foo", { allowString: true }), true);
|
||||
});
|
||||
|
||||
it("Should return false on objects with negative length", function () {
|
||||
assert.equal(isArrayLike({ length: -1 }), false);
|
||||
});
|
||||
|
||||
it("Should return false on plain object", function () {
|
||||
assert.equal(isArrayLike({}), false);
|
||||
});
|
||||
it("Should return false on function", function () {
|
||||
assert.equal(isArrayLike(function () { return true; }), false);
|
||||
});
|
||||
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should return false on object with no prototype", function () {
|
||||
assert.equal(isArrayLike(Object.create(null)), false);
|
||||
});
|
||||
}
|
||||
it("Should return false on number", function () { assert.equal(isArrayLike(123), false); });
|
||||
it("Should return false on NaN", function () { assert.equal(isArrayLike(NaN), false); });
|
||||
it("Should return false on boolean", function () { assert.equal(isArrayLike(true), false); });
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should return false on symbol", function () {
|
||||
assert.equal(isArrayLike(Symbol("foo")), false);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should return false on null", function () { assert.equal(isArrayLike(null), false); });
|
||||
it("Should return false on undefined", function () {
|
||||
assert.equal(isArrayLike(void 0), false);
|
||||
});
|
||||
});
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensureArray = require("../../array/ensure");
|
||||
|
||||
describe("array/ensure", function () {
|
||||
it("Should return input value", function () {
|
||||
var value = [];
|
||||
assert.equal(ensureArray(value), value);
|
||||
});
|
||||
it("Should crash on invalid value", function () {
|
||||
try {
|
||||
ensureArray(null);
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "null is not an array");
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensureArray(null, { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected an array for name, received null");
|
||||
}
|
||||
});
|
||||
});
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, isArray = require("../../array/is");
|
||||
|
||||
describe("array/is", function () {
|
||||
it("Should return true on array", function () { assert.equal(isArray([]), true); });
|
||||
|
||||
it("Should return false on array with no common API exposed", function () {
|
||||
var value = [];
|
||||
value.push = null;
|
||||
assert.equal(isArray(value), false);
|
||||
});
|
||||
it("Should return false on Array.prototype", function () {
|
||||
assert.equal(isArray(Array.prototype), false);
|
||||
});
|
||||
|
||||
it("Should return false on plain object", function () { assert.equal(isArray({}), false); });
|
||||
it("Should return false on function", function () {
|
||||
assert.equal(isArray(function () { return true; }), false);
|
||||
});
|
||||
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should return false on object with no prototype", function () {
|
||||
assert.equal(isArray(Object.create(null)), false);
|
||||
});
|
||||
}
|
||||
it("Should return false on string", function () { assert.equal(isArray("foo"), false); });
|
||||
it("Should return false on empty string", function () { assert.equal(isArray(""), false); });
|
||||
it("Should return false on number", function () { assert.equal(isArray(123), false); });
|
||||
it("Should return false on NaN", function () { assert.equal(isArray(NaN), false); });
|
||||
it("Should return false on boolean", function () { assert.equal(isArray(true), false); });
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should return false on symbol", function () {
|
||||
assert.equal(isArray(Symbol("foo")), false);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should return false on null", function () { assert.equal(isArray(null), false); });
|
||||
it("Should return false on undefined", function () { assert.equal(isArray(void 0), false); });
|
||||
});
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensureDate = require("../../date/ensure");
|
||||
|
||||
describe("date/ensure", function () {
|
||||
it("Should return input value", function () {
|
||||
var value = new Date();
|
||||
assert.equal(ensureDate(value), value);
|
||||
});
|
||||
it("Should crash on invalid value", function () {
|
||||
try {
|
||||
ensureDate(null);
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert(error.message.includes("is not a date"));
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensureDate(null, { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected a date for name, received null");
|
||||
}
|
||||
});
|
||||
});
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, isDate = require("../../date/is");
|
||||
|
||||
describe("date/is", function () {
|
||||
it("Should return true on date", function () { assert.equal(isDate(new Date()), true); });
|
||||
it("Should return false on invalid date", function () {
|
||||
assert.equal(isDate(new Date("foo")), false);
|
||||
});
|
||||
|
||||
it("Should return false on native date with no common API exposed", function () {
|
||||
var value = new Date();
|
||||
value.getFullYear = null;
|
||||
assert.equal(isDate(value), false);
|
||||
});
|
||||
it("Should return false on Date.prototype", function () {
|
||||
assert.equal(isDate(Date.prototype), false);
|
||||
});
|
||||
it("Should return false on time value", function () { assert.equal(isDate(12312313), false); });
|
||||
|
||||
it("Should return false on plain object", function () { assert.equal(isDate({}), false); });
|
||||
it("Should return false on function", function () {
|
||||
assert.equal(isDate(function () { return true; }), false);
|
||||
});
|
||||
|
||||
it("Should return false on array", function () { assert.equal(isDate([]), false); });
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should return false on object with no prototype", function () {
|
||||
assert.equal(isDate(Object.create(null)), false);
|
||||
});
|
||||
}
|
||||
it("Should return false on string", function () { assert.equal(isDate("foo"), false); });
|
||||
it("Should return false on empty string", function () { assert.equal(isDate(""), false); });
|
||||
it("Should return false on number", function () { assert.equal(isDate(123), false); });
|
||||
it("Should return false on NaN", function () { assert.equal(isDate(NaN), false); });
|
||||
it("Should return false on boolean", function () { assert.equal(isDate(true), false); });
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should return false on symbol", function () {
|
||||
assert.equal(isDate(Symbol("foo")), false);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should return false on null", function () { assert.equal(isDate(null), false); });
|
||||
it("Should return false on undefined", function () { assert.equal(isDate(void 0), false); });
|
||||
});
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensure = require("../ensure")
|
||||
, ensureNumber = require("../number/ensure");
|
||||
|
||||
describe("ensure", function () {
|
||||
it("Should support multiple validation datums", function () {
|
||||
assert.deepEqual(ensure(["foo", 12.323, ensureNumber], ["bar", 10, ensureNumber]), [
|
||||
12.323, 10
|
||||
]);
|
||||
});
|
||||
it("Should surface only error", function () {
|
||||
try {
|
||||
ensure(["foo", null, ensureNumber], ["bar", 10, ensureNumber]);
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected a number for foo, received null");
|
||||
}
|
||||
});
|
||||
it("Should surface only error", function () {
|
||||
try {
|
||||
ensure(["foo", null, ensureNumber], ["bar", 10, ensureNumber]);
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected a number for foo, received null");
|
||||
}
|
||||
});
|
||||
it("Should cumulate errors", function () {
|
||||
try {
|
||||
ensure(["foo", null, ensureNumber], ["bar", NaN, ensureNumber]);
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(
|
||||
error.message,
|
||||
"Approached following errors:" +
|
||||
"\n - Expected a number for foo, received null" +
|
||||
"\n - Expected a number for bar, received NaN"
|
||||
);
|
||||
}
|
||||
});
|
||||
it("Should support Error from global options", function () {
|
||||
try {
|
||||
ensure(["foo", null, ensureNumber], ["bar", NaN, ensureNumber], { Error: RangeError });
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "RangeError");
|
||||
assert.equal(
|
||||
error.message,
|
||||
"Approached following errors:" +
|
||||
"\n - Expected a number for foo, received null" +
|
||||
"\n - Expected a number for bar, received NaN"
|
||||
);
|
||||
}
|
||||
try {
|
||||
ensure(["foo", null, ensureNumber], ["bar", 10, ensureNumber], { Error: RangeError });
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "RangeError");
|
||||
assert.equal(error.message, "Expected a number for foo, received null");
|
||||
}
|
||||
});
|
||||
it("Should support individual validation options", function () {
|
||||
try {
|
||||
ensure(["foo", null, ensureNumber, { Error: RangeError }], ["bar", 10, ensureNumber]);
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "RangeError");
|
||||
assert.equal(error.message, "Expected a number for foo, received null");
|
||||
}
|
||||
});
|
||||
});
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensureError = require("../../error/ensure");
|
||||
|
||||
describe("error/ensure", function () {
|
||||
it("Should return input value", function () {
|
||||
var value = new Error();
|
||||
assert.equal(ensureError(value), value);
|
||||
});
|
||||
it("Should crash on invalid value", function () {
|
||||
try {
|
||||
ensureError(null);
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert(error.message.includes("is not an error"));
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensureError(null, { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected an error for name, received null");
|
||||
}
|
||||
});
|
||||
});
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, isError = require("../../error/is");
|
||||
|
||||
describe("error/is", function () {
|
||||
it("Should return true on error", function () { assert.equal(isError(new Error()), true); });
|
||||
|
||||
it("Should return false on native error with no common API exposed", function () {
|
||||
var value = new Error();
|
||||
value.message = null;
|
||||
assert.equal(isError(value), false);
|
||||
});
|
||||
it("Should return false on Error.prototype", function () {
|
||||
assert.equal(isError(Error.prototype), false);
|
||||
});
|
||||
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should return true on custom built ES5 era error", function () {
|
||||
var CustomEs5Error = function () { Error.call(this); };
|
||||
CustomEs5Error.prototype = Object.create(Error.prototype);
|
||||
assert.equal(isError(new CustomEs5Error()), true);
|
||||
});
|
||||
|
||||
it("Should return false on object with no prototype", function () {
|
||||
assert.equal(isError(Object.create(null)), false);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should return false on plain object", function () { assert.equal(isError({}), false); });
|
||||
it("Should return false on function", function () {
|
||||
assert.equal(isError(function () { return true; }), false);
|
||||
});
|
||||
|
||||
it("Should return false on array", function () { assert.equal(isError([]), false); });
|
||||
|
||||
it("Should return false on string", function () { assert.equal(isError("foo"), false); });
|
||||
it("Should return false on empty string", function () { assert.equal(isError(""), false); });
|
||||
it("Should return false on number", function () { assert.equal(isError(123), false); });
|
||||
it("Should return false on NaN", function () { assert.equal(isError(NaN), false); });
|
||||
it("Should return false on boolean", function () { assert.equal(isError(true), false); });
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should return false on symbol", function () {
|
||||
assert.equal(isError(Symbol("foo")), false);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should return false on null", function () { assert.equal(isError(null), false); });
|
||||
it("Should return false on undefined", function () { assert.equal(isError(void 0), false); });
|
||||
});
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, coerceToFinite = require("../../finite/coerce");
|
||||
|
||||
describe("finite/coerce", function () {
|
||||
it("Should return input number", function () {
|
||||
assert.equal(coerceToFinite(123.123), 123.123);
|
||||
});
|
||||
it("Should coerce string", function () { assert.equal(coerceToFinite("12"), 12); });
|
||||
it("Should coerce booleans", function () { assert.equal(coerceToFinite(true), 1); });
|
||||
it("Should coerce number objects", function () {
|
||||
assert.equal(coerceToFinite(new Number(343)), 343);
|
||||
});
|
||||
it("Should coerce objects", function () {
|
||||
assert.equal(coerceToFinite({ valueOf: function () { return 23; } }), 23);
|
||||
});
|
||||
|
||||
it("Should reject infinite number", function () {
|
||||
assert.equal(coerceToFinite(Infinity), null);
|
||||
});
|
||||
it("Should reject NaN", function () { assert.equal(coerceToFinite(NaN), null); });
|
||||
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should not coerce objects with no number representation", function () {
|
||||
assert.equal(coerceToFinite(Object.create(null)), null);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should not coerce null", function () { assert.equal(coerceToFinite(null), null); });
|
||||
it("Should not coerce undefined", function () {
|
||||
assert.equal(coerceToFinite(undefined), null);
|
||||
});
|
||||
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should not coerce symbols", function () {
|
||||
assert.equal(coerceToFinite(Symbol("foo")), null);
|
||||
});
|
||||
}
|
||||
});
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensureFinite = require("../../finite/ensure");
|
||||
|
||||
describe("finite/ensure", function () {
|
||||
it("Should return coerced value", function () { assert.equal(ensureFinite("12.23"), 12.23); });
|
||||
it("Should crash on no value", function () {
|
||||
try {
|
||||
ensureFinite(null);
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "null is not a finite number");
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensureFinite(null, { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected a finite number for name, received null");
|
||||
}
|
||||
});
|
||||
});
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensureFunction = require("../../function/ensure");
|
||||
|
||||
describe("function/ensure", function () {
|
||||
it("Should return input value", function () {
|
||||
var value = function () { return true; };
|
||||
assert.equal(ensureFunction(value), value);
|
||||
});
|
||||
it("Should crash on invalid value", function () {
|
||||
try {
|
||||
ensureFunction(null);
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert(error.message.includes("is not a function"));
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensureFunction(null, { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected a function for name, received null");
|
||||
}
|
||||
});
|
||||
});
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, isFunction = require("../../function/is")
|
||||
, arrowFunctionIfSupported = require("../_lib/arrow-function-if-supported")
|
||||
, classIfSupported = require("../_lib/class-if-supported");
|
||||
|
||||
describe("function/is", function () {
|
||||
it("Should return true on function", function () {
|
||||
assert.equal(isFunction(function () { return true; }), true);
|
||||
});
|
||||
if (arrowFunctionIfSupported) {
|
||||
it("Should return true on arrow function", function () {
|
||||
assert.equal(isFunction(arrowFunctionIfSupported), true);
|
||||
});
|
||||
}
|
||||
if (classIfSupported) {
|
||||
it("Should return true on class", function () {
|
||||
assert.equal(isFunction(classIfSupported), true);
|
||||
});
|
||||
}
|
||||
it("Should return false on reg-exp", function () { assert.equal(isFunction(/foo/), false); });
|
||||
|
||||
it("Should return false on plain object", function () { assert.equal(isFunction({}), false); });
|
||||
it("Should return false on array", function () { assert.equal(isFunction([]), false); });
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should return false on object with no prototype", function () {
|
||||
assert.equal(isFunction(Object.create(null)), false);
|
||||
});
|
||||
}
|
||||
it("Should return false on string", function () { assert.equal(isFunction("foo"), false); });
|
||||
it("Should return false on empty string", function () { assert.equal(isFunction(""), false); });
|
||||
it("Should return false on number", function () { assert.equal(isFunction(123), false); });
|
||||
it("Should return false on NaN", function () { assert.equal(isFunction(NaN), false); });
|
||||
it("Should return false on boolean", function () { assert.equal(isFunction(true), false); });
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should return false on symbol", function () {
|
||||
assert.equal(isFunction(Symbol("foo")), false);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should return false on null", function () { assert.equal(isFunction(null), false); });
|
||||
it("Should return false on undefined", function () {
|
||||
assert.equal(isFunction(void 0), false);
|
||||
});
|
||||
});
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, coerceToInteger = require("../../integer/coerce");
|
||||
|
||||
describe("integer/coerce", function () {
|
||||
it("Should coerce float to integer", function () {
|
||||
assert.equal(coerceToInteger(123.123), 123);
|
||||
assert.equal(coerceToInteger(123.823), 123);
|
||||
assert.equal(coerceToInteger(-123.123), -123);
|
||||
assert.equal(coerceToInteger(-123.823), -123);
|
||||
});
|
||||
it("Should coerce string", function () { assert.equal(coerceToInteger("12.123"), 12); });
|
||||
it("Should coerce booleans", function () { assert.equal(coerceToInteger(true), 1); });
|
||||
it("Should coerce number objects", function () {
|
||||
assert.equal(coerceToInteger(new Number(343)), 343);
|
||||
});
|
||||
it("Should coerce objects", function () {
|
||||
assert.equal(coerceToInteger({ valueOf: function () { return 23; } }), 23);
|
||||
});
|
||||
it("Should coerce number beyond Number.MAX_SAFE_INTEGER", function () {
|
||||
assert.equal(coerceToInteger(9007199254740992), 9007199254740992);
|
||||
});
|
||||
it("Should coerce number beyond Number.MIN_SAFE_INTEGER", function () {
|
||||
assert.equal(coerceToInteger(-9007199254740992), -9007199254740992);
|
||||
});
|
||||
|
||||
it("Should reject infinite number", function () {
|
||||
assert.equal(coerceToInteger(Infinity), null);
|
||||
});
|
||||
it("Should reject NaN", function () { assert.equal(coerceToInteger(NaN), null); });
|
||||
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should not coerce objects with no number representation", function () {
|
||||
assert.equal(coerceToInteger(Object.create(null)), null);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should not coerce null", function () { assert.equal(coerceToInteger(null), null); });
|
||||
it("Should not coerce undefined", function () {
|
||||
assert.equal(coerceToInteger(undefined), null);
|
||||
});
|
||||
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should not coerce symbols", function () {
|
||||
assert.equal(coerceToInteger(Symbol("foo")), null);
|
||||
});
|
||||
}
|
||||
});
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensureInteger = require("../../integer/ensure");
|
||||
|
||||
describe("integer/ensure", function () {
|
||||
it("Should return coerced value", function () { assert.equal(ensureInteger("12.23"), 12); });
|
||||
it("Should crash on no value", function () {
|
||||
try {
|
||||
ensureInteger(null);
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "null is not an integer");
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensureInteger(null, { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected an integer for name, received null");
|
||||
}
|
||||
});
|
||||
});
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensureString = require("../../string/ensure")
|
||||
, isArray = require("../../array/is")
|
||||
, ensureIterable = require("../../iterable/ensure");
|
||||
|
||||
describe("iterable/ensure", function () {
|
||||
it("Should return input value", function () {
|
||||
var value = [];
|
||||
assert.equal(ensureIterable(value), value);
|
||||
});
|
||||
it("Should allow strings with allowString option", function () {
|
||||
var value = "foo";
|
||||
assert.equal(ensureIterable(value, { allowString: true }), value);
|
||||
});
|
||||
it("Should crash on invalid value", function () {
|
||||
try {
|
||||
ensureIterable("foo");
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert(error.message.includes("is not expected iterable"));
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensureIterable("foo", { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected an iterable for name, received foo");
|
||||
}
|
||||
});
|
||||
describe("Should support 'ensureItem' option", function () {
|
||||
it("Should resolve coerced array", function () {
|
||||
var coercedValue = ensureIterable(new Set(["foo", 12]), { ensureItem: ensureString });
|
||||
assert(isArray(coercedValue));
|
||||
assert.deepEqual(coercedValue, ["foo", "12"]);
|
||||
});
|
||||
it("Should crash if some item is invalid", function () {
|
||||
try {
|
||||
ensureIterable(["foo", {}], { ensureItem: ensureString });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert(error.message.includes("is not expected iterable"));
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, isIterable = require("../../iterable/is");
|
||||
|
||||
describe("iterable/is", function () {
|
||||
it("Should return true on array", function () { assert.equal(isIterable([]), true); });
|
||||
it("Should return true on arguments", function () {
|
||||
assert.equal(isIterable((function () { return arguments; })()), true);
|
||||
});
|
||||
it("Should by default return false on string", function () {
|
||||
assert.equal(isIterable("foo"), false);
|
||||
});
|
||||
it("Should accept strings if specified", function () {
|
||||
assert.equal(isIterable("foo", { allowString: true }), true);
|
||||
});
|
||||
|
||||
it("Should support denyEmpty option", function () {
|
||||
assert.equal(isIterable([], { denyEmpty: true }), false);
|
||||
assert.equal(isIterable([null], { denyEmpty: true }), true);
|
||||
assert.equal(isIterable("", { allowString: true, denyEmpty: true }), false);
|
||||
assert.equal(isIterable("foo", { allowString: true, denyEmpty: true }), true);
|
||||
});
|
||||
|
||||
if (typeof Set === "function") {
|
||||
it("Should return true on set", function () { assert.equal(isIterable(new Set()), true); });
|
||||
}
|
||||
if (typeof Map === "function") {
|
||||
it("Should return true on set", function () { assert.equal(isIterable(new Map()), true); });
|
||||
}
|
||||
|
||||
it("Should return false on plain object", function () { assert.equal(isIterable({}), false); });
|
||||
it("Should return false on function", function () {
|
||||
assert.equal(isIterable(function () { return true; }), false);
|
||||
});
|
||||
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should return false on object with no prototype", function () {
|
||||
assert.equal(isIterable(Object.create(null)), false);
|
||||
});
|
||||
}
|
||||
it("Should return false on string", function () { assert.equal(isIterable("foo"), false); });
|
||||
it("Should return false on empty string", function () { assert.equal(isIterable(""), false); });
|
||||
it("Should return false on number", function () { assert.equal(isIterable(123), false); });
|
||||
it("Should return false on NaN", function () { assert.equal(isIterable(NaN), false); });
|
||||
it("Should return false on boolean", function () { assert.equal(isIterable(true), false); });
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should return false on symbol", function () {
|
||||
assert.equal(isIterable(Symbol("foo")), false);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should return false on null", function () { assert.equal(isIterable(null), false); });
|
||||
it("Should return false on undefined", function () {
|
||||
assert.equal(isIterable(void 0), false);
|
||||
});
|
||||
});
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, isToStringTagSupported = require("../../lib/is-to-string-tag-supported");
|
||||
|
||||
describe("lib/is-to-string-tag-supported", function () {
|
||||
it("Should return boolean", function () {
|
||||
assert(typeof isToStringTagSupported === "boolean");
|
||||
});
|
||||
});
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, resolveErrorMessage = require("../../lib/resolve-error-message");
|
||||
|
||||
describe("lib/resolve-error-message", function () {
|
||||
it("Should insert value", function () {
|
||||
assert.equal(resolveErrorMessage("%v is invalid", 12), "12 is invalid");
|
||||
assert.equal(resolveErrorMessage("Value is invalid", 12), "Value is invalid");
|
||||
});
|
||||
it("Should support custom error message via inputOptions.errorMessage", function () {
|
||||
assert.equal(
|
||||
resolveErrorMessage("%v is invalid", null, { errorMessage: "%v is not supported age" }),
|
||||
"null is not supported age"
|
||||
);
|
||||
});
|
||||
it("Should support %n (name) token", function () {
|
||||
assert.equal(resolveErrorMessage("%v is invalid", 12, { name: "foo" }), "12 is invalid");
|
||||
assert.equal(resolveErrorMessage("%n is invalid", 12, { name: "foo" }), "foo is invalid");
|
||||
assert.equal(
|
||||
resolveErrorMessage("%v for %n is invalid", 12, { name: "foo" }),
|
||||
"12 for foo is invalid"
|
||||
);
|
||||
});
|
||||
});
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, handleException = require("../../lib/resolve-exception");
|
||||
|
||||
describe("lib/handle-exception", function () {
|
||||
it("Should throw TypeError", function () {
|
||||
try {
|
||||
handleException(12, "Invalid value");
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Invalid value");
|
||||
}
|
||||
});
|
||||
it("Should resolve value in default message", function () {
|
||||
try {
|
||||
handleException(12, "%v is invalid", {});
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.message, "12 is invalid");
|
||||
}
|
||||
});
|
||||
it("Should support optional values via inputOptions.isOptional", function () {
|
||||
assert.equal(handleException(null, "%v is invalid", { isOptional: true }, null));
|
||||
});
|
||||
it("Should support optional values via inputOptions.default", function () {
|
||||
// prettier-ignore
|
||||
assert.equal(handleException(null, "%v is invalid", { "default": "bar" }), "bar");
|
||||
});
|
||||
it("Should support custom Error constructor", function () {
|
||||
try {
|
||||
handleException(12, "Invalid value", { Error: RangeError });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert(error instanceof RangeError);
|
||||
assert.equal(error.message, "Invalid value");
|
||||
}
|
||||
});
|
||||
});
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, safeToString = require("../../lib/safe-to-string");
|
||||
|
||||
describe("lib/safe-to-string", function () {
|
||||
it("Should return input string", function () { assert.equal(safeToString("foo"), "foo"); });
|
||||
it("Should coerce numbers", function () { assert.equal(safeToString(12), "12"); });
|
||||
it("Should coerce booleans", function () { assert.equal(safeToString(true), "true"); });
|
||||
it("Should coerce string objects", function () {
|
||||
assert.equal(safeToString(new String("bar")), "bar");
|
||||
});
|
||||
it("Should coerce objects", function () {
|
||||
assert.equal(
|
||||
safeToString({ toString: function () { return "Some object"; } }), "Some object"
|
||||
);
|
||||
});
|
||||
it("Should coerce null", function () { assert.equal(safeToString(null), "null"); });
|
||||
it("Should coerce undefined", function () {
|
||||
assert.equal(safeToString(undefined), "undefined");
|
||||
});
|
||||
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should coerce symbols", function () {
|
||||
// eslint-disable-next-line no-undef
|
||||
assert.equal(safeToString(Symbol()), "Symbol()");
|
||||
});
|
||||
}
|
||||
it("Should return null for non coercible values", function () {
|
||||
assert.equal(safeToString({ toString: null }), null);
|
||||
});
|
||||
});
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, toShortString = require("../../lib/to-short-string");
|
||||
|
||||
describe("lib/to-short-string", function () {
|
||||
it("Should return input string", function () { assert.equal(toShortString("foo"), "foo"); });
|
||||
it("Should coerce numbers", function () { assert.equal(toShortString(12), "12"); });
|
||||
it("Should coerce booleans", function () { assert.equal(toShortString(true), "true"); });
|
||||
it("Should coerce string objects", function () {
|
||||
assert.equal(toShortString(new String("bar")), "bar");
|
||||
});
|
||||
it("Should coerce objects", function () {
|
||||
assert.equal(
|
||||
toShortString({ toString: function () { return "Some object"; } }), "Some object"
|
||||
);
|
||||
});
|
||||
it("Should coerce null", function () { assert.equal(toShortString(null), "null"); });
|
||||
it("Should coerce undefined", function () {
|
||||
assert.equal(toShortString(undefined), "undefined");
|
||||
});
|
||||
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should coerce symbols", function () {
|
||||
// eslint-disable-next-line no-undef
|
||||
assert.equal(toShortString(Symbol()), "Symbol()");
|
||||
});
|
||||
}
|
||||
it("Should return replacement non coercible values", function () {
|
||||
assert.equal(toShortString({ toString: null }), "<Non-coercible to string value>");
|
||||
});
|
||||
|
||||
it("Should replace new line characters", function () {
|
||||
assert.equal(toShortString("foo\n\r\u2028\u2029bar"), "foo\\n\\r\\u2028\\u2029bar");
|
||||
});
|
||||
it("Should truncate long string", function () {
|
||||
var str = Math.random().toString(36);
|
||||
while (str.length < 200) str += str;
|
||||
assert.equal(toShortString(str).length, 100);
|
||||
});
|
||||
});
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, coerceToNaturalNumber = require("../../natural-number/coerce");
|
||||
|
||||
describe("natural-number/coerce", function () {
|
||||
it("Should coerce float to integer", function () {
|
||||
assert.equal(coerceToNaturalNumber(123.123), 123);
|
||||
assert.equal(coerceToNaturalNumber(123.823), 123);
|
||||
});
|
||||
it("Should coerce string", function () { assert.equal(coerceToNaturalNumber("12.123"), 12); });
|
||||
it("Should coerce booleans", function () { assert.equal(coerceToNaturalNumber(true), 1); });
|
||||
it("Should coerce number objects", function () {
|
||||
assert.equal(coerceToNaturalNumber(new Number(343)), 343);
|
||||
});
|
||||
it("Should coerce objects", function () {
|
||||
assert.equal(coerceToNaturalNumber({ valueOf: function () { return 23; } }), 23);
|
||||
});
|
||||
it("Should coerce number beyond Number.MAX_SAFE_INTEGER", function () {
|
||||
assert.equal(coerceToNaturalNumber(9007199254740992), 9007199254740992);
|
||||
});
|
||||
|
||||
it("Should reject negative number", function () {
|
||||
assert.equal(coerceToNaturalNumber(-1), null);
|
||||
});
|
||||
it("Should reject infinite number", function () {
|
||||
assert.equal(coerceToNaturalNumber(Infinity), null);
|
||||
});
|
||||
it("Should reject NaN", function () { assert.equal(coerceToNaturalNumber(NaN), null); });
|
||||
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should not coerce objects with no number representation", function () {
|
||||
assert.equal(coerceToNaturalNumber(Object.create(null)), null);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should not coerce null", function () { assert.equal(coerceToNaturalNumber(null), null); });
|
||||
it("Should not coerce undefined", function () {
|
||||
assert.equal(coerceToNaturalNumber(undefined), null);
|
||||
});
|
||||
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should not coerce symbols", function () {
|
||||
assert.equal(coerceToNaturalNumber(Symbol("foo")), null);
|
||||
});
|
||||
}
|
||||
});
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensureNaturalNumber = require("../../natural-number/ensure");
|
||||
|
||||
describe("natural-number/ensure", function () {
|
||||
it("Should return coerced value", function () {
|
||||
assert.equal(ensureNaturalNumber("12.23"), 12);
|
||||
});
|
||||
it("Should crash on no value", function () {
|
||||
try {
|
||||
ensureNaturalNumber(-20);
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "-20 is not a natural number");
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensureNaturalNumber(-20, { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected a natural number for name, received -20");
|
||||
}
|
||||
});
|
||||
it("Should support min validation", function () {
|
||||
try {
|
||||
ensureNaturalNumber(2, { min: 3 });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "2 is not greater or equal 3");
|
||||
}
|
||||
});
|
||||
});
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, coerceToNumber = require("../../number/coerce");
|
||||
|
||||
describe("number/coerce", function () {
|
||||
it("Should return input number", function () {
|
||||
assert.equal(coerceToNumber(123.123), 123.123);
|
||||
});
|
||||
it("Should return input infinite number", function () {
|
||||
assert.equal(coerceToNumber(Infinity), Infinity);
|
||||
});
|
||||
it("Should coerce string", function () { assert.equal(coerceToNumber("12"), 12); });
|
||||
it("Should coerce booleans", function () { assert.equal(coerceToNumber(true), 1); });
|
||||
it("Should coerce number objects", function () {
|
||||
assert.equal(coerceToNumber(new Number(343)), 343);
|
||||
});
|
||||
it("Should coerce objects", function () {
|
||||
assert.equal(coerceToNumber({ valueOf: function () { return 23; } }), 23);
|
||||
});
|
||||
|
||||
it("Should reject NaN", function () { assert.equal(coerceToNumber(NaN), null); });
|
||||
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should not coerce objects with no number representation", function () {
|
||||
assert.equal(coerceToNumber(Object.create(null)), null);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should not coerce null", function () { assert.equal(coerceToNumber(null), null); });
|
||||
it("Should not coerce undefined", function () {
|
||||
assert.equal(coerceToNumber(undefined), null);
|
||||
});
|
||||
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should not coerce symbols", function () {
|
||||
assert.equal(coerceToNumber(Symbol("foo")), null);
|
||||
});
|
||||
}
|
||||
});
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensureNumber = require("../../number/ensure");
|
||||
|
||||
describe("number/ensure", function () {
|
||||
it("Should return coerced value", function () { assert.equal(ensureNumber("12.23"), 12.23); });
|
||||
it("Should crash on no value", function () {
|
||||
try {
|
||||
ensureNumber(null);
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "null is not a number");
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensureNumber(null, { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected a number for name, received null");
|
||||
}
|
||||
});
|
||||
});
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensureObject = require("../../object/ensure");
|
||||
|
||||
describe("object/ensure", function () {
|
||||
it("Should return input value", function () {
|
||||
var value = {};
|
||||
assert.equal(ensureObject(value), value);
|
||||
});
|
||||
it("Should crash on no value", function () {
|
||||
try {
|
||||
ensureObject(null);
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "null is not an object");
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensureObject(null, { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected an object for name, received null");
|
||||
}
|
||||
});
|
||||
});
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, isObject = require("../../object/is");
|
||||
|
||||
describe("object/is", function () {
|
||||
it("Should return true on object", function () { assert.equal(isObject({}), true); });
|
||||
it("Should return true on function", function () {
|
||||
assert.equal(isObject(function () { return true; }), true);
|
||||
});
|
||||
it("Should return true on array", function () { assert.equal(isObject([]), true); });
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should return true on object with no prototype", function () {
|
||||
assert.equal(isObject(Object.create(null)), true);
|
||||
});
|
||||
}
|
||||
it("Should return false on string", function () { assert.equal(isObject("foo"), false); });
|
||||
it("Should return false on empty string", function () { assert.equal(isObject(""), false); });
|
||||
it("Should return false on number", function () { assert.equal(isObject(123), false); });
|
||||
it("Should return false on NaN", function () { assert.equal(isObject(NaN), false); });
|
||||
it("Should return false on boolean", function () { assert.equal(isObject(true), false); });
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should return false on symbol", function () {
|
||||
assert.equal(isObject(Symbol("foo")), false);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should return false on null", function () { assert.equal(isObject(null), false); });
|
||||
it("Should return false on undefined", function () { assert.equal(isObject(void 0), false); });
|
||||
});
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensurePlainFunction = require("../../plain-function/ensure");
|
||||
|
||||
describe("plain-function/ensure", function () {
|
||||
it("Should return input value", function () {
|
||||
var value = function () { return true; };
|
||||
assert.equal(ensurePlainFunction(value), value);
|
||||
});
|
||||
it("Should crash on invalid value", function () {
|
||||
try {
|
||||
ensurePlainFunction(null);
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert(error.message.includes("is not a plain function"));
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensurePlainFunction(null, { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected a plain function for name, received null");
|
||||
}
|
||||
});
|
||||
});
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, isPlainFunction = require("../../plain-function/is")
|
||||
, arrowFunctionIfSupported = require("../_lib/arrow-function-if-supported")
|
||||
, classIfSupported = require("../_lib/class-if-supported");
|
||||
|
||||
describe("plain-function/is", function () {
|
||||
it("Should return true on function", function () {
|
||||
assert.equal(isPlainFunction(function () { return true; }), true);
|
||||
});
|
||||
if (arrowFunctionIfSupported) {
|
||||
it("Should return true on arrow function", function () {
|
||||
assert.equal(isPlainFunction(arrowFunctionIfSupported), true);
|
||||
});
|
||||
}
|
||||
if (classIfSupported) {
|
||||
it("Should return false on class", function () {
|
||||
assert.equal(isPlainFunction(classIfSupported), false);
|
||||
});
|
||||
}
|
||||
it("Should return false on reg-exp", function () {
|
||||
assert.equal(isPlainFunction(/foo/), false);
|
||||
});
|
||||
|
||||
it("Should return false on plain object", function () {
|
||||
assert.equal(isPlainFunction({}), false);
|
||||
});
|
||||
it("Should return false on array", function () { assert.equal(isPlainFunction([]), false); });
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should return false on object with no prototype", function () {
|
||||
assert.equal(isPlainFunction(Object.create(null)), false);
|
||||
});
|
||||
}
|
||||
it("Should return false on string", function () {
|
||||
assert.equal(isPlainFunction("foo"), false);
|
||||
});
|
||||
it("Should return false on empty string", function () {
|
||||
assert.equal(isPlainFunction(""), false);
|
||||
});
|
||||
it("Should return false on number", function () { assert.equal(isPlainFunction(123), false); });
|
||||
it("Should return false on NaN", function () { assert.equal(isPlainFunction(NaN), false); });
|
||||
it("Should return false on boolean", function () {
|
||||
assert.equal(isPlainFunction(true), false);
|
||||
});
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should return false on symbol", function () {
|
||||
assert.equal(isPlainFunction(Symbol("foo")), false);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should return false on null", function () { assert.equal(isPlainFunction(null), false); });
|
||||
it("Should return false on undefined", function () {
|
||||
assert.equal(isPlainFunction(void 0), false);
|
||||
});
|
||||
});
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensureString = require("../../string/ensure")
|
||||
, ensurePlainObject = require("../../plain-object/ensure");
|
||||
|
||||
describe("plain-object/ensure", function () {
|
||||
it("Should return input value", function () {
|
||||
var value = {};
|
||||
assert.equal(ensurePlainObject(value), value);
|
||||
});
|
||||
it("Should crash on invalid value", function () {
|
||||
try {
|
||||
ensurePlainObject(null);
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "null is not a plain object");
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensurePlainObject(null, { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected a plain object for name, received null");
|
||||
}
|
||||
});
|
||||
it("Should support allowedKeys option", function () {
|
||||
var value = { foo: "bar", marko: "elo" };
|
||||
assert.equal(ensurePlainObject(value, { allowedKeys: ["foo", "marko"] }), value);
|
||||
try {
|
||||
ensurePlainObject(value, { allowedKeys: ["marko"] });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message.indexOf("is not a plain object") !== -1, true);
|
||||
}
|
||||
});
|
||||
|
||||
it("Should support ensurePropertyValue option", function () {
|
||||
assert.deepEqual(
|
||||
ensurePlainObject({ foo: "bar", marko: 12 }, { ensurePropertyValue: ensureString }),
|
||||
{ foo: "bar", marko: "12" }
|
||||
);
|
||||
try {
|
||||
ensurePlainObject({ foo: "bar", marko: {} }, { ensurePropertyValue: ensureString });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message.indexOf("is not a plain object") !== -1, true);
|
||||
}
|
||||
});
|
||||
});
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, isPlainObject = require("../../plain-object/is");
|
||||
|
||||
describe("plain-object/is", function () {
|
||||
it("Should return true on plain object", function () {
|
||||
assert.equal(isPlainObject({}), true);
|
||||
});
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should return true on object with no prototype", function () {
|
||||
assert.equal(isPlainObject(Object.create(null)), true);
|
||||
});
|
||||
it(
|
||||
"Should return false on object that inherits from object with no prototype",
|
||||
function () { assert.equal(isPlainObject(Object.create(Object.create(null))), false); }
|
||||
);
|
||||
}
|
||||
it("Should return false on Object.prototype", function () {
|
||||
assert.equal(isPlainObject(Object.prototype), false);
|
||||
});
|
||||
it("Should return false on prototype that derives from Object.prototype", function () {
|
||||
assert.equal(isPlainObject(RegExp.prototype), false);
|
||||
});
|
||||
|
||||
it("Should return false on function", function () {
|
||||
assert.equal(isPlainObject(function () { return true; }), false);
|
||||
});
|
||||
|
||||
it("Should return false on string", function () { assert.equal(isPlainObject("foo"), false); });
|
||||
it("Should return false on empty string", function () {
|
||||
assert.equal(isPlainObject(""), false);
|
||||
});
|
||||
it("Should return false on number", function () { assert.equal(isPlainObject(123), false); });
|
||||
it("Should return false on NaN", function () { assert.equal(isPlainObject(NaN), false); });
|
||||
it("Should return false on boolean", function () { assert.equal(isPlainObject(true), false); });
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should return false on symbol", function () {
|
||||
assert.equal(isPlainObject(Symbol("foo")), false);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should return false on null", function () { assert.equal(isPlainObject(null), false); });
|
||||
it("Should return false on undefined", function () {
|
||||
assert.equal(isPlainObject(void 0), false);
|
||||
});
|
||||
});
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensurePromise = require("../../promise/ensure");
|
||||
|
||||
describe("promise/ensure", function () {
|
||||
it("Should return input value", function () {
|
||||
var value = Promise.resolve();
|
||||
assert.equal(ensurePromise(value), value);
|
||||
});
|
||||
it("Should crash on no value", function () {
|
||||
try {
|
||||
ensurePromise({});
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "[object Object] is not a promise");
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensurePromise(null, { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected a promise for name, received null");
|
||||
}
|
||||
});
|
||||
});
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, isPromise = require("../../promise/is");
|
||||
|
||||
describe("promise/is", function () {
|
||||
if (typeof Promise === "function") {
|
||||
it("Should return true on promise", function () {
|
||||
assert.equal(isPromise(Promise.resolve()), true);
|
||||
});
|
||||
}
|
||||
it("Should return false on non-promise thenable", function () {
|
||||
assert.equal(isPromise({ then: function () { return true; } }), false);
|
||||
});
|
||||
|
||||
it("Should return false on plain object", function () { assert.equal(isPromise({}), false); });
|
||||
it("Should return false on function", function () {
|
||||
assert.equal(isPromise(function () { return true; }), false);
|
||||
});
|
||||
it("Should return false on array", function () { assert.equal(isPromise([]), false); });
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should return false on object with no prototype", function () {
|
||||
assert.equal(isPromise(Object.create(null)), false);
|
||||
});
|
||||
}
|
||||
it("Should return false on string", function () { assert.equal(isPromise("foo"), false); });
|
||||
it("Should return false on empty string", function () { assert.equal(isPromise(""), false); });
|
||||
it("Should return false on number", function () { assert.equal(isPromise(123), false); });
|
||||
it("Should return false on NaN", function () { assert.equal(isPromise(NaN), false); });
|
||||
it("Should return false on boolean", function () { assert.equal(isPromise(true), false); });
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should return false on symbol", function () {
|
||||
assert.equal(isPromise(Symbol("foo")), false);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should return false on null", function () { assert.equal(isPromise(null), false); });
|
||||
it("Should return false on undefined", function () { assert.equal(isPromise(void 0), false); });
|
||||
});
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, isPrototype = require("../../prototype/is");
|
||||
|
||||
describe("prototype/is", function () {
|
||||
it("Should return true on prototype", function () {
|
||||
assert.equal(isPrototype(Object.prototype), true);
|
||||
});
|
||||
it("Should return false on plain object", function () {
|
||||
assert.equal(isPrototype({}), false);
|
||||
});
|
||||
it("Should return false on function", function () {
|
||||
assert.equal(isPrototype(function () { return true; }), false);
|
||||
});
|
||||
it("Should return false on array", function () { assert.equal(isPrototype([]), false); });
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should return false on object with no prototype", function () {
|
||||
assert.equal(isPrototype(Object.create(null)), false);
|
||||
});
|
||||
}
|
||||
it("Should return false on string", function () { assert.equal(isPrototype("foo"), false); });
|
||||
it("Should return false on empty string", function () {
|
||||
assert.equal(isPrototype(""), false);
|
||||
});
|
||||
it("Should return false on number", function () { assert.equal(isPrototype(123), false); });
|
||||
it("Should return false on NaN", function () { assert.equal(isPrototype(NaN), false); });
|
||||
it("Should return false on boolean", function () { assert.equal(isPrototype(true), false); });
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should return false on symbol", function () {
|
||||
assert.equal(isPrototype(Symbol("foo")), false);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should return false on null", function () { assert.equal(isPrototype(null), false); });
|
||||
it("Should return false on undefined", function () {
|
||||
assert.equal(isPrototype(void 0), false);
|
||||
});
|
||||
});
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensureRegExp = require("../../reg-exp/ensure");
|
||||
|
||||
describe("reg-exp/ensure", function () {
|
||||
it("Should return input value", function () {
|
||||
var value = /foo/;
|
||||
assert.equal(ensureRegExp(value), value);
|
||||
});
|
||||
it("Should crash on invalid value", function () {
|
||||
try {
|
||||
ensureRegExp(null);
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert(error.message.includes("is not a regular expression"));
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensureRegExp(null, { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected a regular expression for name, received null");
|
||||
}
|
||||
});
|
||||
});
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, isRegExp = require("../../reg-exp/is");
|
||||
|
||||
describe("reg-exp/is", function () {
|
||||
it("Should return true on regular expression", function () {
|
||||
assert.equal(isRegExp(/foo/), true);
|
||||
});
|
||||
|
||||
it("Should return false on native regular expression with no common API exposed", function () {
|
||||
var re = /foo/;
|
||||
re.test = null;
|
||||
assert.equal(isRegExp(re), false);
|
||||
});
|
||||
it("Should return false on RegExp.prototype", function () {
|
||||
assert.equal(isRegExp(RegExp.prototype), false);
|
||||
});
|
||||
it("Should return false on regexp like string", function () {
|
||||
assert.equal(isRegExp("/foo/"), false);
|
||||
});
|
||||
|
||||
it("Should return false on plain object", function () { assert.equal(isRegExp({}), false); });
|
||||
it("Should return false on function", function () {
|
||||
assert.equal(isRegExp(function () { return true; }), false);
|
||||
});
|
||||
|
||||
it("Should return false on array", function () { assert.equal(isRegExp([]), false); });
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should return false on object with no prototype", function () {
|
||||
assert.equal(isRegExp(Object.create(null)), false);
|
||||
});
|
||||
}
|
||||
it("Should return false on string", function () { assert.equal(isRegExp("foo"), false); });
|
||||
it("Should return false on empty string", function () { assert.equal(isRegExp(""), false); });
|
||||
it("Should return false on number", function () { assert.equal(isRegExp(123), false); });
|
||||
it("Should return false on NaN", function () { assert.equal(isRegExp(NaN), false); });
|
||||
it("Should return false on boolean", function () { assert.equal(isRegExp(true), false); });
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should return false on symbol", function () {
|
||||
assert.equal(isRegExp(Symbol("foo")), false);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should return false on null", function () { assert.equal(isRegExp(null), false); });
|
||||
it("Should return false on undefined", function () { assert.equal(isRegExp(void 0), false); });
|
||||
});
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, coerceToSafeInteger = require("../../safe-integer/coerce");
|
||||
|
||||
describe("safe-integer/coerce", function () {
|
||||
it("Should coerce float to integer", function () {
|
||||
assert.equal(coerceToSafeInteger(123.123), 123);
|
||||
assert.equal(coerceToSafeInteger(123.823), 123);
|
||||
assert.equal(coerceToSafeInteger(-123.123), -123);
|
||||
assert.equal(coerceToSafeInteger(-123.823), -123);
|
||||
});
|
||||
it("Should coerce string", function () { assert.equal(coerceToSafeInteger("12.123"), 12); });
|
||||
it("Should coerce booleans", function () { assert.equal(coerceToSafeInteger(true), 1); });
|
||||
it("Should coerce number objects", function () {
|
||||
assert.equal(coerceToSafeInteger(new Number(343)), 343);
|
||||
});
|
||||
it("Should coerce objects", function () {
|
||||
assert.equal(coerceToSafeInteger({ valueOf: function () { return 23; } }), 23);
|
||||
});
|
||||
it("Should reject infinite number", function () {
|
||||
assert.equal(coerceToSafeInteger(Infinity), null);
|
||||
});
|
||||
it("Should reject number beyond Number.MAX_SAFE_INTEGER", function () {
|
||||
assert.equal(coerceToSafeInteger(9007199254740992), null);
|
||||
});
|
||||
it("Should reject number beyond Number.MIN_SAFE_INTEGER", function () {
|
||||
assert.equal(coerceToSafeInteger(-9007199254740992), null);
|
||||
});
|
||||
|
||||
it("Should reject NaN", function () { assert.equal(coerceToSafeInteger(NaN), null); });
|
||||
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should not coerce objects with no number representation", function () {
|
||||
assert.equal(coerceToSafeInteger(Object.create(null)), null);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should not coerce null", function () { assert.equal(coerceToSafeInteger(null), null); });
|
||||
it("Should not coerce undefined", function () {
|
||||
assert.equal(coerceToSafeInteger(undefined), null);
|
||||
});
|
||||
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should not coerce symbols", function () {
|
||||
assert.equal(coerceToSafeInteger(Symbol("foo")), null);
|
||||
});
|
||||
}
|
||||
});
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensureSafeInteger = require("../../safe-integer/ensure");
|
||||
|
||||
describe("safe-integer/ensure", function () {
|
||||
it("Should return coerced value", function () {
|
||||
assert.equal(ensureSafeInteger("12.23"), 12);
|
||||
});
|
||||
it("Should crash on no value", function () {
|
||||
try {
|
||||
ensureSafeInteger(null);
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "null is not a safe integer");
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensureSafeInteger(null, { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected a safe integer for name, received null");
|
||||
}
|
||||
});
|
||||
});
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, coerceToString = require("../../string/coerce");
|
||||
|
||||
describe("string/coerce", function () {
|
||||
it("Should return input string", function () { assert.equal(coerceToString("foo"), "foo"); });
|
||||
it("Should coerce numbers", function () { assert.equal(coerceToString(12), "12"); });
|
||||
it("Should coerce booleans", function () { assert.equal(coerceToString(true), "true"); });
|
||||
it("Should coerce string objects", function () {
|
||||
assert.equal(coerceToString(new String("bar")), "bar");
|
||||
});
|
||||
it("Should coerce objects", function () {
|
||||
assert.equal(
|
||||
coerceToString({ toString: function () { return "Some object"; } }), "Some object"
|
||||
);
|
||||
});
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should not coerce objects with no toString", function () {
|
||||
assert.equal(coerceToString(Object.create(null)), null);
|
||||
});
|
||||
}
|
||||
it("Should not coerce objects with no custom toString", function () {
|
||||
assert.equal(coerceToString({}), null);
|
||||
});
|
||||
it("Should not coerce null", function () { assert.equal(coerceToString(null), null); });
|
||||
it("Should not coerce undefined", function () {
|
||||
assert.equal(coerceToString(undefined), null);
|
||||
});
|
||||
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should not coerce symbols", function () {
|
||||
assert.equal(coerceToString(Symbol("foo")), null);
|
||||
});
|
||||
}
|
||||
});
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensureString = require("../../string/ensure");
|
||||
|
||||
describe("string/ensure", function () {
|
||||
it("Should return coerced value", function () { assert.equal(ensureString(12), "12"); });
|
||||
it("Should crash on no value", function () {
|
||||
try {
|
||||
ensureString(null);
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "null is not a string");
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensureString(null, { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected a string for name, received null");
|
||||
}
|
||||
});
|
||||
});
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, ensureThenable = require("../../thenable/ensure");
|
||||
|
||||
describe("thenable/ensure", function () {
|
||||
it("Should return input value", function () {
|
||||
var value = { then: function () { return true; } };
|
||||
assert.equal(ensureThenable(value), value);
|
||||
});
|
||||
it("Should crash on no value", function () {
|
||||
try {
|
||||
ensureThenable({});
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "[object Object] is not a thenable");
|
||||
}
|
||||
});
|
||||
it("Should provide alternative error message when name option is passed", function () {
|
||||
try {
|
||||
ensureThenable({}, { name: "name" });
|
||||
throw new Error("Unexpected");
|
||||
} catch (error) {
|
||||
assert.equal(error.name, "TypeError");
|
||||
assert.equal(error.message, "Expected a thenable for name, received [object Object]");
|
||||
}
|
||||
});
|
||||
});
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
"use strict";
|
||||
|
||||
var assert = require("chai").assert
|
||||
, isThenable = require("../../thenable/is");
|
||||
|
||||
describe("thenable/is", function () {
|
||||
it("Should return true on object with `then` method", function () {
|
||||
assert.equal(isThenable({ then: function () { return true; } }), true);
|
||||
});
|
||||
if (typeof Promise === "function") {
|
||||
it("Should return true on promise", function () {
|
||||
assert.equal(isThenable(Promise.resolve()), true);
|
||||
});
|
||||
}
|
||||
it("Should return false on object with `then` non callable property", function () {
|
||||
assert.equal(isThenable({ then: {} }), false);
|
||||
});
|
||||
|
||||
it("Should return false on plain object", function () { assert.equal(isThenable({}), false); });
|
||||
it("Should return false on function", function () {
|
||||
assert.equal(isThenable(function () { return true; }), false);
|
||||
});
|
||||
it("Should return false on array", function () { assert.equal(isThenable([]), false); });
|
||||
if (typeof Object.create === "function") {
|
||||
it("Should return false on object with no prototype", function () {
|
||||
assert.equal(isThenable(Object.create(null)), false);
|
||||
});
|
||||
}
|
||||
it("Should return false on string", function () { assert.equal(isThenable("foo"), false); });
|
||||
it("Should return false on empty string", function () { assert.equal(isThenable(""), false); });
|
||||
it("Should return false on number", function () { assert.equal(isThenable(123), false); });
|
||||
it("Should return false on NaN", function () { assert.equal(isThenable(NaN), false); });
|
||||
it("Should return false on boolean", function () { assert.equal(isThenable(true), false); });
|
||||
if (typeof Symbol === "function") {
|
||||
it("Should return false on symbol", function () {
|
||||
assert.equal(isThenable(Symbol("foo")), false);
|
||||
});
|
||||
}
|
||||
|
||||
it("Should return false on null", function () { assert.equal(isThenable(null), false); });
|
||||
it("Should return false on undefined", function () {
|
||||
assert.equal(isThenable(void 0), false);
|
||||
});
|
||||
});
|
||||
Loaded 100 of 3408 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user