Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// MODULES //
var isFunction = require( '@stdlib/assert/is-function' );
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var isObject = require( '@stdlib/assert/is-plain-object' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var isCollection = require( '@stdlib/assert/is-collection' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var indexOf = require( '@stdlib/utils/index-of' );
var format = require( '@stdlib/string/format' );
// VARIABLES //
var RETURNS = [ 'values', 'indices', '*' ];
// MAIN //
/**
* Finds elements in an array-like object that satisfy a test condition.
*
* @param {(Collection|string)} arr - object from which elements will be tested
* @param {Options} [options] - function options
* @param {integer} [options.k=arr.length] - limits the number of returned elements
* @param {string} [options.returns='indices'] - if `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned
* @param {Function} clbk - function invoked for each array element. If the return value is truthy, the value is considered to have satisfied the test condition.
* @throws {TypeError} first argument must be an array-like object
* @throws {TypeError} options argument must be an object
* @throws {TypeError} last argument must be a function
* @throws {TypeError} `options.k` must be an integer
* @throws {TypeError} `options.returns` must be a string equal to `values`, `indices` or `*`
* @returns {Array} array of indices, element values, or arrays of index-value pairs
*
* @example
* var data = [ 30, 20, 50, 60, 10 ];
* var vals = find( data, condition );
* // returns [ 0, 2, 3 ]
*
* function condition( val ) {
* return val > 20;
* }
*
* @example
* var data = [ 30, 20, 50, 60, 10 ];
* var opts = {
* 'k': 2,
* 'returns': 'values'
* };
* var vals = find( data, opts, condition );
* // returns [ 30, 50 ]
*
* function condition( val ) {
* return val > 20;
* }
*
* @example
* var data = [ 30, 20, 50, 60, 10 ];
* var vals = find( data, condition );
* // returns []
*
* function condition( val ) {
* return val > 1000;
* }
*
* @example
* var data = [ 30, 20, 50, 60, 10 ];
* var opts = {
* 'k': -2,
* 'returns': 'values'
* };
* var vals = find( data, opts, condition );
* // returns [ 60, 50 ]
*
* function condition( val ) {
* return val > 20;
* }
*
* @example
* var data = [ 30, 20, 50, 60, 10 ];
* var opts = {
* 'k': -2,
* 'returns': '*'
* };
* var vals = find( data, opts, condition );
* // returns [ [3, 60], [2, 50] ]
*
* function condition( val ) {
* return val > 20;
* }
*/
function find( arr, options, clbk ) { // eslint-disable-line stdlib/no-redeclare
var count;
var mode;
var opts;
var len;
var out;
var ret;
var cb;
var i;
var k;
var v;
mode = 0;
if ( !isCollection( arr ) && !isString( arr ) ) {
throw new TypeError( format( 'invalid argument. Must provide an array-like object. Value: `%s`.', arr ) );
}
len = arr.length;
if ( arguments.length < 3 ) {
opts = {};
cb = options;
} else {
opts = options;
cb = clbk;
}
if ( !isFunction( cb ) ) {
throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) );
}
if ( !isObject( opts ) ) {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', opts ) );
}
if ( hasOwnProp( opts, 'k' ) ) {
k = opts.k;
if ( !isInteger( k ) ) {
throw new TypeError( format( 'invalid option. `%s` option must be an integer. Option: `%s`.', 'k', k ) );
}
} else {
k = len;
}
if ( hasOwnProp( opts, 'returns' ) ) {
ret = opts.returns;
if ( !isString( ret ) || indexOf( RETURNS, ret ) === -1 ) {
throw new TypeError( format( 'invalid option. `%s` option must be one of the following: "%s". Option: `%s`.', 'returns', RETURNS.join( '", "' ), ret ) );
}
if ( ret === 'values' ) {
mode = 1;
} else if ( ret === '*' ) {
mode = 2;
}
}
out = [];
count = 0;
if ( k === 0 ) {
return out;
}
if ( k > 0 ) {
// Search moving from begin-to-end [0,1,...]:
for ( i = 0; i < len; i++ ) {
v = arr[ i ];
if ( cb( v, i, arr ) ) { // eslint-disable-line node/callback-return
if ( mode === 2 ) {
out.push( [ i, v ] );
} else if ( mode === 1 ) {
out.push( v );
} else {
out.push( i );
}
count += 1;
if ( count === k ) {
break;
}
}
}
return out;
}
// Search moving from end-to-begin [...,2,1,0]:
k = -k;
for ( i = len-1; i >= 0; i-- ) {
v = arr[ i ];
if ( cb( v, i, arr ) ) { // eslint-disable-line node/callback-return
if ( mode === 2 ) {
out.push( [ i, v ] );
} else if ( mode === 1 ) {
out.push( v );
} else {
out.push( i );
}
count += 1;
if ( count === k ) {
break;
}
}
}
return out;
}
// EXPORTS //
module.exports = find;
|