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

iterarray

v0.4.0

Published

iterable indexable

Downloads

16

Readme

IterArray

travis ci npm version Coverage Status Dependency Status

IterArray is an iterable adapter that, given an iterable/generator or iterator, creates a new indexable lazy iterable that provides nth and slice methods.

Performance of IterArray instances are optimized for Array and primitive string iterables.

Usage.

const iterable = IterArray(new Set([5, 2, 8, 3, 4, 0, 9]))

iterable.nth(2) // 8
const slicedIterable = iterable.slice(1, 4) // IterArray([2, 8, 3])
slicedIterable.nth(2) // 3
;[...slicedIterable] // [2, 8, 3]

for (const x of iterable) {
    console.log(x) // log 5 2 8 3 4 0 9
}

for (const x of slicedIterable) {
    console.log(x) // log 2 8 3
}

API

constructor (iterable)

It creates a IterArray instance that iterates over the same values as iterable parameter or if iterable is a function, then it iterates over the same values as iterable(). It throws an exception if it is not passed an iterable, iterator or function that returns an iterable or an iterator.

Example:

const IterArray = require('iterarray')

function* gen () {
  yield 1
  yield 2
  yield 4
  yield 8
}

const iterableByArray = IterArray([1, 2, 3])
;[...iterableByArray] // [1, 2, 3]
const iterableBySet = IterArray(new Set([4, 5, 6]))
;[...iterableBySet] // [4, 5, 6]
const iterableByIterArray = IterArray(iterableByArray)
;[...iterableByIterArray] // [1, 2, 3]
const iterableByGenerator = IterArray(gen)
;[...iterableByGenerator] // [1, 2, 4, 8]
const iterableByIterator = IterArray(gen())
;[...iterableByIterator] // [1, 2, 4, 8]
const iterableByFunction = IterArray(() => iterableBySet)
;[...iterableByFunction] // [4, 5, 6]

nth (index)

It returns the index-th value that produces IterArray instance.

Internally, when .nth(index) is called, the values between 0 and index are cached. Then, if it is called nth with a lower index, no values are computed and if it is called with a greater index, only the values between index + 1 and the greater index are computed.

Example:

const IterArray = require('iterarray')

const iterable = IterArray([2, 5, 3, 7, 6, 1, 9, 4])
iterable.nth(2) // 3
iterable.nth(6) // 9
iterable.nth(0) // 2
iterable.nth(-5) // undefined
iterable.nth(10) // undefined

has (index)

It returns true if IterArray instance produces the index-th value.

Example:

const IterArray = require('iterarray')

const iterable = IterArray([2, 5, 3, 7, 6, 1, 9, 4])
iterable.has(0) // true
iterable.has(5) // true
iterable.nth(2) // true
iterable.nth(-5) // false
iterable.nth(21) // false

slice (start, end)

It returns an IterArray instance that iterates over the same values as sliced iterable.

Internally, the slice method does not slice the iterable, it just define a new iterable that iterates over sliced values (lazy evaluation). The sliced values of IterArray instance are also cached to get better performance when it called more than once.

length

A property of IterArray instance that can be an integer positive number or Infinity. The length property can be determined with a finite number only if all of the values of IterArray instance are cached.

In general, while not all of the values of iterable are traversed, length property is Infinity. However IterArray makes optimizations for arrays and primitive strings caching the values when the IterArray instance is created. Then, length is a finite number in a first instance.

Example:

const a = IterArray(new Set([1, 2]))
a.length // Infinity
a.nth(0) // 1
a.length // Infinity
a.nth(1) // 2
a.length // Infinity
a.nth(2) // undefined
a.length // 2

const b = IterArray(new Set([1, 2]))
b.length // Infinity
;[...b] // [1, 2]
b.length // 2

const c = IterArray([1, 2])
c.length // 2

const d = IterArray('abc')
c.length // 3

LICENSE

MIT