Skip to content

thiagodp/shuffle-obj-arrays

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

shuffle-obj-arrays

Version Build Status Known Vulnerabilities

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

Install

npm install shuffle-obj-arrays

Example

const shuffleObjArrays = require( 'shuffle-obj-arrays' );

console.log(
	shuffleObjArrays( {
		"foo": [ "x", "y" ],
		"bar": [ "a", "b", "c", "d" ],
		"baz": [ "f", "g" ]
	} )
);

It will print something like:

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

Version 1

API

Version 1 accepts the same options as shuffle, plus copyNonArrays.

Syntax:

shuffleObjArrays(
	obj: object,
	options: { copy: false, copyNonArrays: false },
	rng: (n: number): number = Math.random
): object

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 number generator
// https://github.com/davidbau/seedrandom
const seedrandom = require( 'seedrandom' );

const options = {
    copy: true,
    rng: seedrandom( 'my seed' )
};

console.log(
	shuffleObjArrays(
		{
			"foo": [ "x", "y" ],
			"bar": [ "a", "b", "c", "d" ],
			"baz": [ "f", "g" ],
			"zoo": "non array property"
		},
		options
	)
);

It returns something like:

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

See also

License

MIT © Thiago Delgado Pinto