diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..1c6314a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +indent_style = tab +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.yml] +indent_style = space +indent_size = 2 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..391f0a4 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +* text=auto +*.js text eol=lf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..239ecff --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +yarn.lock diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..e0cc348 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: node_js +node_js: + - '8' + - '6' diff --git a/index.js b/index.js new file mode 100644 index 0000000..f7b874f --- /dev/null +++ b/index.js @@ -0,0 +1,28 @@ +'use strict'; + +module.exports = iterable => { + return new Proxy(iterable, { + get(target, property, receiver) { + if (property in target) { + return Reflect.get(target, property, receiver); + } + + return function (...args) { + const ret = []; + + let i = 0; + for (const item of target) { + i++; + + if (!Reflect.has(item, property)) { + throw new TypeError(`Item ${i} of the iterable is missing the ${property}() method`); + } + + ret.push(Reflect.apply(item[property], item, args)); + } + + return ret; + }; + } + }); +}; diff --git a/license b/license new file mode 100644 index 0000000..e7af2f7 --- /dev/null +++ b/license @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) Sindre Sorhus (sindresorhus.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/package.json b/package.json new file mode 100644 index 0000000..6224dbe --- /dev/null +++ b/package.json @@ -0,0 +1,38 @@ +{ + "name": "magic-iterable", + "version": "0.0.0", + "description": "Call a method on all items in an iterable", + "license": "MIT", + "repository": "sindresorhus/magic-iterable", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "engines": { + "node": ">=6" + }, + "scripts": { + "test": "xo && ava" + }, + "files": [ + "index.js" + ], + "keywords": [ + "iterable", + "iterator", + "array", + "method", + "function", + "call", + "apply", + "proxy", + "proxies", + "es2015", + "magic" + ], + "devDependencies": { + "ava": "*", + "xo": "*" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..d5f821b --- /dev/null +++ b/readme.md @@ -0,0 +1,73 @@ +# magic-iterable [![Build Status](https://travis-ci.org/sindresorhus/magic-iterable.svg?branch=master)](https://travis-ci.org/sindresorhus/magic-iterable) + +> Call a method on all items in an iterable + +Uses the [`Proxy` API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy). + + +## Install + +``` +$ npm install magic-iterable +``` + + +## Usage + +```js +const magicIterable = require('magic-iterable'); + +const x = { + i: 0, + increment(value) { + this.i += value; + return this.i; + } +}; + +const array = [x, x, x, x]; +const magicArray = magicIterable(array); + +Array.isArray(magicArray); +//=> true + +magicArray.increment(2); +//=> [2, 4, 6, 8]; + +x.i; +//=> 8 +``` + +```js +const magicIterable = require('magic-iterable'); + +// Subscribes to click events for all `` elements +magicIterable(document.querySelectorAll('a')).addEventListener('click', () => { + console.log('Click'); +}); +``` + + +## API + +### magicIterable(iterable) + +Returns a version of `iterable` that when you call a method on it that doesn't already exist, it will call that method on all items in the iterable and return an array with the result. + +#### iterable + +Type: [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol) *(For example, an `Array`)* + +Iterable where all the items has the method you want to call. + + +## Related + +- [on-change](https://github.com/sindresorhus/on-change) - Watch an object or array for changes (Uses `Proxy` too) +- [negative-array](https://github.com/sindresorhus/negative-array) - Negative array index support (Uses `Proxy` too) +- [known](https://github.com/sindresorhus/known) - Allow only access to known object properties (Uses `Proxy` too) + + +## License + +MIT © [Sindre Sorhus](https://sindresorhus.com) diff --git a/test.js b/test.js new file mode 100644 index 0000000..97c7afd --- /dev/null +++ b/test.js @@ -0,0 +1,64 @@ +import test from 'ava'; +import m from '.'; + +test('main', t => { + let i = 0; + + const createFixture = () => { + return { + increment(value) { + i += value; + return i; + } + }; + }; + + const array = [ + createFixture(), + createFixture(), + createFixture(), + createFixture() + ]; + + const magicArray = m(array); + + t.true(Array.isArray(magicArray)); + t.deepEqual(magicArray.increment(2), [2, 4, 6, 8]); + t.is(i, 8); +}); + +test('`this` works correctly', t => { + const fixture = { + i: 0, + increment(value) { + this.i += value; + return this.i; + } + }; + + const array = [fixture, fixture, fixture, fixture]; + + t.deepEqual(m(array).increment(2), [2, 4, 6, 8]); + t.is(fixture.i, 8); +}); + +test('does not work on heterogeneous iterable', t => { + const createFixture = () => { + return { + foo() {} + }; + }; + + const array = [ + createFixture(), + createFixture(), + {}, + createFixture() + ]; + + const magicArray = m(array); + + t.throws(() => { + magicArray.foo(); + }, /Item 3 of the iterable is missing the foo\(\) method/); +});