lopatnov/make-iterable


This TypeScript library makes objects as Array like and iterable

https://lopatnov.github.io/make-iterable/

License: Apache-2.0

Language: JavaScript

Keywords: array-like, iterable, javascript, typescript


make-iterable

NPM version License Build Status Twitter

This TypeScript library makes objects as Array like and iterable.

Install

https://nodei.co/npm/@lopatnov/make-iterable.png?downloads=true&downloadRank=true&stars=true

npm install @lopatnov/make-iterable

Browser

<script src="//lopatnov.github.io/make-iterable/dist/make-iterable.min.js"></script>

Import package to the project

TypeScript:

import makeIterable from "@lopatnov/make-iterable";

or JavaScript:

var makeIterable = require("@lopatnov/make-iterable");

Make Objects Iterable and Array-Like

makeIterable<T>(value: T): T | any[]

let x = {
  hello: "world"
};
let iterableX = makeIterable(x);

iterableX.push(10);
iterableX.push(20);
iterableX.push(30);
iterableX.push(40);
iterableX.pop();

console.log(`x === iterableX ? ${x === iterableX}`); // true
console.log(`indexOf(30) = ${iterableX.indexOf(30)}`); // 2
console.log(`[...iterableX] = ${[...iterableX]}`); // [10,20,30]
console.log(`iterableX.hello = ${iterableX.hello}`); // "world"

Before 1.1.0 version it works with Function prototype like with array-like object with Function prototype context. From 1.1.0 version it works with Function prototype to make array-like object:

class Sample {
  static count = 0;
  message: string;

  constructor(message: string) {
    Sample.count++;
    this.message = message;
  }
}

makeIterable(Sample.prototype);

let q = new Sample("Hello world") as Sample & any[]; // makes array-like object
let t = new Sample("It working!") as Sample & any[]; // makes array-like object
q.push(true, false, true, true, false, true, false, true);
t.push("hello", "world", "!");

console.log(Sample.count); // 2
console.log([...q]); // true, false, true, true, false, true, false, true
console.log([...t]); // "hello", "world", "!"
console.log(q.message); // Hello world
console.log(t.message); // It working!

It means that length property become enumerable in new objects.

for (var index in t) {
 console.log(`t[${index}]=${t[index]}`)
}

/*
"t[0]=hello"
"t[1]=world"
"t[2]=!"
"t[message]=It working!"
"t[length]=3"
*/

To avoid this, use Object.defineProperty(this, "length", { value: 0, writable: true, enumerable: false, configurable: false });

class Simple {
  constructor(message) {
    this.message = message;
    Object.defineProperty(this, "length", {
      value: 0,
      writable: true,
      enumerable: false,
      configurable: false
    });
  }
}

makeIterable(Simple.prototype);

let z = new Simple('Length is not enumerable now');
z.push([1,2], [3,4], [5,6]);

for (var index in z) {
 console.log(`z[${index}]=${z[index]}`)
}

/*
"z[0]=1,2"
"z[1]=3,4"
"z[2]=5,6"
"z[message]=Length is not enumerable now"
*/

Demo

See, how it's working: https://runkit.com/lopatnov/make-iterable-demo-1.2.0

Test it with a runkit: https://npm.runkit.com/@lopatnov/make-iterable

Rights and Agreements

License Apache-2.0

Copyright 2019 Oleksandr Lopatnov

Project Statistics

Sourcerank 3
Repository Size 120 KB
Stars 1
Forks 0
Watchers 1
Open issues 0
Dependencies 445
Contributors 1
Tags 4
Created
Last updated
Last pushed

Top Contributors See all

Oleksandr

Packages Referencing this Repo

@lopatnov/make-iterable
This TypeScript library makes objects as Array like and iterable.
Latest release 1.3.0 - Updated - 1 stars

Recent Tags See all

1.3.0 December 22, 2019
1.2.0 December 03, 2019
1.1.0 November 23, 2019
1.0.0 November 22, 2019

Something wrong with this page? Make a suggestion

Last synced: 2019-12-22 23:49:47 UTC

Login to resync this repository