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

power-set

v1.1.8

Published

A utility function to generate the power set of an array of elements, with a ton of options to get the output you need.

Downloads

280

Readme

power-set

A utility function to generate the power set of an array of elements, with a ton of options to get the output you need.

Usage

var power = require('power-set');

var set = power([1,2,3], arr);
/*
set is:
[ [ 1, 2, 3 ],
  [ 1, 2 ],
  [ 1, 3 ],
  [ 1 ],
  [ 2, 3 ],
  [ 2 ],
  [ 3 ] ]
*/

The function signature is power(sourceArray, options)

Options

The following options are available (explained later in detail)

  • options.sort
  • options.limit
  • options.key
  • options.keySafe
  • options.filter
  • options.filterKey

options.sort

Sort the subarrays by their length. Possible values:

  • 'desc' or true: from the longest to the shortest
  • 'asc' : from the shortest to the longest
  • false : do not sort

options.limit

Select the subarrays based on their length. It can be:

  • a single number:
    • > 0 : return all subarrays whose size is equal to limit
    • <= 0 : return all subarrays whose length is equal sourceArray.length - abs(limit)
  • array: return subarrays whose length is included in the specified range. If one of the two limits is <0, the same approach explained above is adopted
var set = power([1,2,3], {limit : 2});
// set is [ [1,2], [1,3], [2,3] ]

var set = power([1,2,3,4], {limit : -1})
// set is [ [ 1,2,3 ], [ 1,2,4 ], [ 1,3,4 ], [2,3,4] ]

var set = power([1,2,3,4], {limit : [1,3]})
/* set is
[ [ 1, 2, 3 ],
  [ 1, 2, 4 ],
  [ 1, 2 ],
  [ 1, 3, 4 ],
  [ 1, 3 ],
  [ 1, 4 ],
  [ 1 ],
  [ 2, 3, 4 ],
  [ 2, 3 ],
  [ 2, 4 ],
  [ 2 ],
  [ 3, 4 ],
  [ 3 ],
  [ 4 ] ]
*/

var set = power([1,2,3,4], { limit : [-1,-2]})
/* set is
[ [ 1, 2, 3 ],
  [ 1, 2, 4 ],
  [ 1, 2 ],
  [ 1, 3, 4 ],
  [ 1, 3 ],
  [ 1, 4 ],
  [ 2, 3, 4 ],
  [ 2, 3 ],
  [ 2, 4 ],
  [ 3, 4 ] ]
*/

key

If the elements of the sourceArray are objects, extracts only the specified key.

var input = [ {id:1}, {id:2} ]; // default behaviour
var set = power(input)
/* set is
[ [ { id: 1 }, { id: 2 }, { id: 3 } ],
  [ { id: 1 }, { id: 2 } ],
  [ { id: 1 }, { id: 3 } ],
  [ { id: 1 } ],
  [ { id: 2 }, { id: 3 } ],
  [ { id: 2 } ],
  [ { id: 3 } ] ]
*/

var set = power(input, { key : 'id'})
/* set is
[ [ 1, 2, 3 ],
  [ 1, 2 ],
  [ 1, 3 ],
  [ 1 ],
  [ 2, 3 ],
  [ 2 ],
  [ 3 ] ]
*/

keySafe

Check if objects have the specified key, discard them if not. Default: FALSE.

var input = [ {id:1}, {id:2}, {other:3} ]; // last object has no id property
var set = power(input, { key : 'id', keySafe : false }) // default behaviour
/* set is
[ [ 1, 2, undefined ],
  [ 1, 2 ],
  [ 1, undefined ],
  [ 1 ],
  [ 2, undefined ],
  [ 2 ],
  [ undefined ] ]
*/

var set = power(input, { key : 'id', keySafe : true })
/* set is
[ [ 1, 2 ],
  [ 2 ],
  [ 1 ] ]
*/

filter

Select only the subarrays that contain all, any, none of the passed element(s). If the elements are objects, it is possible to set a filterKey to compare to (see later). This options is an object with one of the following properties:

  • any : select subarrays that contain any of the elements passed (default)
  • all : select subarrays that contain all the passed elements
  • none : select subarrays that do not contain any of the passed elements
var set = power([1,2,3,4,5], { filter : 1 });
/*
same as { filter : { any : [1]} }
set is:
[ [ 1, 2, 3, 4, 5 ],
  [ 1, 2, 3, 4 ],
  [ 1, 2, 3, 5 ],
  [ 1, 2, 3 ],
  [ 1, 2, 4, 5 ],
  [ 1, 2, 4 ],
  [ 1, 2, 5 ],
  [ 1, 2 ],
  [ 1, 3, 4, 5 ],
  [ 1, 3, 4 ],
  [ 1, 3, 5 ],
  [ 1, 3 ],
  [ 1, 4, 5 ],
  [ 1, 4 ],
  [ 1, 5 ],
  [ 1 ] ]
*/

var set = power([1,2,3,4,5], { filter : { any : [1,5] } });
/* set is
[ [ 1, 2, 3, 4, 5 ],
  [ 1, 2, 3, 4 ],
  [ 1, 2, 3, 5 ],
  [ 1, 2, 3 ],
  [ 1, 2, 4, 5 ],
  [ 1, 2, 4 ],
  [ 1, 2, 5 ],
  [ 1, 2 ],
  [ 1, 3, 4, 5 ],
  [ 1, 3, 4 ],
  [ 1, 3, 5 ],
  [ 1, 3 ],
  [ 1, 4, 5 ],
  [ 1, 4 ],
  [ 1, 5 ],
  [ 1 ],
  [ 2, 3, 4, 5 ],
  [ 2, 3, 5 ],
  [ 2, 4, 5 ],
  [ 2, 5 ],
  [ 3, 4, 5 ],
  [ 3, 5 ],
  [ 4, 5 ],
  [ 5 ] ]
*/


var set = power([1,2,3,4,5], { filter : { all : [1,5,4] } });
// set is [ [ 1, 2, 3, 4, 5 ], [ 1, 2, 4, 5 ], [ 1, 3, 4, 5 ], [ 1, 4, 5 ] ]

var set = power([1,2,3,4,5], { filter : { none : [1,5,4] } });
// set is [ [ 2, 3 ], [ 2 ], [ 3 ] ]

options.filterKey

When using the filter option, allows to filter by a specific key in the objects. As for key, the keySafe options will make sure that the key exists in the object before comparing.

var input = [{id:1}, {id:2}, {id:3}, {id:4}, {id:5}];
var set = power(input, { filter : {none : [1,5,4]} }, filterKey : 'id' });
// set is [ [ {id:2}, {id:3} ] , [ {id:2} ] , [ {id:3} ] ]

Note: when using key, the keys are extracted immediately, therefore using a different filterKey will not work.