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

binary-variations

v1.0.2

Published

A small permutations module, which gives you all possible binary variations in order.

Downloads

23

Readme

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

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.

| 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 = 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.

| Param | Type | Description | | --- | --- | --- | | variations | Array | The 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.

| 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 = 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.