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

permutation-way

v2.0.0

Published

permutation using Steinhaus-Johnson-Trotter algorithm with Even's speed up. This package deliver asynchronously permutations of the original array

Downloads

3

Readme

Build Status

About

This is a module to deliver asynchronously one unique permutation of the input array.

You can get permutations of any objects into an array. If you want to get permutation of complex object you should provide a comparator function.

Examples

Declaration

The module permutation-way has one function: permutationOf( inputArray[, comparator][, options] ).

Where parameters are

  • inputArray an array of any type element
  • comparator a function used to compare each element of the inputArray. This is optional
  • options an object to give extra options to the permutation engine.
    • options.max is a number that give the maximum number of permutation to receive. This is usefull if you want only 4 permutations for an inputArray of 10 elements.

The inputArray is always returned first. So if you want to receive 4 fresh new permutation at maximum for a big array, you must set the options.max to 5.

Simple Example

We see how to get permutation of simple javascript types.

var p = require( 'permutation-way' );

var input = [ 1, 2, 3 ];

p.permutationOf( input ).on( 'data', function ( data ) {
  // display one permutation
  console.log( data );
} ).on( 'end', function () {
  // end of permutation
  console.log( 'end' );
} ).on( 'error', function ( err ) {
  // Error object.
  // Error types: invalid input or all permutation not found (which should never happen)
} );

This will output into the console:

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

Even the input array match one permutation and hence is emmitted too.

Events

  • 'data' event is emmitted for each permutation array found
  • 'end' event is emmitted once all permutation were found without error
  • 'error' event is emmitted if an error is met like invalid input or not all permutation found

Permutation of objects

You need to pass a comparator function to to the second argument of the fuunction permutationOf


var p = require( 'permutation-way' );

var input = [ {code:1}, {code:2}, {code:3} ];
/**
 * compare two objects and tells about the order of one object relative to the second object.
 */
function comparator ( a, b ) {
  if ( a.code > b.code ) {
    // after
    return 1;
  } else if ( a.code < b.code ) {
    // before
    return -1;
  } else {
    // equals
    return 0;
  }

p.permutationOf( input, comparator ).on( 'data', function ( data ) {
  // display one permutation
  console.log( data );
} ).on( 'end', function () {
  // end of permutation
  console.log( 'end' );
} ).on( 'error', function ( err ) {
  // Error object.
  // Error types: invalid input or all permutation not found (which should never happen)
} );

Much more permutations.

If you want to play with a high volume of permutation, you can start with an array of 10 elements.

Limit the number of permutation to receive

You have to set the options.max to a positive number. If the value of options.max is not a number of is a negative number, then an 'error' event is emitted. If this value is set to 0, then, no 'data' event is emitted.

if you set options.max the permutation found will be compared with the options.max value set. An error will be emitted if both value are not the same.


var p = require( 'permutation-way' );

var input = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
var options = {
  max: 6
};

p.permutationOf( input, options ).on( 'data', function ( data ) {
  // display one permutation
  console.log( data );
} ).on( 'end', function () {
  // end of permutation
  console.log( 'end' );
} ).on( 'error', function ( err ) {
  // Error object.
  // Error types: invalid input or all permutation not found (which should never happen)
} );

Interrupt the permutation count

Let's say you have a big array and you do not want to handle all its permutation. You can either set the options.max value or you can interrupt the permutation search based on an external condition


var p = require( 'permutation-way' );

var input = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
var actualCount = 0;

function checkError ( data ) {
  var hasError = false;
  // do something and set hasError...
  return hasError;
}

var pengine = p.permutationOf( input ).on( 'data', function ( data ) {
  // display one permutation
  actualCount++;
  console.log( data );
  if ( actualCount >= 4 ) {
    pengine.interrupt();
  }  else if ( checkError( data ) ) {
    pengine.interrupt( new Error( 'Error found' ) );
  }

} ).on( 'end', function () {
  // end of permutation
  console.log( 'end' );
} ).on( 'error', function ( err ) {
  // Error object.
  // Error types: invalid input or all permutation not found (which should never happen)
} );