123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796 |
- /* globals window, HTMLElement */
- 'use strict';
- /**!
- * is
- * the definitive JavaScript type testing library
- *
- * @copyright 2013-2014 Enrico Marino / Jordan Harband
- * @license MIT
- */
- var objProto = Object.prototype;
- var owns = objProto.hasOwnProperty;
- var toStr = objProto.toString;
- var symbolValueOf;
- if (typeof Symbol === 'function') {
- symbolValueOf = Symbol.prototype.valueOf;
- }
- var isActualNaN = function (value) {
- return value !== value;
- };
- var NON_HOST_TYPES = {
- 'boolean': 1,
- number: 1,
- string: 1,
- undefined: 1
- };
- var base64Regex = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/;
- var hexRegex = /^[A-Fa-f0-9]+$/;
- /**
- * Expose `is`
- */
- var is = {};
- /**
- * Test general.
- */
- /**
- * is.type
- * Test if `value` is a type of `type`.
- *
- * @param {Mixed} value value to test
- * @param {String} type type
- * @return {Boolean} true if `value` is a type of `type`, false otherwise
- * @api public
- */
- is.a = is.type = function (value, type) {
- return typeof value === type;
- };
- /**
- * is.defined
- * Test if `value` is defined.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if 'value' is defined, false otherwise
- * @api public
- */
- is.defined = function (value) {
- return typeof value !== 'undefined';
- };
- /**
- * is.empty
- * Test if `value` is empty.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is empty, false otherwise
- * @api public
- */
- is.empty = function (value) {
- var type = toStr.call(value);
- var key;
- if (type === '[object Array]' || type === '[object Arguments]' || type === '[object String]') {
- return value.length === 0;
- }
- if (type === '[object Object]') {
- for (key in value) {
- if (owns.call(value, key)) {
- return false;
- }
- }
- return true;
- }
- return !value;
- };
- /**
- * is.equal
- * Test if `value` is equal to `other`.
- *
- * @param {Mixed} value value to test
- * @param {Mixed} other value to compare with
- * @return {Boolean} true if `value` is equal to `other`, false otherwise
- */
- is.equal = function equal(value, other) {
- if (value === other) {
- return true;
- }
- var type = toStr.call(value);
- var key;
- if (type !== toStr.call(other)) {
- return false;
- }
- if (type === '[object Object]') {
- for (key in value) {
- if (!is.equal(value[key], other[key]) || !(key in other)) {
- return false;
- }
- }
- for (key in other) {
- if (!is.equal(value[key], other[key]) || !(key in value)) {
- return false;
- }
- }
- return true;
- }
- if (type === '[object Array]') {
- key = value.length;
- if (key !== other.length) {
- return false;
- }
- while (key--) {
- if (!is.equal(value[key], other[key])) {
- return false;
- }
- }
- return true;
- }
- if (type === '[object Function]') {
- return value.prototype === other.prototype;
- }
- if (type === '[object Date]') {
- return value.getTime() === other.getTime();
- }
- return false;
- };
- /**
- * is.hosted
- * Test if `value` is hosted by `host`.
- *
- * @param {Mixed} value to test
- * @param {Mixed} host host to test with
- * @return {Boolean} true if `value` is hosted by `host`, false otherwise
- * @api public
- */
- is.hosted = function (value, host) {
- var type = typeof host[value];
- return type === 'object' ? !!host[value] : !NON_HOST_TYPES[type];
- };
- /**
- * is.instance
- * Test if `value` is an instance of `constructor`.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is an instance of `constructor`
- * @api public
- */
- is.instance = is['instanceof'] = function (value, constructor) {
- return value instanceof constructor;
- };
- /**
- * is.nil / is.null
- * Test if `value` is null.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is null, false otherwise
- * @api public
- */
- is.nil = is['null'] = function (value) {
- return value === null;
- };
- /**
- * is.undef / is.undefined
- * Test if `value` is undefined.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is undefined, false otherwise
- * @api public
- */
- is.undef = is.undefined = function (value) {
- return typeof value === 'undefined';
- };
- /**
- * Test arguments.
- */
- /**
- * is.args
- * Test if `value` is an arguments object.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is an arguments object, false otherwise
- * @api public
- */
- is.args = is.arguments = function (value) {
- var isStandardArguments = toStr.call(value) === '[object Arguments]';
- var isOldArguments = !is.array(value) && is.arraylike(value) && is.object(value) && is.fn(value.callee);
- return isStandardArguments || isOldArguments;
- };
- /**
- * Test array.
- */
- /**
- * is.array
- * Test if 'value' is an array.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is an array, false otherwise
- * @api public
- */
- is.array = Array.isArray || function (value) {
- return toStr.call(value) === '[object Array]';
- };
- /**
- * is.arguments.empty
- * Test if `value` is an empty arguments object.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is an empty arguments object, false otherwise
- * @api public
- */
- is.args.empty = function (value) {
- return is.args(value) && value.length === 0;
- };
- /**
- * is.array.empty
- * Test if `value` is an empty array.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is an empty array, false otherwise
- * @api public
- */
- is.array.empty = function (value) {
- return is.array(value) && value.length === 0;
- };
- /**
- * is.arraylike
- * Test if `value` is an arraylike object.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is an arguments object, false otherwise
- * @api public
- */
- is.arraylike = function (value) {
- return !!value && !is.bool(value)
- && owns.call(value, 'length')
- && isFinite(value.length)
- && is.number(value.length)
- && value.length >= 0;
- };
- /**
- * Test boolean.
- */
- /**
- * is.bool
- * Test if `value` is a boolean.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is a boolean, false otherwise
- * @api public
- */
- is.bool = is['boolean'] = function (value) {
- return toStr.call(value) === '[object Boolean]';
- };
- /**
- * is.false
- * Test if `value` is false.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is false, false otherwise
- * @api public
- */
- is['false'] = function (value) {
- return is.bool(value) && Boolean(Number(value)) === false;
- };
- /**
- * is.true
- * Test if `value` is true.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is true, false otherwise
- * @api public
- */
- is['true'] = function (value) {
- return is.bool(value) && Boolean(Number(value)) === true;
- };
- /**
- * Test date.
- */
- /**
- * is.date
- * Test if `value` is a date.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is a date, false otherwise
- * @api public
- */
- is.date = function (value) {
- return toStr.call(value) === '[object Date]';
- };
- /**
- * is.date.valid
- * Test if `value` is a valid date.
- *
- * @param {Mixed} value value to test
- * @returns {Boolean} true if `value` is a valid date, false otherwise
- */
- is.date.valid = function (value) {
- return is.date(value) && !isNaN(Number(value));
- };
- /**
- * Test element.
- */
- /**
- * is.element
- * Test if `value` is an html element.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is an HTML Element, false otherwise
- * @api public
- */
- is.element = function (value) {
- return value !== undefined
- && typeof HTMLElement !== 'undefined'
- && value instanceof HTMLElement
- && value.nodeType === 1;
- };
- /**
- * Test error.
- */
- /**
- * is.error
- * Test if `value` is an error object.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is an error object, false otherwise
- * @api public
- */
- is.error = function (value) {
- return toStr.call(value) === '[object Error]';
- };
- /**
- * Test function.
- */
- /**
- * is.fn / is.function (deprecated)
- * Test if `value` is a function.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is a function, false otherwise
- * @api public
- */
- is.fn = is['function'] = function (value) {
- var isAlert = typeof window !== 'undefined' && value === window.alert;
- return isAlert || toStr.call(value) === '[object Function]';
- };
- /**
- * Test number.
- */
- /**
- * is.number
- * Test if `value` is a number.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is a number, false otherwise
- * @api public
- */
- is.number = function (value) {
- return toStr.call(value) === '[object Number]';
- };
- /**
- * is.infinite
- * Test if `value` is positive or negative infinity.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is positive or negative Infinity, false otherwise
- * @api public
- */
- is.infinite = function (value) {
- return value === Infinity || value === -Infinity;
- };
- /**
- * is.decimal
- * Test if `value` is a decimal number.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is a decimal number, false otherwise
- * @api public
- */
- is.decimal = function (value) {
- return is.number(value) && !isActualNaN(value) && !is.infinite(value) && value % 1 !== 0;
- };
- /**
- * is.divisibleBy
- * Test if `value` is divisible by `n`.
- *
- * @param {Number} value value to test
- * @param {Number} n dividend
- * @return {Boolean} true if `value` is divisible by `n`, false otherwise
- * @api public
- */
- is.divisibleBy = function (value, n) {
- var isDividendInfinite = is.infinite(value);
- var isDivisorInfinite = is.infinite(n);
- var isNonZeroNumber = is.number(value) && !isActualNaN(value) && is.number(n) && !isActualNaN(n) && n !== 0;
- return isDividendInfinite || isDivisorInfinite || (isNonZeroNumber && value % n === 0);
- };
- /**
- * is.integer
- * Test if `value` is an integer.
- *
- * @param value to test
- * @return {Boolean} true if `value` is an integer, false otherwise
- * @api public
- */
- is.integer = is['int'] = function (value) {
- return is.number(value) && !isActualNaN(value) && value % 1 === 0;
- };
- /**
- * is.maximum
- * Test if `value` is greater than 'others' values.
- *
- * @param {Number} value value to test
- * @param {Array} others values to compare with
- * @return {Boolean} true if `value` is greater than `others` values
- * @api public
- */
- is.maximum = function (value, others) {
- if (isActualNaN(value)) {
- throw new TypeError('NaN is not a valid value');
- } else if (!is.arraylike(others)) {
- throw new TypeError('second argument must be array-like');
- }
- var len = others.length;
- while (--len >= 0) {
- if (value < others[len]) {
- return false;
- }
- }
- return true;
- };
- /**
- * is.minimum
- * Test if `value` is less than `others` values.
- *
- * @param {Number} value value to test
- * @param {Array} others values to compare with
- * @return {Boolean} true if `value` is less than `others` values
- * @api public
- */
- is.minimum = function (value, others) {
- if (isActualNaN(value)) {
- throw new TypeError('NaN is not a valid value');
- } else if (!is.arraylike(others)) {
- throw new TypeError('second argument must be array-like');
- }
- var len = others.length;
- while (--len >= 0) {
- if (value > others[len]) {
- return false;
- }
- }
- return true;
- };
- /**
- * is.nan
- * Test if `value` is not a number.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is not a number, false otherwise
- * @api public
- */
- is.nan = function (value) {
- return !is.number(value) || value !== value;
- };
- /**
- * is.even
- * Test if `value` is an even number.
- *
- * @param {Number} value value to test
- * @return {Boolean} true if `value` is an even number, false otherwise
- * @api public
- */
- is.even = function (value) {
- return is.infinite(value) || (is.number(value) && value === value && value % 2 === 0);
- };
- /**
- * is.odd
- * Test if `value` is an odd number.
- *
- * @param {Number} value value to test
- * @return {Boolean} true if `value` is an odd number, false otherwise
- * @api public
- */
- is.odd = function (value) {
- return is.infinite(value) || (is.number(value) && value === value && value % 2 !== 0);
- };
- /**
- * is.ge
- * Test if `value` is greater than or equal to `other`.
- *
- * @param {Number} value value to test
- * @param {Number} other value to compare with
- * @return {Boolean}
- * @api public
- */
- is.ge = function (value, other) {
- if (isActualNaN(value) || isActualNaN(other)) {
- throw new TypeError('NaN is not a valid value');
- }
- return !is.infinite(value) && !is.infinite(other) && value >= other;
- };
- /**
- * is.gt
- * Test if `value` is greater than `other`.
- *
- * @param {Number} value value to test
- * @param {Number} other value to compare with
- * @return {Boolean}
- * @api public
- */
- is.gt = function (value, other) {
- if (isActualNaN(value) || isActualNaN(other)) {
- throw new TypeError('NaN is not a valid value');
- }
- return !is.infinite(value) && !is.infinite(other) && value > other;
- };
- /**
- * is.le
- * Test if `value` is less than or equal to `other`.
- *
- * @param {Number} value value to test
- * @param {Number} other value to compare with
- * @return {Boolean} if 'value' is less than or equal to 'other'
- * @api public
- */
- is.le = function (value, other) {
- if (isActualNaN(value) || isActualNaN(other)) {
- throw new TypeError('NaN is not a valid value');
- }
- return !is.infinite(value) && !is.infinite(other) && value <= other;
- };
- /**
- * is.lt
- * Test if `value` is less than `other`.
- *
- * @param {Number} value value to test
- * @param {Number} other value to compare with
- * @return {Boolean} if `value` is less than `other`
- * @api public
- */
- is.lt = function (value, other) {
- if (isActualNaN(value) || isActualNaN(other)) {
- throw new TypeError('NaN is not a valid value');
- }
- return !is.infinite(value) && !is.infinite(other) && value < other;
- };
- /**
- * is.within
- * Test if `value` is within `start` and `finish`.
- *
- * @param {Number} value value to test
- * @param {Number} start lower bound
- * @param {Number} finish upper bound
- * @return {Boolean} true if 'value' is is within 'start' and 'finish'
- * @api public
- */
- is.within = function (value, start, finish) {
- if (isActualNaN(value) || isActualNaN(start) || isActualNaN(finish)) {
- throw new TypeError('NaN is not a valid value');
- } else if (!is.number(value) || !is.number(start) || !is.number(finish)) {
- throw new TypeError('all arguments must be numbers');
- }
- var isAnyInfinite = is.infinite(value) || is.infinite(start) || is.infinite(finish);
- return isAnyInfinite || (value >= start && value <= finish);
- };
- /**
- * Test object.
- */
- /**
- * is.object
- * Test if `value` is an object.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is an object, false otherwise
- * @api public
- */
- is.object = function (value) {
- return toStr.call(value) === '[object Object]';
- };
- /**
- * is.primitive
- * Test if `value` is a primitive.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is a primitive, false otherwise
- * @api public
- */
- is.primitive = function isPrimitive(value) {
- if (!value) {
- return true;
- }
- if (typeof value === 'object' || is.object(value) || is.fn(value) || is.array(value)) {
- return false;
- }
- return true;
- };
- /**
- * is.hash
- * Test if `value` is a hash - a plain object literal.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is a hash, false otherwise
- * @api public
- */
- is.hash = function (value) {
- return is.object(value) && value.constructor === Object && !value.nodeType && !value.setInterval;
- };
- /**
- * Test regexp.
- */
- /**
- * is.regexp
- * Test if `value` is a regular expression.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is a regexp, false otherwise
- * @api public
- */
- is.regexp = function (value) {
- return toStr.call(value) === '[object RegExp]';
- };
- /**
- * Test string.
- */
- /**
- * is.string
- * Test if `value` is a string.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if 'value' is a string, false otherwise
- * @api public
- */
- is.string = function (value) {
- return toStr.call(value) === '[object String]';
- };
- /**
- * Test base64 string.
- */
- /**
- * is.base64
- * Test if `value` is a valid base64 encoded string.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if 'value' is a base64 encoded string, false otherwise
- * @api public
- */
- is.base64 = function (value) {
- return is.string(value) && (!value.length || base64Regex.test(value));
- };
- /**
- * Test base64 string.
- */
- /**
- * is.hex
- * Test if `value` is a valid hex encoded string.
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if 'value' is a hex encoded string, false otherwise
- * @api public
- */
- is.hex = function (value) {
- return is.string(value) && (!value.length || hexRegex.test(value));
- };
- /**
- * is.symbol
- * Test if `value` is an ES6 Symbol
- *
- * @param {Mixed} value value to test
- * @return {Boolean} true if `value` is a Symbol, false otherise
- * @api public
- */
- is.symbol = function (value) {
- return typeof Symbol === 'function' && toStr.call(value) === '[object Symbol]' && typeof symbolValueOf.call(value) === 'symbol';
- };
- module.exports = is;
|