1.0.2 • Published 8 years ago

binary-variations v1.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

Installation

Install binary-variations as dependency (append --save-dev for development only):

npm install binary-variations

Explanation

Assume you have given a list of items, which you want to know all possible combinations who qualify that those items stay in order as they are defined and they don't repeat, but items can be omitted.

E.g. assume a list of a, b, c (or in programming terms ['a', 'b', 'c']).

abc
a
b
ab
c
ac
bc
abc

Which gives us 7 combinations, or as it turns out (2^n) - 1 - where n is the number of items. In this case: (2^3) - 1 = (2*2*2) - 1 = 8 - 1 = 7

Actually this is equal to the possible combinations of n bits -1.

1 bit2 bit3 bit
100
010
110
001
101
011
111

API Documentation

binaryVariations ⇒ Array

Computes all possible binary ordered permutations of a given variations array, considering optional inclusion/exclusion filter.

Returns: Array - - Returns an array of possible binary, ordered permutations.

ParamTypeDefaultDescription
variationsArrayA array of variations who's binary, ordered permutations you want to compute.
filterObjectA filter object containing inclusion or exclusion rules.
filter.includeArrayAn array of items or combination of items to include.
filter.excludeArrayAn array of items or combination of items to exclude.
filter.precedencebooleantrueWhether the inclusion filter takes precedence or not.
callbackfunctionA Callback to call for every matched combination.

Example (All permutations)

var binaryVariations = require('binary-variations');

binaryVariations(['a', 'b', 'c']);
=> [["a"], ["b"], ["a", "b"], ["c"], ["a", "c"], ["b", "c"], ["a", "b", "c"]]

Example (Inclusion)

var binaryVariations = require('binary-variations');

binaryVariations(['a', 'b', 'c'], {
 // include only 'a' or containing 'a' and 'b' or containing 'b' and 'c'
 include: [ 'a', ['a', 'b'], ['b', 'c'] ]
});
=> [["a"], ["a", "b"], ["b", "c"], ["a", "b", "c"]]

Example (Exclusion)

var binaryVariations = require('binary-variations');

binaryVariations(['a', 'b', 'c'], {
 // exclude only 'a' or containing 'a' and 'b' or containing 'b' and 'c'
 exclude: [ 'a', ['a', 'b'], ['b', 'c'] ]
});
=> [["b"], ["c"], ["a", "c"]]

binaryVariations.expect(variations) ⇒ number

Returns the number of all possible variations. Which is the sum of (2^n)-1.

Kind: static method of binaryVariations
Returns: number - - Returns the amount of possible combinations.

ParamTypeDescription
variationsArrayThe array of possible variations.

Example

var binaryVariations = require('binary-variations');
var expect = binaryVariations.expect;

expect( ['a'] );
// => 1

expect( ['a', 'b'] );
// => 3

expect( ['a', 'b', 'c'] );
// => 7

expect( ['a', 'b', 'c', 'd'] );
// => 15

binaryVariations.join(variations, separator, prefix, suffix) ⇒ string

The join() function joins all element of variations into a string.

Kind: static method of binaryVariations
Returns: string - - Returns all elements of variations joined into a string.

ParamTypeDefaultDescription
variationsArrayThe variations array to join.
separatorstring","Specifies a string to separate each element of the array.
prefixstringThe prefix prepended.
suffixstringThe suffix appended.

Example (Default separator)

var binaryVariations = require('binary-variations');
var join = binaryVariations.join;

join( ['a', 'b', 'c'] );
=> 'a,b,c'

Example (Custom separator)

var binaryVariations = require('binary-variations');
var join = binaryVariations.join;

join( ['a', 'b', 'c'], '-' );
=> 'a-b-c'

Example (Prefix)

var binaryVariations = require('binary-variations');
var join = binaryVariations.join;

join( ['a', 'b', 'c'] , ',', '-' );
=> '-a,b,c'

Example (Suffix)

var binaryVariations = require('binary-variations');
var join = binaryVariations.join;

join( ['a', 'b', 'c'] , ',', '', '-' );
=> 'a,b,c-'

#License

The MIT License (MIT)

Copyright (c) 2016 Andreas Deuschlinger

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.