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

cartesian-mutable

v0.0.4

Published

Cartesian product with a twist: each set can mutate depending on the values picked from the previous sets

Downloads

5

Readme

cartesian-mutable NPM version Build Status

Cartesian product with a twist: each set can mutate depending on the values picked from the previous sets.

Basic usage

The API is backwards compatible with many other cartesian modules on npm.

const cartesian = require('cartesian-mutable');
console.log(cartesian({
	greeting: ['Hello', 'Good bye'],
	name: ['Mr. Anderson', 'Agent Smith']
}));

🡇

[ { greeting: 'Hello', name: 'Mr. Anderson' },
  { greeting: 'Hello', name: 'Agent Smith' },
  { greeting: 'Good bye', name: 'Mr. Anderson' },
  { greeting: 'Good bye', name: 'Agent Smith' } ]

Fun stuff, that makes this module different

If you need a property to depend on other properties' values, use a function instead of an array. The function will be called with a single argument: an object containing values for all the properties above this one.

Mutate a set of values for a property

If the function returns an array, it will be used as a set of values for the property.

const cartesian = require('cartesian-mutable');
console.log(cartesian({
	meal: ['breakfast', 'lunch'],
	drink: (v) => v.meal === 'breakfast' ? ['water', 'milk'] : ['tea', 'coffee']
}));

🡇

[ { meal: 'breakfast', drink: 'water' },
  { meal: 'breakfast', drink: 'milk' },
  { meal: 'lunch', drink: 'tea' },
  { meal: 'lunch', drink: 'coffee' } ]

Make a property conditional

If the function returns an undefined, the property will not be used in the product.

const cartesian = require('cartesian-mutable');
console.log(cartesian({
	meal: ['breakfast', 'lunch'],
	drink: (v) => v.meal === 'breakfast' ? ['water', 'milk'] : ['tea', 'coffee'],
	sugar: (v) => v.meal === 'breakfast' ? undefined : [true, false] 
}));

🡇

[ { meal: 'breakfast', drink: 'water' },
  { meal: 'breakfast', drink: 'milk' },
  { meal: 'lunch', drink: 'tea', sugar: true },
  { meal: 'lunch', drink: 'tea', sugar: false },
  { meal: 'lunch', drink: 'coffee', sugar: true },
  { meal: 'lunch', drink: 'coffee', sugar: false } ]

Specify an explicit value for a property

If the function returns any other value (i.e. neither an Array nor an undefined) it will be used as a value for the property.

const cartesian = require('cartesian-mutable');
console.log(cartesian({
	meal: ['breakfast', 'lunch'],
	drink: (v) => v.meal === 'breakfast' ? ['water', 'milk'] : ['tea', 'coffee'],
	food: (v) => v.meal === 'breakfast' ? 'granola' : 'sandwich'
}));

🡇

[ { meal: 'breakfast', drink: 'water', food: 'granola' },
  { meal: 'breakfast', drink: 'milk', food: 'granola' },
  { meal: 'lunch', drink: 'tea', food: 'sandwich' },
  { meal: 'lunch', drink: 'coffee', food: 'sandwich' } ]

License

MIT License