├── .editorconfig ├── .gitattributes ├── .gitignore ├── .jshintrc ├── .travis.yml ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{package.json,*.yml}] 11 | indent_style = space 12 | indent_size = 2 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "esnext": true, 4 | "bitwise": true, 5 | "curly": true, 6 | "immed": true, 7 | "newcap": true, 8 | "noarg": true, 9 | "undef": true, 10 | "unused": "vars", 11 | "strict": true 12 | } 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - 4 5 | - 6 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | module.exports = function (val) { 3 | return (typeof Symbol !== 'undefined' && Symbol && 'iterator' in Symbol 4 | && val != null && typeof val[Symbol.iterator] === 'function'); 5 | }; 6 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Hemanth.HM (h3manth.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-iterable", 3 | "version": "1.1.1", 4 | "description": "Checks if a given object is iterable", 5 | "license": "MIT", 6 | "repository": "hemanth/is-iterable", 7 | "author": { 8 | "name": "Hemanth.HM", 9 | "email": "hemanth.hm@gmail.com", 10 | "url": "h3manth.com" 11 | }, 12 | "engines": { 13 | "node": ">= 4" 14 | }, 15 | "scripts": { 16 | "test": "mocha" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "es6", 23 | "check", 24 | "iterable" 25 | ], 26 | "dependencies": {}, 27 | "devDependencies": { 28 | "mocha": "*" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # is-iterable [![Build Status](https://travis-ci.org/hemanth/is-iterable.svg?branch=master)](https://travis-ci.org/hemanth/is-iterable) 2 | 3 | > Checks if a given object is iterable. 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install --save is-iterable 10 | ``` 11 | 12 | ## Usage 13 | 14 | ```js 15 | var isIterable = require('is-iterable'); 16 | 17 | isIterable([]); // true 18 | isIterable({}); // false 19 | ``` 20 | 21 | ## License 22 | 23 | MIT © [Hemanth.HM](http://h3manth.com) 24 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var assert = require('assert'); 3 | var isIterable = require('./'); 4 | 5 | it('should detect non-iterable values', function () { 6 | assert.strictEqual(isIterable({}), false); 7 | assert.strictEqual(isIterable(new Object()), false); 8 | 9 | assert.strictEqual(isIterable(true), false); 10 | assert.strictEqual(isIterable(new Boolean(true)), false); 11 | 12 | assert.strictEqual(isIterable(1), false); 13 | assert.strictEqual(isIterable(new Number(1)), false); 14 | assert.strictEqual(isIterable(Infinity), false); 15 | 16 | assert.strictEqual(isIterable(function(){}), false); 17 | assert.strictEqual(isIterable(new Function('')), false); 18 | 19 | assert.strictEqual(isIterable(new RegExp('/./i')), false); 20 | assert.strictEqual(isIterable(/./i), false); 21 | 22 | assert.strictEqual(isIterable(undefined), false); 23 | assert.strictEqual(isIterable(null), false); 24 | assert.strictEqual(isIterable(NaN), false); 25 | 26 | assert.strictEqual(isIterable(new WeakSet()), false); 27 | assert.strictEqual(isIterable(new WeakMap()), false); 28 | }); 29 | 30 | it('should detect iterable values', function() { 31 | assert.strictEqual(isIterable(new String('foo')), true); 32 | assert.strictEqual(isIterable('foo'), true); 33 | 34 | assert.strictEqual(isIterable([1,2,3]), true); 35 | assert.strictEqual(isIterable([1,2,3][Symbol.iterator]()), true); 36 | assert.strictEqual(isIterable(new Array([1,2,3])), true); 37 | 38 | assert.strictEqual(isIterable(new Map()), true); 39 | assert.strictEqual(isIterable(new Set()), true); 40 | }); 41 | 42 | it('should return false if Symbol is not defined', function () { 43 | var oldSymbol = Symbol; 44 | 45 | try { 46 | delete global.Symbol; 47 | 48 | assert.strictEqual(isIterable({}), false); 49 | assert.strictEqual(isIterable(new Object()), false); 50 | 51 | assert.strictEqual(isIterable([1,2,3]), false); 52 | assert.strictEqual(isIterable(new Array([1,2,3])), false); 53 | } finally { 54 | global.Symbol = oldSymbol; 55 | } 56 | }); 57 | --------------------------------------------------------------------------------