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

iterate-anything

v1.0.6

Published

Iterate anything with a same interface.

Downloads

3

Readme

iterate-anything

Iterate anything with a same interface.

GitHub Build Status Coverage Status

Installation

npm install iterate-anything

Usage

import { itany } from 'iterate-anything'

for (let [value] of itany(1)) {
    console.log(value)     // prints 1
}

for (let [value] of itany([1, 2, 3])) {
    console.log(value)     // prints 1, 2, 3
}

for (let [value, key] of itany({a: 1, b: 2, c: 3})) {
    console.log(`${key}:${value}`)    // prints a:1 b:2 c:3
}

Item Parameters

In each iteration, there are 4 pieces of data passed back as an array.

[value, key, passedValue, Type]
import { itany } from 'iterate-anything';

for (let [value, key, passedValue, type] of itany({a: 1, b: 2, c: 3})) {
    console.log(value, key, passedValue, type)
}

This will print below:

1 a undefined 4
2 b undefined 4
3 c undefined 4
  • First index (value) is the final value of iterable.

  • Second index (key) is the key, index or unique value. Beware that although itany pass you an incremental keys for forexample Sets, this doesn't mean Sets have order. This order theorically can be changed next time you call itany and iterate over it. But I found it handy to have a unique key always passed back.

  • Third index (passedValue) is whatever method next() is being called with. Here since we're using for (... of ...) this value is undefined, but if you get the native iterator using const iterator = itany(target)[Symbol.iterator](), when calling the next method with a parameter, passedValue would be that parameter.

  • Fourth index (type) is itany type. For example here 4 means LISTS. If you're using TypeScript this is TargetType.LIST.

Targets

Array

Array targets in itany are considers Lists.

The key for each item when iterator over arrays is an index of that item.

Type for Array is TargetType.LIST.

for (let [value, index] of itany([10, 11, 12])) {
    console.log(value, index)
}

/* Prints:
    10 0
    11 1
    12 2
*/

String

String targets in itany are considers Lists.

The key for each item when iterator over a String is an index of the character.

Type for String is TargetType.LIST.

for (let [value, index] of itany("Sam")) {
    console.log(value, index)
}

/* Prints:
    S 0
    a 1
    m 2
*/

Object

Object targets in itany are considers Maps.

The key for each item when iterator over objects is the property name on that object and value is the value for that property.

Keep in mind that, everytime you iterate over an object, the order of values showing up can theorically changes.

Type for Object is TargetType.MAP.

for (let [value, key] of itany({a: 1, b: 2, c: 3})) {
    console.log(value, key)
}

/* Prints:
    1 a
    2 b
    3 c
*/

Map

The key for each item when iterator over Map is the key on that Map and value is the value for that key.

Keep in mind that, everytime you iterate over a Map, the order of items showing up can theorically changes.

Type for Map is TargetType.MAP.

const map = new Map();
map.set('a', 10)
map.set('b', 11)

for (let [value, key] of itany(map)) {
    console.log(value, key)
}

/* Prints:
    10 a
    11 b
*/

Set

Keys when iterating Sets are unique incremental indexes start from 0, but this is only for convenience since Set items don't have any order and this should'nt be confused with indexes.

Keep in mind that, everytime you iterate over a Set, the order of items showing up can theorically changes.

Type for Set is TargetType.SET.

const map = new Set();
map.add(10)
map.add(11)

for (let [value] of itany(map)) {
    console.log(value)
}

/* Prints:
    10
    11
*/

Native Iterators

Keys when iterating over JavaScript native Itarators are unique incremental indexes start from 0 in order of the values show up.

Type for Set is TargetType.ITERATOR.

const map = new Map();
map.set('a', 10)
map.set('b', 11)
const iterator = map.values()

for (let [value, key] of itany(iterator)) {
    console.log(value, key)
}

/* Prints:
    10 0
    11 1
*/

null, undefined, NaN, number, bigint, boolean, Regex, WeakMap, WeakSet, Promise and Function

These are targets which itany considers Scalar. These are: null, undefined, NaN, number, bigint, boolean, Regex, WeakMap, WeakSet, Promise and Function.

These targets will always have one value when iterate over and the key for that item would be 0.

Type for Scalars is TargetType.SCALAR.

for (let [value] of itany(12)) {
    console.log(value)
}

/* Prints:
    12
*/

Item Types

import { TargetType } from 'iterate-anything';

TargetType.SET;         // Set of values with no order: Set
TargetType.MAP;         // Set of key/value pairs with no order: Map, Object
TargetType.LIST;        // List of values with order: Array, String
TargetType.SCALAR;      // Single Value: number, boolean, function, null, undefined, NaN, Promise, WeakMap, WeakSet, Promise
TargetType.ITERATOR;    // Native Iterator

TypeScript

If you are using iterate-anything with TypeScript, you need to add --downlevelIteration switch to tsc.

tsc index.tsc --downlevelIteration

Get native Iterator

import { itany } from 'iterate-anything'

const iterator = itany([1, 2, 3])[Symbol.iterator]()

const { value: [value1], done: done1 } = iterator.next()   // value: 1         done: false 
const { value: [value2], done: done2 } = iterator.next()   // value: 2         done: false
const { value: [value3], done: done3 } = iterator.next()   // value: 3         done: false
const { value: [value4], done: done4 } = iterator.next()   // value: undefined  done: true