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

@mitchallen/shuffle

v0.1.10

Published

Uses Fisher-Yates to shuffle an array

Downloads

13

Readme

@mitchallen/shuffle

Uses Fisher-Yates to shuffle an array.


Installation

You must use npm 2.7.0 or higher because of the scoped package name.

$ npm init
$ npm install @mitchallen/shuffle --save

Usage

"use strict";

var shuffleFactory = require("@mitchallen/shuffle");

var list = [1, 2, 3, 4, 5];

var shuffler = shuffleFactory.create({ array: list });

var shuffled = shuffler.shuffle();

console.log(shuffled);

Browser Usage:

You can reference a minimized client version inside an HTML script tag using one of these URL's:

  • https://cdn.rawgit.com/mitchallen/shuffle/v0.1.4/dist/shuffle.min.js
  • https://unpkg.com/@mitchallen/[email protected]/dist/shuffle.min.js

Adjust for the version that you wish to use.

The rawgit.com URL will pull based on the version from GitHub.

The unpkg.com URL will pull based on the version in npmjs.com.

See http://rawgit.com and https://unpkg.com for other ways to retrieve the file.

The factory function can be retrieved from window.MitchAllen.Shuffle:

var factory = window.MitchAllen.Shuffle;
var list = [1, 2, 3, 4, 5];
var obj = factory.create({ array: list });
var shuffled = obj.shuffle();

Example:

<!DOCTYPE html>
<html>
  <head>
<meta charset="utf-8">
    <title>Shuffle Example</title>
    <meta name="description" content="Shuffle Example">
    <!-- either cdn should work -->
    <!--
    <script src="https://cdn.rawgit.com/mitchallen/shuffle/v0.1.10/dist/shuffle.min.js"></script>
    -->
    <script src="https://unpkg.com/@mitchallen/[email protected]/dist/shuffle.min.js"></script>
    <script>
      var factory = window.MitchAllen.Shuffle;
      var list = [1, 2, 3, 4, 5];
      var obj = factory.create({ array: list });
      var shuffled = obj.shuffle();
      console.log(shuffled); 
    </script>
  </head>
  <body>
    <h1>Shuffle Example</h1>
    <p>See the JavaScript console for results.</p>
  </body>
</html>

Methods

create( spec )

Factory method that returns a shuffle object.

It takes one spec parameter that must be an object an array value specifying the array to be shuffled.

The method will return null if create fails, such as with bad parameters.

You can call create multiple times to create multiple shuffle objects.

var shuffleFactory = require("@mitchallen/shuffle");

var s1 = shuffleFactory.create( { array: [ 1, 2, 3, 4, 5 ] } );
var s2 = shuffleFactory.create( { array: [ 6, 7, 8, 9, 10 ] }  );

if(!s1 || !s2) ...

shuffle()

Returns a shuffled version of the array passed to the create method. It does not affect the original but instead returns a shuffled copy. You can call shuffle multiple times and it will keep shuffling it's internal copy.

var shuffleFactory = require("@mitchallen/shuffle");

var s1 = shuffleFactory.create( { array: [ 1, 2, 3, 4, 5 ] } );

console.log( s1.shuffle() );
console.log( s1.shuffle() );

Testing

To test, go to the root folder and type (sans $):

$ npm test

Repo(s)


Contributing

In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.


Version History

Version 0.1.10

  • updated .npmignore

Version 0.1.9

  • updated test cases for 100% code coverage

Version 0.1.8

  • integrated travis-ci and codecov.io

Version 0.1.4

  • Added namespace requirement for browser: changed window.SHUFFLE to window.MitchAllen.Shuffle

Version 0.1.3

  • updated CDN URL

Version 0.1.2

  • added client example

Version 0.1.1

  • added client side distribution

Version 0.1.0

  • initial release