diff --git a/.gitattributes b/.gitattributes index 391f0a4..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -* text=auto -*.js text eol=lf +* text=auto eol=lf diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b85fc2a..441975c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,14 +10,10 @@ jobs: fail-fast: false matrix: node-version: - - 14 - - 12 - - 10 - - 8 - - 6 + - 16 steps: - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 + - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/index.js b/index.js index 7623f21..40ddb67 100644 --- a/index.js +++ b/index.js @@ -1,24 +1,22 @@ -'use strict'; - -module.exports = iterable => { +export default function magicIterable(iterable) { return new Proxy(iterable, { get(target, property) { - return function (...args) { - const ret = []; + return function (...arguments_) { + const returnValue = []; - let i = 0; + let index = 0; for (const item of target) { - i++; + index++; if (typeof item[property] === 'undefined') { - throw new TypeError(`Item ${i} of the iterable is missing the ${property}() method`); + throw new TypeError(`Item ${index} of the iterable is missing the ${property}() method`); } - ret.push(Reflect.apply(item[property], item, args)); + returnValue.push(Reflect.apply(item[property], item, arguments_)); } - return ret; + return returnValue; }; - } + }, }); -}; +} diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (https://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: diff --git a/package.json b/package.json index 0f6f0da..03cf1b0 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,16 @@ "description": "Call a method on all items in an iterable by calling it on the iterable itself", "license": "MIT", "repository": "sindresorhus/magic-iterable", + "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" + "url": "https://sindresorhus.com" }, + "type": "module", + "exports": "./index.js", "engines": { - "node": ">=6" + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "scripts": { "test": "xo && ava" @@ -32,7 +35,7 @@ "magic" ], "devDependencies": { - "ava": "*", - "xo": "*" + "ava": "^3.15.0", + "xo": "^0.44.0" } } diff --git a/readme.md b/readme.md index e6d1365..63f8044 100644 --- a/readme.md +++ b/readme.md @@ -4,24 +4,22 @@ 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'); +import magicIterable from 'magic-iterable'; const x = { - i: 0, + index: 0, increment(value) { - this.i += value; - return this.i; + this.index += value; + return this.index; } }; @@ -34,12 +32,12 @@ Array.isArray(magicArray); magicArray.increment(2); //=> [2, 4, 6, 8]; -x.i; +x.index; //=> 8 ``` ```js -const magicIterable = require('magic-iterable'); +import magicIterable from 'magic-iterable'; // Subscribes to click events for all `` elements magicIterable(document.querySelectorAll('a')).addEventListener('click', () => { @@ -47,7 +45,6 @@ magicIterable(document.querySelectorAll('a')).addEventListener('click', () => { }); ``` - ## API ### magicIterable(iterable) @@ -60,14 +57,8 @@ Type: [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer 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 index 2edaeeb..1e78a8f 100644 --- a/test.js +++ b/test.js @@ -1,85 +1,83 @@ import test from 'ava'; -import m from '.'; +import magicIterable from './index.js'; test('main', t => { - let i = 0; - - const createFixture = () => { - return { - increment(value) { - i += value; - return i; - } - }; - }; + let index = 0; + + const createFixture = () => ({ + increment(value) { + index += value; + return index; + }, + }); const array = [ createFixture(), createFixture(), createFixture(), - createFixture() + createFixture(), ]; - const magicArray = m(array); + const magicArray = magicIterable(array); t.true(Array.isArray(magicArray)); t.deepEqual(magicArray.increment(2), [2, 4, 6, 8]); - t.is(i, 8); + t.is(index, 8); }); test('`this` works correctly', t => { const fixture = { - i: 0, + index: 0, increment(value) { - this.i += value; - return this.i; - } + this.index += value; + return this.index; + }, }; const array = [fixture, fixture, fixture, fixture]; - t.deepEqual(m(array).increment(2), [2, 4, 6, 8]); - t.is(fixture.i, 8); + t.deepEqual(magicIterable(array).increment(2), [2, 4, 6, 8]); + t.is(fixture.index, 8); }); test('does not work on heterogeneous iterable', t => { - const createFixture = () => { - return { - foo() {} - }; - }; + const createFixture = () => ({ + foo() {}, + }); const array = [ createFixture(), createFixture(), {}, - createFixture() + createFixture(), ]; - const magicArray = m(array); + const magicArray = magicIterable(array); t.throws(() => { magicArray.foo(); - }, /Item 3 of the iterable is missing the foo\(\) method/); + }, { + message: /Item 3 of the iterable is missing the foo\(\) method/, + }); }); test('should work on array of non-objects', t => { - t.deepEqual(m(['a', 'b']).includes('b'), [false, true]); + t.deepEqual(magicIterable(['a', 'b']).includes('b'), [false, true]); }); test('should only apply to the items of the iterable', t => { const fixture = { foo() { return '🦄'; - } + }, }; const array = [fixture, fixture]; array.foo = () => '🤡'; - t.deepEqual(m(array).foo(), ['🦄', '🦄']); + t.deepEqual(magicIterable(array).foo(), ['🦄', '🦄']); }); test.failing('should support properties, not just methods', t => { - t.deepEqual(m(['a', 'ab', 'abc']).length.toString(), ['1', '2', '3']); + t.deepEqual(magicIterable(['a', 'ab', 'abc']).length.toString(), ['1', '2', '3']); });