npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lopatnov/make-iterable

v1.3.3

Published

How to make object as array? This TypeScript library makes objects as Array like and iterable.

Downloads

46

Readme

make-iterable Twitter

npm NPM version License GitHub issues GitHub forks GitHub stars GitHub top language

Patreon sobe.ru LinkedIn

Build Status Libraries.io dependency status for latest release

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://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";

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

Make an Object Iterable and Array-Like

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

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

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

Rights and Agreements

License Apache-2.0

Copyright 2019–2021 Oleksandr Lopatnov