Coder Social home page Coder Social logo

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)

magic-iterable's People

Contributors

richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

magic-iterable's Issues

It doesn't work on array of non-objects

Example

const magicIterable = require('magic-iterable');

const array = ['ax', 'b', 'xx', 'x'];
const magicArray = magicIterable(array);
console.log(magicArray.match('x'))

Expected

[ 
  [ 'x', index: 1, input: 'ax' ], // String#match's output
  null,
  [ 'x', index: 0, input: 'xx' ],
  [ 'x', index: 0, input: 'x' ]
]

Currently

magic-iterable/index.js:17
					if (!Reflect.has(item, property)) {
					             ^

TypeError: Reflect.has called on non-object

It only supports methods

Example

const magicIterable = require('magic-iterable');

const magicElements = magicIterable(document.querySelectorAll('a'));
console.log(magicElements.textContent);
console.log(magicElements.classList.add('x'));

Expected

[ 'Home', 'Contacts', 'Click Here', 'Close' ]
[ undefined, undefined, undefined, undefined ]

Currently

function // this is the function you return at line 10
Uncaught TypeError: magicElements.classList.add is not a function

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.