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

art-comprehensions

v3.0.8

Published

Simple, powerful iteration comprehensions for JavaScript.

Downloads

24

Readme

API

Functions

each()
array()
object()
find()
inject()
reduce()

COMMON API

All comprehensions support four standard input patterns:

f() => undefined
f(source) => into
f(source, with) => into
f(source, into, with) => into

f(source, options) => into
f(source, into, options) => into

Where:

  • f: is each/array/object/find/reduce

  • source: can be one of:

    • array-like: iterated over using for (i = 0; i < source.length; i++)
    • object: iterated over using for (k in source)
    • null/undefined: treated as an empty container (eqivelent to passing in [] or {})
  • options:

    • when: (value, key) -> booleanish
    • with: (see with)
    • map: (value, key) -> new value
    • into/returning/inject: any (these are alias of each other)
  • with:

    • (value, key) -> new value
    • Generally, this function generates the 'value' used for each part of the iteration. When constructing a new collection, this is the value for each entry. ('find' and 'reduce' use this differently)

each

Iterate over the source container. By default, just returns the source container.

each(source) => source
each(source, with) => source
each(source, into, with) => into

each(source, options) => options.into ? source
each(source, into, options) => into

each differs from the common-api:

  1. into defaults to source
  2. each doesn't modify source

object

Create a new object derived fromthe source container. The default behavior differs slightly between array-sources and object-sources:

  • from-array:
object(source) => new {} derived from source
object(source, with) => new {} derived from source
object(source, options) => options.into ? new {} derived from source

object(source, into, with) => into
object(source, into, options) => into

object differences from the common-api:

  • options:
    • key: (value, key, into, whenBlockResult) -> value
    • withKey: (alias: key)
    • default key is v if source is array-like, k if object
  • into defaults to a new object ({})
  • each iteration, into is updated as folows: into[options.key()] = with()

array

Create a new array from the values and keys of the source container.

array(source) => new [] containing all values from source
array(source, with) => new [] of 'with' applied to each value in source
array(source, into, with) => into

array(source, options) => options.into ? new [] derived from source
array(source, into, options) => into

array differs from the common-api:

  • into defaults to a new array []
  • each iteration, into is updated as folows: into.push(with())

find

Find an element in the source container.

find(source) => first trueish value from source
find(source, with) => first trueish value returned from 'with'
find(source, options) => a value from 'options.with' or directly from source

find differs from common api:

  • returns:

    • the last value returned by with
    • or returns undefined if with was never executed
  • stops

    • if have when: !!when() == true
    • if no when: !!with() == true

reduce and inject

These comprehensions are very similar. Inject always injects an initial last value (from inject/into/returning value if passed in, undefined otherwise). Reduce never injects a value.

  • last gets initialized to

    • for inject: inject/into/returning
    • for reduce: the first value in source
  • with has a different argument order:

    • (last, value, key) ->

    • This allows you to drop-in functions that take two args and reduce them to one like:

      • add = (a, b) => a + b
    • The default with still returns v (which is now the second argument).

    • Note, for reduce, with gets called N-1 times, for inject, with gets called N times.

  • each iteration, last is updated as follows: last = with(last, value, key)

  • map is super handy; it gets called AFTER when but before with

// sum records containing amount
reduce(source, 0, {map: ({amount}) -> amount, with: add})