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'
ortrue
: from the longest to the shortest'asc'
: from the shortest to the longestfalse
: 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 tolimit
<= 0
: return all subarrays whose length is equalsourceArray.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 elementsnone
: 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.