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']
).
a | b | c |
---|---|---|
a | ||
b | ||
a | b | |
c | ||
a | c | |
b | c | |
a | b | c |
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 bit | 2 bit | 3 bit |
---|---|---|
1 | 0 | 0 |
0 | 1 | 0 |
1 | 1 | 0 |
0 | 0 | 1 |
1 | 0 | 1 |
0 | 1 | 1 |
1 | 1 | 1 |
API Documentation
Array
binaryVariations ⇒ 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.
Param | Type | Default | Description |
---|---|---|---|
variations | Array |
A array of variations who's binary, ordered permutations you want to compute. | |
[filter] | Object |
A filter object containing inclusion or exclusion rules. | |
[filter.include] | Array |
An array of items or combination of items to include. | |
[filter.exclude] | Array |
An array of items or combination of items to exclude. | |
[filter.precedence] | boolean |
true |
Whether the inclusion filter takes precedence or not. |
[callback] | function |
A Callback to call for every matched combination. |
Example (All permutations)
var binaryVariations = ; ; "a" "b" "a" "b" "c" "a" "c" "b" "c" "a" "b" "c"
Example (Inclusion)
var binaryVariations = ; ; "a" "a" "b" "b" "c" "a" "b" "c"
Example (Exclusion)
var binaryVariations = ; ; "b" "c" "a" "c"
- binaryVariations ⇒
Array
- .expect(variations) ⇒
number
- .join(variations, [separator], [prefix], [suffix]) ⇒
string
- .expect(variations) ⇒
number
binaryVariations.expect(variations) ⇒ 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.
Param | Type | Description |
---|---|---|
variations | Array |
The array of possible variations. |
Example
var binaryVariations = ;var expect = binaryVariationsexpect; ;// => 1 ;// => 3 ;// => 7 ;// => 15
string
binaryVariations.join(variations, [separator], [prefix], [suffix]) ⇒ 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.
Param | Type | Default | Description |
---|---|---|---|
variations | Array |
The variations array to join. | |
[separator] | string |
"," |
Specifies a string to separate each element of the array. |
[prefix] | string |
The prefix prepended. | |
[suffix] | string |
The suffix appended. |
Example (Default separator)
var binaryVariations = ;var join = binaryVariationsjoin; ; 'a,b,c'
Example (Custom separator)
var binaryVariations = ;var join = binaryVariationsjoin; ; 'a-b-c'
Example (Prefix)
var binaryVariations = ;var join = binaryVariationsjoin; ; '-a,b,c'
Example (Suffix)
var binaryVariations = ;var join = binaryVariationsjoin; ; '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.