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

heaps-permute

v2.0.3

Published

A JavaScript implementation of Heap's efficient Permutation Algorithm

Downloads

196

Readme

heaps-permute

NPM Version Build Status Coverage Status

A JavaScript implementation of Heap's efficient Permutation Algorithm.

This algorithm is "efficient", but still runs in factorial time. It will probably run out of memory if you try it on an array longer than 10 items. For example:

  • n = 7 will finish within 5 milliseconds (5040 permutations)
  • n = 8 will finish within 50 milliseconds (40,320 permutations)
  • n = 9 will finish within 500 milliseconds (362,880 permutations)
  • n = 10 will finish within 6000 milliseconds (328,800 permutations)

Install

$ npm install heaps-permute
var permute = require('heaps-permute');

Usage

permute(array)

This returns all the permutations for the given array.

permute([1, 2, 3]);

// returns
[
  [ 1, 2, 3 ],
  [ 2, 1, 3 ],
  [ 3, 1, 2 ],
  [ 1, 3, 2 ],
  [ 2, 3, 1 ],
  [ 3, 2, 1 ]
];


permute(['a', 'b', 'c', 'd'])

// returns
[
  [ 'a', 'b', 'c', 'd' ],
  [ 'b', 'a', 'c', 'd' ],
  [ 'c', 'a', 'b', 'd' ],
  [ 'a', 'c', 'b', 'd' ],
  [ 'b', 'c', 'a', 'd' ],
  [ 'c', 'b', 'a', 'd' ],
  [ 'd', 'b', 'c', 'a' ],
  [ 'b', 'd', 'c', 'a' ],
  [ 'c', 'd', 'b', 'a' ],
  [ 'd', 'c', 'b', 'a' ],
  [ 'b', 'c', 'd', 'a' ],
  [ 'c', 'b', 'd', 'a' ],
  [ 'd', 'a', 'c', 'b' ],
  [ 'a', 'd', 'c', 'b' ],
  [ 'c', 'd', 'a', 'b' ],
  [ 'd', 'c', 'a', 'b' ],
  [ 'a', 'c', 'd', 'b' ],
  [ 'c', 'a', 'd', 'b' ],
  [ 'd', 'a', 'b', 'c' ],
  [ 'a', 'd', 'b', 'c' ],
  [ 'b', 'd', 'a', 'c' ],
  [ 'd', 'b', 'a', 'c' ],
  [ 'a', 'b', 'd', 'c' ],
  [ 'b', 'a', 'd', 'c' ]
];

permute.callback(array, cb)

This method invokes cb each time a unique permutation is found. The new permutation will be the only argument to the callback.

permute.callback([1, 2, 3], console.log);

// will print to the console:

[ 1, 2, 3 ]
[ 2, 1, 3 ]
[ 3, 1, 2 ]
[ 1, 3, 2 ]
[ 2, 3, 1 ]
[ 3, 2, 1 ]

Do not pass this method a third argument: it's needed for an internal counter.

Tests

To run the test suite, first install the dependencies, then run npm test:

$ npm install
$ npm test

To test code coverage:

$ npm run cover