# magic-iterable

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

Latest version **1.0.0** (published 2021-08-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install magic-iterable
pnpm add magic-iterable
yarn add magic-iterable
bun add magic-iterable
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2021-08-10 |
| First published | 2018-01-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Node | ^12.20.0 \|\| ^14.13.1 \|\| >=16.0.0 |
| Dependencies | 0 |
| Unpacked size | 3.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 162 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | iterable, iterator, array, method, function, call, apply, proxy, proxies, es2015, magic |

## Links

- npm: https://www.npmjs.com/package/magic-iterable
- Repository: https://github.com/sindresorhus/magic-iterable
- Homepage: https://github.com/sindresorhus/magic-iterable#readme
- Issues: https://github.com/sindresorhus/magic-iterable/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/magic-iterable

## Recent versions

- 1.0.0 (latest) — 2021-08-10
- 0.2.0 — 2018-01-14
- 0.1.0 — 2018-01-13

## README

# magic-iterable

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

Uses the [`Proxy` API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy).

## Install

```
$ npm install magic-iterable
```

## Usage

```js
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
```

```js
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`](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)

---
_Source: https://npm.io/package/magic-iterable · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
