@sindresorhus/is
Advanced tools
Comparing version 0.2.1 to 0.3.0
49
index.js
@@ -64,3 +64,3 @@ 'use strict'; | ||
is.number = isOfType('number'); | ||
is.boolean = isOfType('boolean'); | ||
is.boolean = x => x === true || x === false; | ||
is.symbol = isOfType('symbol'); | ||
@@ -165,2 +165,49 @@ | ||
const NODE_TYPE_ELEMENT = 1; | ||
const DOM_PROPERTIES_TO_CHECK = [ | ||
'innerHTML', | ||
'ownerDocument', | ||
'style', | ||
'attributes', | ||
'nodeValue' | ||
]; | ||
is.domElement = x => is.object(x) && x.nodeType === NODE_TYPE_ELEMENT && is.string(x.nodeName) && | ||
!is.plainObject(x) && DOM_PROPERTIES_TO_CHECK.every(property => property in x); | ||
is.infinite = x => x === Infinity || x === -Infinity; | ||
const isEmptyStringOrArray = x => (is.string(x) || is.array(x)) && x.length === 0; | ||
const isEmptyObject = x => !is.map(x) && !is.set(x) && is.object(x) && Object.keys(x).length === 0; | ||
const isEmptyMapOrSet = x => (is.map(x) || is.set(x)) && x.size === 0; | ||
is.empty = x => !x || isEmptyStringOrArray(x) || isEmptyObject(x) || isEmptyMapOrSet(x); | ||
const predicateOnArray = (method, predicate, values) => { | ||
// `values` is the calling function's "arguments object". | ||
// We have to do it this way to keep node v4 support. | ||
// So here we convert it to an array and slice off the first item. | ||
values = Array.prototype.slice.call(values, 1); | ||
if (is.function(predicate) === false) { | ||
throw new TypeError(`Invalid predicate: ${util.inspect(predicate)}`); | ||
} | ||
if (values.length === 0) { | ||
throw new TypeError(`Invalid number of values`); | ||
} | ||
return method.call(values, predicate); | ||
}; | ||
// We have to use anonymous functions for the any() and all() methods | ||
// to get the arguments since we can't use rest parameters in node v4. | ||
is.any = function (predicate) { | ||
return predicateOnArray(Array.prototype.some, predicate, arguments); | ||
}; | ||
is.all = function (predicate) { | ||
return predicateOnArray(Array.prototype.every, predicate, arguments); | ||
}; | ||
module.exports = is; |
{ | ||
"name": "@sindresorhus/is", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Type check values: `is.string('🦄') //=> true`", | ||
@@ -47,4 +47,5 @@ "license": "MIT", | ||
"ava": "*", | ||
"jsdom": "^9.12.0", | ||
"xo": "*" | ||
} | ||
} |
@@ -148,3 +148,39 @@ # is [![Build Status](https://travis-ci.org/sindresorhus/is.svg?branch=master)](https://travis-ci.org/sindresorhus/is) | ||
##### .domElement(value) | ||
Returns `true` if `value` is a DOM Element. | ||
##### .infinite(value) | ||
Check if `value` is `Infinity` or `-Infinity`. | ||
##### .empty(value) | ||
Returns `true` if `value` is falsy or an empty string, array, object, map, or set. | ||
##### .any(predicate, ...values) | ||
Returns `true` if **any** of the input `values` returns true in the `predicate`: | ||
```js | ||
is.any(is.string, {}, true, '🦄'); | ||
//=> true | ||
is.any(is.boolean, 'unicorns', [], new Map()); | ||
//=> false | ||
``` | ||
##### .all(predicate, ...values) | ||
Returns `true` if **all** of the input `values` returns true in the `predicate`: | ||
```js | ||
is.all(is.object, {}, new Map(), new Set()); | ||
//=> true | ||
is.all(is.string, '🦄', [], 'unicorns'); | ||
//=> false | ||
``` | ||
## FAQ | ||
@@ -177,2 +213,3 @@ | ||
- [is-empty-iterable](https://github.com/sindresorhus/is-empty-iterable) - Check if an Iterable is empty | ||
- [is-blob](https://github.com/sindresorhus/is-blob) - Check if a value is a Blob - File-like object of immutable, raw data | ||
@@ -179,0 +216,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13080
163
224
3