Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

shuffle-obj-arrays

1.0.0 • Public • Published

shuffle-obj-arrays

Shuffles the arrays of the given (object) map using shuffle-array.

Install

npm install shuffle-obj-arrays --save

Example

const shuffleObjArrays = require( 'shuffle-obj-arrays' );
 
shuffleObjArrays( {
    "foo": [ "x", "y" ],
    "bar": [ "a", "b", "c", "d" ],
    "baz": [ "f", "g" ]
} )

will return something like

{
    "foo": [ "y", "x" ],
    "bar": [ "c", "b", "a", "d" ],
    "baz": [ "g", "f" ]
}

API

shuffle-obj-arrays accepts the same options as shuffle, plus the option copyNonArrays.

shuffleObjArrays( obj, [options] )

Randomizes the order of the elements in all arrays of the given object.

  • obj {object} - The given object.
  • options {object} - Options, which may have:
    • copy {boolean} - true to copy the given object. Defaults to false.
    • copyNonArrays {boolean} - true to copy non-array properties of the given object. Only works when copy is true. Defaults to false.
    • rng {function} - Custom random number generator. Defaults to Math.random.

Example:

const shuffleObjArrays = require( 'shuffle-obj-arrays' );
 
// Using a external pseudo-random generator
// https://github.com/davidbau/seedrandom
const seedrandom = require( 'seedrandom' );
const myRng = seedrandom( 'my seed' );
 
const options = {
    copy: true,
    rng: myRng
};
 
shuffleObjArrays(
    {
        "foo": [ "x", "y" ],
        "bar": [ "a", "b", "c", "d" ],
        "baz": [ "f", "g" ],
        "zoo": "non array property"
    },
    options
);

will return something like

{
    "foo": [ "y", "x" ],
    "bar": [ "c", "b", "a", "d" ],
    "baz": [ "g", "f" ]
}

See also

License

MIT © Thiago Delgado Pinto

install

npm i shuffle-obj-arrays

Downloadsweekly downloads

59

version

1.0.0

license

MIT

repository

githubgithub

last publish

collaborators

  • avatar