Skip to content

Commit 3e505ad

Browse files
aayush0325kgryte
authored andcommitted
feat: add every method to array/fixed-endian-factory
PR-URL: #3200 Closes: #3138 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 3b3d051 commit 3e505ad

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

lib/node_modules/@stdlib/array/fixed-endian-factory/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,58 @@ var count = context.count;
390390
// returns 3
391391
```
392392

393+
<a name="method-every"></a>
394+
395+
#### TypedArrayFE.prototype.findIndex( callbackFn\[, thisArg] )
396+
397+
Tests whether all the elements in an array pass a test implemented by a predicate function.
398+
399+
```javascript
400+
function greaterthantwo( v ) {
401+
return ( v > 2.0 );
402+
}
403+
404+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
405+
406+
var arr = new Float64ArrayFE( 'little-endian', 3 );
407+
408+
arr.set( 1.5, 0 );
409+
arr.set( 2.5, 1 );
410+
arr.set( 3.5, 2 );
411+
412+
var bool = arr.every( isNegative );
413+
// returns true
414+
```
415+
416+
The invoked function is provided three arguments:
417+
418+
- **value**: current array element.
419+
- **index**: current array element index.
420+
- **arr**: the array on which this method was called.
421+
422+
To set the function execution context, provide a `thisArg`.
423+
424+
```javascript
425+
function isPositive( v, i ) {
426+
this.count += 1;
427+
return v > 0;
428+
}
429+
430+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
431+
432+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, -3.0 ] );
433+
434+
var context = {
435+
'count': 0
436+
};
437+
438+
var bool = arr.every( isPositive, context );
439+
// returns false
440+
441+
var count = context.count;
442+
// returns 3
443+
```
444+
393445
<a name="method-for-each"></a>
394446

395447
#### TypedArrayFE.prototype.forEach( callbackFn\[, thisArg] )

lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,68 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
569569
return true;
570570
});
571571

572+
/**
573+
* Tests whether all elements in an array pass a test implemented by a predicate function.
574+
*
575+
* @name every
576+
* @memberof TypedArray.prototype
577+
* @type {Function}
578+
* @param {Function} predicate - predicate function
579+
* @param {*} [thisArg] - function invocation context
580+
* @throws {TypeError} `this` must be a typed array instance
581+
* @throws {TypeError} first argument must be a function
582+
* @returns {boolean} boolean indicating whether all elements pass a test
583+
*/
584+
setReadOnly( TypedArray.prototype, 'every', function every( predicate, thisArg ) {
585+
var buf;
586+
var i;
587+
588+
if ( !isTypedArray( this ) ) {
589+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
590+
}
591+
if ( !isFunction( predicate ) ) {
592+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
593+
}
594+
buf = this._buffer;
595+
for ( i = 0; i < this._length; i++ ) {
596+
if ( !predicate.call( thisArg, buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ), i, this ) ) {
597+
return false;
598+
}
599+
}
600+
return true;
601+
});
602+
603+
/**
604+
* Tests whether all elements in an array pass a test implemented by a predicate function.
605+
*
606+
* @name every
607+
* @memberof TypedArray.prototype
608+
* @type {Function}
609+
* @param {Function} predicate - predicate function
610+
* @param {*} [thisArg] - function invocation context
611+
* @throws {TypeError} `this` must be a typed array instance
612+
* @throws {TypeError} first argument must be a function
613+
* @returns {boolean} boolean indicating whether all elements pass a test
614+
*/
615+
setReadOnly( TypedArray.prototype, 'every', function every( predicate, thisArg ) {
616+
var buf;
617+
var i;
618+
619+
if ( !isTypedArray( this ) ) {
620+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
621+
}
622+
if ( !isFunction( predicate ) ) {
623+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
624+
}
625+
buf = this._buffer;
626+
for ( i = 0; i < this._length; i++ ) {
627+
if ( !predicate.call( thisArg, buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ), i, this ) ) {
628+
return false;
629+
}
630+
}
631+
return true;
632+
});
633+
572634
/**
573635
* Invokes a function once for each array element.
574636
*

0 commit comments

Comments
 (0)