Releases: lopatnov/make-iterable
@lopatnov/make-iterable
make-iterable
How to make object as array? This TypeScript library makes objects as Array like and iterable. This change allows to iterate objects, functions and their prototypes. Making prototype iterable by this library allows to create iterable objects from classes.
Install
npm install @lopatnov/make-iterable
<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";
JavaScript
var makeIterable = require("@lopatnov/make-iterable");
Make Objects Iterable and Array-Like
TypeScript usage of makeIterable<T>(value: T): T & any[]
. Interaction with an object
let x = {
hello: "world"
};
let iterableX = makeIterable(x); // <-- now object iterableX has hello property and Array properties
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"
JavaScript usage of makeIterable. Interaction with function prototype.
class Simple {
constructor(message) {
this.message = message;
}
}
makeIterable(Simple.prototype); // <-- now Simple.prototype has Array properties
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.3.1
Test it with a runkit: https://npm.runkit.com/@lopatnov/make-iterable
Rights and Agreements
License Apache-2.0
Copyright 2019–2020 Oleksandr Lopatnov
@lopatnov/make-iterable
make-iterable
How to make object as array? This TypeScript library makes objects as Array like and iterable. This change allows to iterate objects, functions and their prototypes. Making prototype iterable by this library allows to create iterable objects from classes.
Install
npm install @lopatnov/make-iterable
<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-2020 Oleksandr Lopatnov
@lopatnov/make-iterable
Fixes for IE 9 support
@lopatnov/make-iterable
make-iterable
This TypeScript library makes objects as Array like and iterable.
Install
Node:
npm install @lopatnov/make-iterable
<script src="//lopatnov.github.io/make-iterable/dist/make-iterable.min.js"></script>
Polyfills
- Symbol: https://github.com/rousan/symbol-es6
- IE polyfills: https://github.com/ben-yip/ie7js
Import package to the project
TypeScript:
import makeIterable from "@lopatnov/make-iterable";
or JavaScript:
var makeIterable = require("@lopatnov/make-iterable");
Make Iterable
makeIterable(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"
From v.1.1.0 it works with Function prototype like:
class Sample {
static count = 0;
message: string;
constructor(message: string) {
Sample.count++;
this.message = message;
}
}
makeIterable(Sample.prototype);
let x = new Sample("Hello world") as Sample & any[];
let y = new Sample("It working!") as Sample & any[];
x.push(true, false, true, true, false, true, false, true);
y.push("hello", "world", "!");
console.log(Sample.count); // 2
console.log([...x]); // true, false, true, true, false, true, false, true
console.log([...y]); // "hello", "world", "!"
console.log(x.message); // Hello world
console.log(y.message); // It working!
Demo
See, how it's working: https://runkit.com/lopatnov/make-iterable-1.1.0-demo
Test it with a runkit: https://npm.runkit.com/@lopatnov/make-iterable
Rights and Agreements
License Apache-2.0
Copyright 2019 Oleksandr Lopatnov
make-iterable version 1.1.0
Update node-package-ci.yml
make-iterable version 1.0.0
This TypeScript library makes objects as Array like and iterable