Magic Iterable Save

Call a method on all items in an iterable by calling it on the iterable itself

Project README

magic-iterable

Call a method on all items in an iterable by calling it on the iterable itself

Uses the Proxy API.

Install

$ npm install magic-iterable

Usage

import magicIterable from 'magic-iterable';

const x = {
	index: 0,
	increment(value) {
		this.index += value;
		return this.index;
	}
};

const array = [x, x, x, x];
const magicArray = magicIterable(array);

Array.isArray(magicArray);
//=> true

magicArray.increment(2);
//=> [2, 4, 6, 8];

x.index;
//=> 8
import magicIterable from 'magic-iterable';

// Subscribes to click events for all `<a>` 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, it will call that method on all items in the iterable and return an array with the result.

iterable

Type: Iterable (For example, an Array)

Iterable where all the items has the method you want to call.

  • on-change - Watch an object or array for changes (Uses Proxy too)
  • negative-array - Negative array index support (Uses Proxy too)
  • known - Allow only access to known object properties (Uses Proxy too)
Open Source Agenda is not affiliated with "Magic Iterable" Project. README Source: sindresorhus/magic-iterable
Stars
163
Open Issues
0
Last Commit
2 years ago
License
MIT

Open Source Agenda Badge

Open Source Agenda Rating