time-to-botec

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

main.js (4188B)


      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 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 iteratorSymbol = require( '@stdlib/symbol/iterator' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Returns an iterator which iterates from right to left over each element in an array-like object.
     33 *
     34 * ## Notes
     35 *
     36 * -   For dynamic array resizing, the only behavior made intentionally consistent with iterating from left to right is when elements are pushed onto the beginning (end) of an array. In other words, iterating from left to right combined with `[].push()` is consistent with iterating from right to left combined with `[].unshift()`.
     37 *
     38 * @param {Collection} src - input value
     39 * @param {Function} [mapFcn] - function to invoke for each iterated value
     40 * @param {*} [thisArg] - execution context
     41 * @throws {TypeError} first argument must be an array-like object
     42 * @throws {TypeError} second argument must be a function
     43 * @returns {Iterator} iterator
     44 *
     45 * @example
     46 * var iter = array2iteratorRight( [ 1, 2, 3, 4 ] );
     47 *
     48 * var v = iter.next().value;
     49 * // returns 4
     50 *
     51 * v = iter.next().value;
     52 * // returns 3
     53 *
     54 * v = iter.next().value;
     55 * // returns 2
     56 *
     57 * // ...
     58 */
     59 function array2iteratorRight( src ) {
     60 	var thisArg;
     61 	var iter;
     62 	var FLG;
     63 	var fcn;
     64 	var len;
     65 	var i;
     66 	if ( !isCollection( src ) ) {
     67 		throw new TypeError( 'invalid argument. First argument must be an array-like object. Value: `' + src + '`.' );
     68 	}
     69 	if ( arguments.length > 1 ) {
     70 		fcn = arguments[ 1 ];
     71 		if ( !isFunction( fcn ) ) {
     72 			throw new TypeError( 'invalid argument. Second argument must be a function. Value: `' + fcn + '`.' );
     73 		}
     74 		thisArg = arguments[ 2 ];
     75 	}
     76 	len = src.length;
     77 	i = len;
     78 
     79 	// Create an iterator protocol-compliant object:
     80 	iter = {};
     81 	if ( fcn ) {
     82 		setReadOnly( iter, 'next', next1 );
     83 	} else {
     84 		setReadOnly( iter, 'next', next2 );
     85 	}
     86 	setReadOnly( iter, 'return', end );
     87 
     88 	// If an environment supports `Symbol.iterator`, make the iterator iterable:
     89 	if ( iteratorSymbol ) {
     90 		setReadOnly( iter, iteratorSymbol, factory );
     91 	}
     92 	return iter;
     93 
     94 	/**
     95 	* Returns an iterator protocol-compliant object containing the next iterated value.
     96 	*
     97 	* @private
     98 	* @returns {Object} iterator protocol-compliant object
     99 	*/
    100 	function next1() {
    101 		i += src.length - len - 1; // accounts for a dynamic array
    102 		len = src.length;
    103 		if ( FLG || i < 0 ) {
    104 			FLG = true;
    105 			return {
    106 				'done': true
    107 			};
    108 		}
    109 		return {
    110 			'value': fcn.call( thisArg, src[ i ], i, src ),
    111 			'done': false
    112 		};
    113 	}
    114 
    115 	/**
    116 	* Returns an iterator protocol-compliant object containing the next iterated value.
    117 	*
    118 	* @private
    119 	* @returns {Object} iterator protocol-compliant object
    120 	*/
    121 	function next2() {
    122 		i += src.length - len - 1; // accounts for a dynamic array
    123 		len = src.length;
    124 		if ( FLG || i < 0 ) {
    125 			FLG = true;
    126 			return {
    127 				'done': true
    128 			};
    129 		}
    130 		return {
    131 			'value': src[ i ],
    132 			'done': false
    133 		};
    134 	}
    135 
    136 	/**
    137 	* Finishes an iterator.
    138 	*
    139 	* @private
    140 	* @param {*} [value] - value to return
    141 	* @returns {Object} iterator protocol-compliant object
    142 	*/
    143 	function end( value ) {
    144 		FLG = true;
    145 		if ( arguments.length ) {
    146 			return {
    147 				'value': value,
    148 				'done': true
    149 			};
    150 		}
    151 		return {
    152 			'done': true
    153 		};
    154 	}
    155 
    156 	/**
    157 	* Returns a new iterator.
    158 	*
    159 	* @private
    160 	* @returns {Iterator} iterator
    161 	*/
    162 	function factory() {
    163 		if ( fcn ) {
    164 			return array2iteratorRight( src, fcn, thisArg );
    165 		}
    166 		return array2iteratorRight( src );
    167 	}
    168 }
    169 
    170 
    171 // EXPORTS //
    172 
    173 module.exports = array2iteratorRight;