time-to-botec

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

main.js (5392B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2019 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 setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
     24 var isFunction = require( '@stdlib/assert/is-function' );
     25 var isCollection = require( '@stdlib/assert/is-collection' );
     26 var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
     27 var iteratorSymbol = require( '@stdlib/symbol/iterator' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Returns an iterator which iterates from right to left over each element in an array-like object view.
     34 *
     35 * @param {Collection} src - input value
     36 * @param {integer} [begin=0] - starting **view** index (inclusive)
     37 * @param {integer} [end=src.length] - ending **view** index (non-inclusive)
     38 * @param {Function} [mapFcn] - function to invoke for each iterated value
     39 * @param {*} [thisArg] - execution context
     40 * @throws {TypeError} first argument must be an array-like object
     41 * @throws {TypeError} second argument must be either an integer (starting index) or a function
     42 * @throws {TypeError} third argument must be either an integer (ending index) or a function
     43 * @throws {TypeError} fourth argument must be a function
     44 * @returns {Iterator} iterator
     45 *
     46 * @example
     47 * var iter = arrayview2iteratorRight( [ 1, 2, 3, 4 ], 1, 3 );
     48 *
     49 * var v = iter.next().value;
     50 * // returns 3
     51 *
     52 * v = iter.next().value;
     53 * // returns 2
     54 *
     55 * var bool = iter.next().done;
     56 * // returns true
     57 */
     58 function arrayview2iteratorRight( src ) {
     59 	var thisArg;
     60 	var begin;
     61 	var nargs;
     62 	var iter;
     63 	var FLG;
     64 	var fcn;
     65 	var end;
     66 	var i;
     67 	if ( !isCollection( src ) ) {
     68 		throw new TypeError( 'invalid argument. First argument must be an array-like object. Value: `' + src + '`.' );
     69 	}
     70 	nargs = arguments.length;
     71 	if ( nargs === 1 ) {
     72 		begin = 0;
     73 		end = src.length;
     74 	} else if ( nargs === 2 ) {
     75 		if ( isFunction( arguments[ 1 ] ) ) {
     76 			begin = 0;
     77 			fcn = arguments[ 1 ];
     78 		} else {
     79 			begin = arguments[ 1 ];
     80 		}
     81 		end = src.length;
     82 	} else if ( nargs === 3 ) {
     83 		if ( isFunction( arguments[ 1 ] ) ) {
     84 			begin = 0;
     85 			end = src.length;
     86 			fcn = arguments[ 1 ];
     87 			thisArg = arguments[ 2 ];
     88 		} else if ( isFunction( arguments[ 2 ] ) ) {
     89 			begin = arguments[ 1 ];
     90 			end = src.length;
     91 			fcn = arguments[ 2 ];
     92 		} else {
     93 			begin = arguments[ 1 ];
     94 			end = arguments[ 2 ];
     95 		}
     96 	} else { // nargs >= 4
     97 		begin = arguments[ 1 ];
     98 		end = arguments[ 2 ];
     99 		fcn = arguments[ 3 ];
    100 		if ( !isFunction( fcn ) ) {
    101 			throw new TypeError( 'invalid argument. Fourth argument must be a function. Value: `' + fcn + '`.' );
    102 		}
    103 		thisArg = arguments[ 4 ];
    104 	}
    105 	if ( !isInteger( begin ) ) {
    106 		throw new TypeError( 'invalid argument. Second argument must be either an integer (starting vie windex) or a callback function. Value: `' + begin + '`.' );
    107 	}
    108 	if ( !isInteger( end ) ) {
    109 		throw new TypeError( 'invalid argument. Third argument must be either an integer (ending view index) or a callback function. Value: `' + end + '`.' );
    110 	}
    111 	if ( end < 0 ) {
    112 		end = src.length + end;
    113 		if ( end < 0 ) {
    114 			end = 0;
    115 		}
    116 	} else if ( end > src.length ) {
    117 		end = src.length;
    118 	}
    119 	if ( begin < 0 ) {
    120 		begin = src.length + begin;
    121 		if ( begin < 0 ) {
    122 			begin = 0;
    123 		}
    124 	}
    125 	i = end;
    126 
    127 	// Create an iterator protocol-compliant object:
    128 	iter = {};
    129 	if ( fcn ) {
    130 		setReadOnly( iter, 'next', next1 );
    131 	} else {
    132 		setReadOnly( iter, 'next', next2 );
    133 	}
    134 	setReadOnly( iter, 'return', finish );
    135 
    136 	// If an environment supports `Symbol.iterator`, make the iterator iterable:
    137 	if ( iteratorSymbol ) {
    138 		setReadOnly( iter, iteratorSymbol, factory );
    139 	}
    140 	return iter;
    141 
    142 	/**
    143 	* Returns an iterator protocol-compliant object containing the next iterated value.
    144 	*
    145 	* @private
    146 	* @returns {Object} iterator protocol-compliant object
    147 	*/
    148 	function next1() {
    149 		i -= 1;
    150 		if ( FLG || i < begin ) {
    151 			return {
    152 				'done': true
    153 			};
    154 		}
    155 		return {
    156 			'value': fcn.call( thisArg, src[ i ], i, end-i-1, src ),
    157 			'done': false
    158 		};
    159 	}
    160 
    161 	/**
    162 	* Returns an iterator protocol-compliant object containing the next iterated value.
    163 	*
    164 	* @private
    165 	* @returns {Object} iterator protocol-compliant object
    166 	*/
    167 	function next2() {
    168 		i -= 1;
    169 		if ( FLG || i < begin ) {
    170 			return {
    171 				'done': true
    172 			};
    173 		}
    174 		return {
    175 			'value': src[ i ],
    176 			'done': false
    177 		};
    178 	}
    179 
    180 	/**
    181 	* Finishes an iterator.
    182 	*
    183 	* @private
    184 	* @param {*} [value] - value to return
    185 	* @returns {Object} iterator protocol-compliant object
    186 	*/
    187 	function finish( value ) {
    188 		FLG = true;
    189 		if ( arguments.length ) {
    190 			return {
    191 				'value': value,
    192 				'done': true
    193 			};
    194 		}
    195 		return {
    196 			'done': true
    197 		};
    198 	}
    199 
    200 	/**
    201 	* Returns a new iterator.
    202 	*
    203 	* @private
    204 	* @returns {Iterator} iterator
    205 	*/
    206 	function factory() {
    207 		if ( fcn ) {
    208 			return arrayview2iteratorRight( src, begin, end, fcn, thisArg );
    209 		}
    210 		return arrayview2iteratorRight( src, begin, end );
    211 	}
    212 }
    213 
    214 
    215 // EXPORTS //
    216 
    217 module.exports = arrayview2iteratorRight;