@lopatnov/make-iterable
Version:
How to make object as array? This TypeScript library makes objects as Array like and iterable.
127 lines (93 loc) • 5.72 kB
Markdown
# make-iterable [](https://twitter.com/intent/tweet?text=Wow:&url=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F@lopatnov/make-iterable)
[](https://www.npmjs.com/package/@lopatnov/make-iterable)
[](https://www.npmjs.com/package/@lopatnov/make-iterable)
[](https://github.com/lopatnov/make-iterable/blob/master/LICENSE)
[](https://github.com/lopatnov/make-iterable/issues)
[](https://github.com/lopatnov/make-iterable/network)
[](https://github.com/lopatnov/make-iterable/stargazers)

[](https://www.patreon.com/lopatnov)
[](https://sobe.ru/na/tech_knigi)
[](https://www.linkedin.com/in/lopatnov/)
[](https://travis-ci.org/lopatnov/make-iterable)
[](https://www.npmjs.com/package/@lopatnov/make-iterable?activeTab=dependencies)
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
[](https://www.npmjs.com/package/@lopatnov/make-iterable)
```shell
npm install @lopatnov/make-iterable
```
[Browser](//lopatnov.github.io/make-iterable/dist/make-iterable.js)
```html
<script src="//lopatnov.github.io/make-iterable/dist/make-iterable.min.js"></script>
```
## Import package to the project
### TypeScript
```typescript
import makeIterable from "@lopatnov/make-iterable";
```
### JavaScript
```javascript
var makeIterable = require("@lopatnov/make-iterable");
```
## Usage of `makeIterable<T>(value: T): T & any[]`
Apply `makeIterable` method to an object, function or it's prototype and use [Array methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
### Make an Object Iterable and Array-Like
```typescript
let x = {
hello: "world"
};
let iterableX = makeIterable(x); // <-- now object iterableX has hello property and Array properties
iterableX.push(10); // [10]
iterableX.push(20); // [10, 20]
iterableX.push(30); // [10, 20, 30]
iterableX.push(40); // [10, 20, 30, 40]
iterableX.pop(); // [10, 20, 30]
for (const index in x) {
/*
"Index [0] = 10"
"Index [1] = 20"
"Index [2] = 30"
"Index [hello] = world"
*/
console.log(`Index [${index}] = ${x[index]}`);
}
for (const value of x) {
/*
10
20
30
*/
console.log(value)
}
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"
```
### Interaction with function prototype
```javascript
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) {
/*
"z[0]=1,2"
"z[1]=3,4"
"z[2]=5,6"
"z[message]=Length is not enumerable now"
*/
console.log(`z[${index}]=${z[index]}`)
}
```
## Demo
See, how it's working: [https://runkit.com/lopatnov/make-iterable-demo-1.3.1](https://runkit.com/lopatnov/make-iterable-demo-1.3.1)
Test it with a runkit: [https://npm.runkit.com/@lopatnov/make-iterable](https://npm.runkit.com/@lopatnov/make-iterable)
## Rights and Agreements
License [Apache-2.0](https://github.com/lopatnov/make-iterable/blob/master/LICENSE)
Copyright 2019–2021 Oleksandr Lopatnov