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

cerveza

v1.0.5

Published

Process arrays and collections with chainable processors

Downloads

8

Readme

Cerveza

Cerveza is an array processor. It can pick specified properties from a list of objects and reduces them to a single object using the supplied processors. It works on nested properties as well.

Cerveza is a small utility function with no dependencies.

Getting started

  npm install cerveza
const cerveza = require('cerveza')

API

cerveza(values, processor)

Process an array.

  • values is an array of values to process. The array will be flattened before processing.
  • processor is processor specification for processing the values. For details about processor specification, see below.
  • returns the processed result.

cerveza.define( processorDefs )

Define additional named processors.

  • processorDefs An object of named processors to define. Each definition is a function which takes the provided argument (if any) and returns a valid processor specification, as described below.
  • returns the instance of cerveza, to allow chaining

cerveza.processor( processorSpec )

Set the default processor for this cerveza instance. This allows you to reuse the cerveza instance to process multiple arrays with the same processor.

  • processorSpec A processor specification, as described below.
  • returns the instance of cerveza, to allow chaining

cerveza.configure( { define?, processor? })

Sets the configuration for this cerveza instance

  • define See above.
  • processor See above.
  • returns the instance of cerveza, to allow chaining

cerveza.create( { define?, processor? })

Creates a new cerveza instance with the provided configuration.

  • define See above.
  • processor See above.
  • returns the instance of cerveza, to allow chaining

Processor specifications

"<name>"

The name of a defined processor.

  cerveza([1,2,3], 'override' ) // 3

<function>

A custom function to process the array.

  cerveza( [1,2,3], arr => Math.min(...arr) ) // 1

{ ...<props> }

An object which defines processors for nested properties.

  cerveza( 
    [ { a:1 }, { a:2, b:2 }, { b:3 } ], 
    { a:'override', b:'all' } 
  ) // { a:2, b:[2, 3] }

This cannot appear within a list of processors. Use the props named processor instead.

[{ <name>: <arg> }]

A named processor with an argument.

  cerveza( 
    [ { a:1 }, { a:2, b:2 }, { b:3 } ], 
    { a :'override', b: [{ override: 10 }], c: [{ override: 100 }] } 
  ) // { a:2, b:3, c: 100 }

This cannot appear outside an array of processors, to avoid confusion with named properties. Always put these specifications in an array, even if you need only one.

[...<processors>]

An array of processors. Each processor will be called in turn with the result of the previous processor, starting with the array of values picked from the list of objects.

Predefined processors

"all"

Returns the whole array of picked values. The same as []

"override"

Returns the last defined value.

[{override: <default>}]

Returns the last defined value, or the default if no values are found.

[{ set: <value> }]

Return the literal value, the same as () => <value>

[{map: <fn>}]

Shortocut for arr => arr.map(<fn>)

[{filter: <fn>}]

Shortocut for arr => arr.map(<fn>)

[{reduce: <fn>}]

Shortocut for arr => arr.reduce(<fn>)

[{sort: <fn>}]

Shortocut for arr => arr.sort(<fn>)

flat

Shortocut for arr => arr.flat(Infinity)

[{flat: <depth>}]

Shortocut for arr => arr.flat(<depth>)

[{pick: <name>}]

Picks values from another property. Shortcut for (_,objects) => objects.map(obj=>obj[name]

[{pick: <fn>}]

Picks values using a custom function. Shortcut for (_,objects) => objects.map(fn)

[{props: {...processors}}]

Process nested properties. This is the same as {...processors}, but can be chained with other processors.

Usage

Processing an array

let reduced = cerveza( [1,2,3], 'override') // 3

Processing nested properties

let result = cerveza( objects, {
  someProp: 'override',
  someOtherProp: 'all',
  fixedProp: [{ set: 10 }],
  renamedProp: [{ pick: 'originalName'}, 'override'],
  nestedProps: {
    first: 'override',
  }
})

Using custom processors


let result = cerveza([{
  inheritable: {
    a: 1,
    b: 2
  }
},{
  inheritable: {
    a: 3,
    c: 4
  },
}], {
  inherited: [
    { pick: 'inheritable' }, 
    arr => Object.assign({},...arr)
  ],
}) // { a: 3, b: 2, c: 4 }

Defining new named processors

const myCerveza = cerveza({
  sum: ()=> arr => arr.reduce( (a, b) => a+b, 0 ),
  atLeast: min => arr => arr.filter( x => x > min ),
  sumLarge: min => [ { atLeast:min }, 'sum' ]
})
let result = myCerveza( objects, {
  totalCount: [ { pick: 'count' }, 'sum' ],
  largeCounts: [ { pick: 'count' }, { atLeast: 10 } ],
  sumLarge: [ { pick: 'count' }, { sumLarge: 10 } ],
})