time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

main.js (2540B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     24 var isObject = require( '@stdlib/assert/is-plain-object' );
     25 var isArray = require( '@stdlib/assert/is-array' );
     26 var contains = require( '@stdlib/assert/contains' );
     27 var Int32Array = require( '@stdlib/array/int32' );
     28 var Uint32Array = require( '@stdlib/array/uint32' );
     29 var table = require( './prngs.js' );
     30 
     31 
     32 // VARIABLES //
     33 
     34 var TYPED_ARRAY_CTORS = {
     35 	'Int32Array': Int32Array,
     36 	'Uint32Array': Uint32Array
     37 };
     38 var PRNG_WRAPPERS = [ 'randi', 'randn', 'randu' ];
     39 
     40 
     41 // MAIN //
     42 
     43 /**
     44 * Revives a JSON-serialized pseudorandom number generator.
     45 *
     46 * @param {string} key - key
     47 * @param {*} value - value
     48 * @returns {(*|Function)} value or PRNG
     49 *
     50 * @example
     51 * var parseJSON = require( '@stdlib/utils/parse-json' );
     52 * var mt19937 = require( '@stdlib/random/base/mt19937' );
     53 *
     54 * var str = JSON.stringify( mt19937 );
     55 * var rand = parseJSON( str, reviver );
     56 * // returns <Function>
     57 */
     58 function reviver( key, value ) {
     59 	var factory;
     60 	var opts;
     61 	var args;
     62 	var ctor;
     63 	var tmp;
     64 	if (
     65 		value &&
     66 		value.type === 'PRNG' &&
     67 		isString( value.name ) &&
     68 		isObject( value.state ) &&
     69 		isArray( value.params ) &&
     70 		isString( value.state.type ) &&
     71 		isArray( value.state.data )
     72 	) {
     73 		opts = {};
     74 		factory = table[ value.name ];
     75 		if ( factory === void 0 ) {
     76 			tmp = value.name.split( '-' );
     77 			if ( contains( PRNG_WRAPPERS, tmp[ 0 ] ) ) {
     78 				factory = table[ tmp[ 0 ] ];
     79 				opts.name = tmp.slice( 1 ).join( '-' );
     80 			}
     81 		}
     82 		if ( factory ) {
     83 			ctor = TYPED_ARRAY_CTORS[ value.state.type ];
     84 			if ( ctor ) {
     85 				opts.state = new ctor( value.state.data );
     86 
     87 				args = value.params.slice();
     88 				args.push( opts );
     89 
     90 				try {
     91 					return factory.apply( null, args );
     92 				} catch ( error ) { // eslint-disable-line no-unused-vars
     93 					// Return the original JSON value...
     94 				}
     95 			}
     96 		}
     97 	}
     98 	return value;
     99 }
    100 
    101 
    102 // EXPORTS //
    103 
    104 module.exports = reviver;