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

arraygen

v1.1.1

Published

Turn any array into a generator

Downloads

3

Readme

arraygen

Turn any array into a generator. (More accurately, grab the built-in iterator).

Getting started

For now you'll need a recent version of Node (v6+ works great). This will not work in old browsers or old Node versions unless it gets compiled to ES5. Currently there are no precompiled bundles.

npm install --save arraygen

Why?

I wrote this as an exercise to get more familiar with ES6 generators and iterators. The API is just a more natural (to me) wrapper around Array.prototype.slice(), but instead of returning the raw array, we return the array[Symbol.iterator]().

If this seems silly, it probably is. Let me know if you come up with any compelling use-cases.

Here's what the arr[Symbol.iterator]() allows you to do that you can't do with the ES5 Array API:

let g = arraygen(['c', 'd', 'e'])();
g.next(); // { value: 'c', done: false }
g.next(); // { value: 'd', done: false }
g.next(); // { value: 'e', done: false }
g.next(); // { value: undefined, done: true }

This is the same object type that gets returned from generator functions.

What does that buy us? It means that we can pull values one at a time, perhaps in response to asynchronous events, such as user clicks, network communications, etc...

Currently, I do most of those kinds of things using RxJS, but now that we have native support for something like it (albeit missing most of the cool utility API), maybe there are good use cases to skip the RxJS dependency.

How do you use it?

Destructuring assignment lets you easily grab elements from an array, for example:

let [a, b] = ['a', 'b', 'c'];
console.log(a); // 'a'
console.log(b); // 'b'

You can even use the rest operator to grab all the remaining elements of an array:

let [a, ...rest] = ['a', 'b', 'c'];
console.log(rest); // ['b', 'c']

OK, neat, but wouldn't it be cool if you could specify a range of elements?

For example:

let gen = arraygen(['a', 'b', 'c', 'd', 'e']);
let [...arr] = gen(1, 3); // ['b', 'c', 'd']

This is equivalent to:

let arr = ['a', 'b', 'c', 'd', 'e'].slice(1, 4); // ['b', 'c', 'd']

You could also grab the first n elements:

let gen = arraygen(['a', 'b', 'c', 'd', 'e']);
let [...arr] = gen(3); // ['a', 'b', 'c'];

Equivalent to:

let arr = ['a', 'b', 'c', 'd', 'e'].slice(0, 3); // ['a', 'b', 'c'];

Or the last n elements:

let gen = arraygen(['a', 'b', 'c', 'd', 'e']);
let [...arr] = gen(-3); // ['c', 'd', 'e'];

Equivalent to:

let arr = ['a', 'b', 'c', 'd', 'e'].slice(-3); // ['c', 'd', 'e'];

Grab all elements from start to the last element:

let gen = arraygen(['a', 'b', 'c', 'd', 'e']);
let [...arr] = gen(2, 'tail'); // ['c', 'd', 'e']

Equivalent to:

let arr = ['a', 'b', 'c', 'd', 'e'].slice(2); // ['c', 'd', 'e']

Of course, you can still mix and match any of this with other destructuring assignments:

const gen = arraygen(['a', 'b', 'c', 'd', 'e'])(-3);
const [c, ...rest] = gen(-3);
console.log(`${ c }, ${ JSON.stringify(rest) }`); // c, ["d","e"]

Equvilant to:

let [c, ...rest] = ['a', 'b', 'c', 'd', 'e'].slice(-3);
console.log(`${ c }, ${ JSON.stringify(rest) }`); // c, ["d","e"]

As you can see, the synchronous operations are probably better handled with native array.slice(). It's almost certainly less typing, anyway. It might get more compelling if you experiment with using asynchronous events to trigger the .next() method calls... Enjoy. Let me know if you do anything interesting or fun with this.

Written for Learn JavaScript with Eric Elliott

Join the chat at https://gitter.im/learn-javascript-courses/javascript-questions

An online course series for application developers. Ready to jump in? Learn more.