All files factory.js

100% Statements 166/166
100% Branches 29/29
100% Functions 2/2
100% Lines 166/166

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 1672x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 22x 22x 22x 22x 22x 22x 22x 17x 17x 10x 10x 17x 22x 7x 7x 7x 22x 5x 5x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 8x 8x 28x 15x 15x 15x 10x 10x 15x 28x 28x 28x 28x 1x 1x 1x 10x 10x 28x 4x 28x 1x 1x 10x 10x 10x 10x 28x 38x 38x 38x 38x 38x 38x 38x 38x 10x 28x 1x 1x 10x 28x 22x 2x 2x 2x 2x 2x  
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var isArrayLike = require( '@stdlib/assert/is-array-like' );
var isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var deepCopy = require( '@stdlib/utils/copy' );
var floor = require( '@stdlib/math/base/special/floor' );
var randu = require( '@stdlib/random/base/mt19937' ).factory;
var format = require( '@stdlib/string/format' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var defaults = require( './defaults.json' );
var validate = require( './validate.js' );
 
 
// MAIN //
 
/**
* Returns a function to create a random permutation of elements from an array-like object.
*
* @param {Options} [config] - function options
* @param {PositiveInteger} [config.seed] - integer-valued seed
* @param {string} [config.copy="shallow"] - default copy option (`deep`, `shallow` or `none`)
* @throws {TypeError} options argument must be an object
* @returns {Function} shuffle function
*
* @example
* var shuffle = factory({
*     'seed': 249
* });
* var data = [ 3, 8, 4, 8 ];
* var out = shuffle( data );
* // e.g., returns [ 4, 3, 8, 8 ]
*/
function factory( config ) {
	var conf;
	var rand;
	var err;
 
	conf = deepCopy( defaults );
	if ( arguments.length ) {
		err = validate( conf, config );
		if ( err ) {
			throw err;
		}
	}
	if ( config && hasOwnProp( config, 'seed' ) ) {
		rand = randu({
			'seed': config.seed
		});
	} else {
		rand = randu();
	}
	setReadOnly( shuffle, 'seed', rand.seed );
	setReadOnly( shuffle, 'PRNG', rand );
 
	rand = rand.normalized;
 
	return shuffle;
 
	/**
	* Returns a random permutation of elements in `arr`.
	*
	* @private
	* @param {(ArrayLike|TypedArrayLike)} arr - array-like object to shuffle
	* @param {Options} [options] - function options
	* @param {string} [options.copy] - string indicating whether to return a copy (`deep`,`shallow` or `none`)
	* @throws {TypeError} first argument must be array-like
	* @throws {TypeError} options must be an object
	* @throws {TypeError} must provide valid options
	* @returns {ArrayLike} the shuffled array-like object
	*
	* @example
	* var data = [ 1, 2, 3 ];
	* var out = shuffle( data );
	* // e.g., returns [ 3, 1, 2 ]
	*
	* @example
	* var data = [ 1, 2, 3 ];
	* var out = shuffle( data, {
	*     'copy': 'none'
	* });
	* var bool = ( data === out );
	* // returns true
	*/
	function shuffle( arr, options ) {
		var strflg;
		var level;
		var copy;
		var opts;
		var err;
		var out;
		var tmp;
		var N;
		var i;
		var j;
 
		if ( !( isArrayLike( arr ) || isTypedArrayLike( arr ) ) ) {
			throw new TypeError( format( 'invalid argument. First argument must be array-like. Value: `%s`.', arr ) );
		}
		if ( arguments.length > 1 ) {
			opts = {};
			err = validate( opts, options );
			if ( err ) {
				throw err;
			}
		}
		copy = ( opts && opts.copy ) ? opts.copy : conf.copy;
 
		strflg = isString( arr );
		if ( strflg ) {
			arr = arr.split( '' );
			copy = 'none';
		}
 
		level = 0;
		if ( copy === 'shallow' ) {
			level += 1;
		} else if ( copy === 'deep' ) {
			level += 2;
		}
		N = arr.length;
		out = deepCopy( arr, level );
 
		// Note: we skip the first element, as no further swaps are possible given that all other indices are excluded from swapping...
		for ( i = N - 1; i > 0; i-- ) {
			// Generate an integer index on the interval [0,i]:
			j = floor( rand() * (i+1.0) );
 
			// Swap elements:
			tmp = out[ i ];
			out[ i ] = out[ j ];
			out[ j ] = tmp;
		}
 
		if ( strflg ) {
			out = arr.join( '' );
		}
		return out;
	}
}
 
 
// EXPORTS //
 
module.exports = factory;