do_until_each_right.js (3339B)
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 isCollection = require( '@stdlib/assert/is-collection' ); 24 var isFunction = require( '@stdlib/assert/is-function' ); 25 26 27 // MAIN // 28 29 /** 30 * Until a test condition is true, invokes a function once for each element in a collection, iterating from right to left. 31 * 32 * ## Notes 33 * 34 * - For dynamic array resizing, the only behavior made intentionally consistent with `doUntilEach` (iterating from left to right) is when elements are pushed onto the beginning (end) of an array. In other words, for `doUntilEach()`, `[].push()` behavior is consistent with `doUntilEachRight()` `[].unshift()` behavior. 35 * - The condition is evaluated **after** executing the function to invoke; thus, the provided function **always** executes at least once. 36 * - If provided an empty collection, the function invokes the provided function with the collection index set to `undefined`. 37 * 38 * 39 * @param {Collection} collection - input collection 40 * @param {Function} fcn - function to invoke 41 * @param {Function} predicate - function which indicates whether to stop iterating over a collection 42 * @param {*} [thisArg] - execution context for the applied function 43 * @throws {TypeError} first argument must be a collection 44 * @throws {TypeError} second argument must be a function 45 * @throws {TypeError} third argument must be a function 46 * @returns {Collection} input collection 47 * 48 * @example 49 * function predicate( v, index, collection ) { 50 * return ( v !== v ); 51 * } 52 * 53 * function log( v, index, collection ) { 54 * console.log( '%s: %d', index, v ); 55 * } 56 * 57 * var arr = [ 1, NaN, 2, 3, 4, 5 ]; 58 * 59 * doUntilEachRight( arr, log, predicate ); 60 */ 61 function doUntilEachRight( collection, fcn, predicate, thisArg ) { 62 var len; 63 var i; 64 if ( !isCollection( collection ) ) { 65 throw new TypeError( 'invalid argument. First argument must be a collection. Value: `'+collection+'`.' ); 66 } 67 if ( !isFunction( fcn ) ) { 68 throw new TypeError( 'invalid argument. Second argument must be a function. Value: `'+fcn+'`.' ); 69 } 70 if ( !isFunction( predicate ) ) { 71 throw new TypeError( 'invalid argument. Third argument must be a function. Value: `'+predicate+'`.' ); 72 } 73 len = collection.length; 74 if ( len === 0 ) { 75 fcn.call( thisArg, void 0, void 0, collection ); 76 len = collection.length; 77 if ( len === 0 ) { 78 return collection; 79 } 80 } 81 i = len - 1; 82 do { 83 fcn.call( thisArg, collection[ i ], i, collection ); 84 85 // Account for dynamically resizing a collection... 86 if ( len !== collection.length ) { 87 i += ( collection.length - len ); 88 len = collection.length; 89 } 90 i -= 1; 91 } while ( 92 i >= 0 && 93 !predicate( collection[ i+1 ], i+1, collection ) 94 ); 95 return collection; 96 } 97 98 99 // EXPORTS // 100 101 module.exports = doUntilEachRight;