GithubHelp home page GithubHelp logo

sindresorhus / magic-iterable Goto Github PK

View Code? Open in Web Editor NEW
163.0 6.0 8.0 8 KB

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

License: MIT License

JavaScript 100.00%
iterable es2015-proxy nodejs npm-package

magic-iterable's Introduction

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.

Related

  • 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.