check-iterable
Advanced tools
Comparing version 1.0.2 to 1.0.4
87
index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.isIterable = isIterable; | ||
exports.isAsyncIterable = isAsyncIterable; | ||
exports.isIteratorLike = isIteratorLike; | ||
exports.isIterableIterator = isIterableIterator; | ||
exports.isAsyncIterableIterator = isAsyncIterableIterator; | ||
exports.isGenerator = isGenerator; | ||
exports.isAsyncGenerator = isAsyncGenerator; | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
if (!Symbol.asyncIterator) { | ||
Symbol.asyncIterator = Symbol("Symbol.asyncIterator"); | ||
Symbol.asyncIterator = Symbol("Symbol.asyncIterator"); | ||
} | ||
/** | ||
* Checks if the given object is an Iterable (implemented `@@iterator`). | ||
*/ | ||
exports.isIterable = isIterable; | ||
function isIterable(obj) { | ||
return obj !== null | ||
&& obj !== undefined | ||
&& typeof obj[Symbol.iterator] === "function"; | ||
return obj !== null && obj !== undefined && typeof obj[Symbol.iterator] === "function"; | ||
} | ||
exports.isAsyncIterable = isAsyncIterable; | ||
/** | ||
* Checks if the given object is an AsyncIterable (implemented `@@asyncIterator`). | ||
*/ | ||
function isAsyncIterable(obj) { | ||
return obj !== null | ||
&& obj !== undefined | ||
&& typeof obj[Symbol.asyncIterator] === "function"; | ||
return obj !== null && obj !== undefined && typeof obj[Symbol.asyncIterator] === "function"; | ||
} | ||
/** | ||
* Checks if the given object is an IteratorLike (implemented `next`). | ||
*/ | ||
exports.isIteratorLike = isIteratorLike; | ||
function isIteratorLike(obj) { | ||
// An iterable object has a 'next' method, however including a 'next' method | ||
// doesn't ensure the object is an iterator, it is only iterator-like. | ||
return typeof obj === "object" | ||
&& obj !== null | ||
&& typeof obj.next === "function"; | ||
// An iterable object has a 'next' method, however including a 'next' method | ||
// doesn't ensure the object is an iterator, it is only iterator-like. | ||
return _typeof(obj) === "object" && obj !== null && typeof obj.next === "function"; | ||
} | ||
/** | ||
* Checks if the given object is an IterableIterator (implemented both | ||
* `@@iterator` and `next`). | ||
*/ | ||
exports.isIterableIterator = isIterableIterator; | ||
function isIterableIterator(obj) { | ||
return isIteratorLike(obj) | ||
&& typeof obj[Symbol.iterator] === "function"; | ||
return isIteratorLike(obj) && typeof obj[Symbol.iterator] === "function"; | ||
} | ||
/** | ||
* Checks if the given object is an AsyncIterableIterator (implemented | ||
* both `@@asyncIterator` and `next`). | ||
*/ | ||
exports.isAsyncIterableIterator = isAsyncIterableIterator; | ||
function isAsyncIterableIterator(obj) { | ||
return isIteratorLike(obj) | ||
&& typeof obj[Symbol.asyncIterator] === "function"; | ||
return isIteratorLike(obj) && typeof obj[Symbol.asyncIterator] === "function"; | ||
} | ||
/** | ||
* Checks if the given object is a Generator. | ||
*/ | ||
exports.isGenerator = isGenerator; | ||
function isGenerator(obj) { | ||
return isIterableIterator(obj) | ||
&& hasGeneratorSpecials(obj); | ||
return isIterableIterator(obj) && hasGeneratorSpecials(obj); | ||
} | ||
/** | ||
* Checks if the given object is an AsyncGenerator. | ||
*/ | ||
exports.isAsyncGenerator = isAsyncGenerator; | ||
function isAsyncGenerator(obj) { | ||
return isAsyncIterableIterator(obj) | ||
&& hasGeneratorSpecials(obj); | ||
return isAsyncIterableIterator(obj) && hasGeneratorSpecials(obj); | ||
} | ||
function hasGeneratorSpecials(obj) { | ||
return typeof obj.return === "function" | ||
&& typeof obj.throw === "function"; | ||
} | ||
return typeof obj["return"] === "function" && typeof obj["throw"] === "function"; | ||
} |
{ | ||
"name": "check-iterable", | ||
"version": "1.0.2", | ||
"version": "1.0.4", | ||
"description": "A toolbox to check if an object is iterable, an iterator or a generator, etc.", | ||
@@ -8,3 +8,7 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "tsc -p test & mocha --exit" | ||
"prepublishOnly": "npm link @babel/core && mv index.js index.mjs && babel --presets @babel/env -o index.js index.mjs", | ||
"postpublish": "mv index.mjs index.js", | ||
"pretest": "tsc test/index.ts --module commonjs --target es2015 && npm run prepublishOnly", | ||
"posttest": "npm run postpublish && rm test/index.js", | ||
"test": "mocha" | ||
}, | ||
@@ -24,3 +28,3 @@ "repository": { | ||
], | ||
"author": "Ayon Lee <i@hyurl.com>", | ||
"author": "A-yon Lee <i@hyurl.com>", | ||
"license": "MIT", | ||
@@ -32,8 +36,7 @@ "bugs": { | ||
"devDependencies": { | ||
"@types/mocha": "^5.2.6", | ||
"@types/node": "^11.13.6", | ||
"mocha": "^6.1.4", | ||
"tslib": "^1.9.3", | ||
"typescript": "^3.4.4" | ||
"@babel/preset-env": "^7.12.7", | ||
"@types/mocha": "^5.2.7", | ||
"@types/node": "^11.13.17", | ||
"mocha": "^6.1.4" | ||
} | ||
} |
@@ -5,2 +5,16 @@ # Check Iterable | ||
## Install | ||
### Node.js | ||
```sh | ||
npm i check-iterable | ||
``` | ||
### Deno | ||
```ts | ||
import * as CheckIterable from "https://deno.land/x/check_iterable/index.js"; | ||
``` | ||
## API | ||
@@ -26,2 +40,2 @@ | ||
suggested on [MDN](https://developer.mozilla.org) in sloppy mode, go to MDN for | ||
more details. | ||
more details. |
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
7062
4
94
40