time-to-botec

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

reorder_arguments.js (2457B)


      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 isFunction = require( '@stdlib/assert/is-function' );
     24 var isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Returns a function that invokes a provided function with reordered arguments.
     31 *
     32 * @param {Function} fcn - input function
     33 * @param {NonNegativeIntegerArray} indices - argument indices
     34 * @param {*} [thisArg] - function context
     35 * @throws {TypeError} first argument must be a function
     36 * @throws {TypeError} second argument must be an array of nonnegative integers
     37 * @returns {Function} function with reordered arguments
     38 *
     39 * @example
     40 * function foo( a, b, c ) {
     41 *     return [ a, b, c ];
     42 * }
     43 *
     44 * var bar = reorderArguments( foo, [ 2, 0, 1 ] );
     45 *
     46 * var out = bar( 1, 2, 3 );
     47 * // returns [ 3, 1, 2 ]
     48 */
     49 function reorderArguments( fcn, indices, thisArg ) {
     50 	if ( !isFunction( fcn ) ) {
     51 		throw new TypeError( 'invalid argument. First argument must be a function. Value: `'+fcn+'`.' );
     52 	}
     53 	if ( !isNonNegativeIntegerArray( indices ) ) {
     54 		throw new TypeError( 'invalid argument. Second argument must be an array containing only nonnegative integers. Value: `'+indices+'`.' );
     55 	}
     56 	return reordered;
     57 
     58 	/**
     59 	* Invokes a function with reordered arguments.
     60 	*
     61 	* @private
     62 	* @param {...*} args - arguments
     63 	* @throws {Error} must provide expected number of input arguments
     64 	* @returns {*} return value
     65 	*/
     66 	function reordered() {
     67 		var args;
     68 		var len;
     69 		var i;
     70 
     71 		len = arguments.length;
     72 		if ( len !== indices.length ) {
     73 			throw new Error( 'invalid invocation. Unexpected number of input arguments. Expected: '+indices.length+'. Actual: '+len+'.' );
     74 		}
     75 		args = new Array( len );
     76 		for ( i = 0; i < len; i++ ) {
     77 			args[ i ] = arguments[ indices[i] ];
     78 		}
     79 		return fcn.apply( thisArg, args );
     80 	}
     81 }
     82 
     83 
     84 // EXPORTS //
     85 
     86 module.exports = reorderArguments;