1.0.1 • Published 7 years ago
shuffle-obj-arrays v1.0.1
shuffle-obj-arrays
Shuffles the arrays of the given (object) map using shuffle-array.
Install
npm install shuffle-obj-arrays --saveExample
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} -trueto copy the given object. Defaults tofalse.copyNonArrays{boolean} -trueto copy non-array properties of the given object. Only works whencopyistrue. Defaults tofalse.rng{function} - Custom random number generator. Defaults toMath.random.
Example:
const shuffleObjArrays = require( 'shuffle-obj-arrays' );
// Using a external pseudo-random number 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
- shuffle-array - Shuffles an array using Fisher-Yates algorithm and allows to pass a custom pseudo-random number generator (PRNG)
- seedrandom - Predictive PRNG
- one-wise - One-wise combinatorial testing for the arrays of the given object.