Skip to content

Commit 5ab8479

Browse files
refactor: put findLast function into correct order
1 parent d6b5968 commit 5ab8479

File tree

2 files changed

+82
-82
lines changed

2 files changed

+82
-82
lines changed

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

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,54 @@ var count = context.count;
450450
// returns 4
451451
```
452452

453+
<a name="method-find-last"></a>
454+
455+
#### TypedArray.prototype.findLast( predicate\[, thisArg] )
456+
457+
Returns the last element in an array for which a predicate function returns a truthy value.
458+
459+
```javascript
460+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
461+
462+
function predicate( element ) {
463+
return element < 0.0;
464+
}
465+
466+
var arr = new Float64ArrayFE( 'little-endian', [ -1.0, 2.0, -4.0, 3.0 ] );
467+
468+
var res = arr.findLast( predicate );
469+
// returns -4.0
470+
```
471+
472+
The `predicate` function is provided three arguments:
473+
474+
- **value**: current array element.
475+
- **index**: current array element index.
476+
- **arr**: the array on which this method was called.
477+
478+
To set the function execution context, provide a `thisArg`
479+
480+
```javascript
481+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
482+
483+
function predicate( v ) {
484+
this.count += 1;
485+
return ( v < 0.0 );
486+
}
487+
488+
var arr = new Float64ArrayFE( 'little-endian', [ -1.0, 2.0, 3.0 ] );
489+
490+
var context = {
491+
'count': 0
492+
};
493+
494+
var z = arr.findLast( predicate, context );
495+
// returns -1.0
496+
497+
var count = context.count;
498+
// returns 3
499+
```
500+
453501
<a name="method-for-each"></a>
454502

455503
#### TypedArrayFE.prototype.forEach( callbackFn\[, thisArg] )
@@ -919,59 +967,11 @@ var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );
919967

920968
var out = arr.with( 0, 0.0 );
921969
// returns <Float64ArrayFE>
922-
970+
923971
var v = out.get( 0 );
924972
// returns 0.0
925973
```
926974

927-
<a name="method-find-last"></a>
928-
929-
#### TypedArray.prototype.findLast( predicate\[, thisArg] )
930-
931-
Returns the last element in an array for which a predicate function returns a truthy value.
932-
933-
```javascript
934-
var Float64ArrayFE = fixedEndianFactory( 'float64' );
935-
936-
function predicate( element ) {
937-
return element < 0.0;
938-
}
939-
940-
var arr = new Float64ArrayFE( 'little-endian', [ -1.0, 2.0, -4.0, 3.0 ] );
941-
942-
var res = arr.findLast( predicate );
943-
// returns -4.0
944-
```
945-
946-
The `predicate` function is provided three arguments:
947-
948-
- **value**: current array element.
949-
- **index**: current array element index.
950-
- **arr**: the array on which this method was called.
951-
952-
To set the function execution context, provide a `thisArg`
953-
954-
```javascript
955-
var Float64ArrayFE = fixedEndianFactory( 'float64' );
956-
957-
function predicate( v ) {
958-
this.count += 1;
959-
return ( v < 0.0 );
960-
}
961-
962-
var arr = new Float64ArrayFE( 'little-endian', [ -1.0, 2.0, 3.0 ] );
963-
964-
var context = {
965-
'count': 0
966-
};
967-
968-
var z = arr.findLast( predicate, context );
969-
// returns -1.0
970-
971-
var count = context.count;
972-
// returns 3
973-
```
974-
975975
</section>
976976

977977
<!-- /.usage -->

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

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,39 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
617617
return new this.constructor( flag2byteOrder( this._isLE ), out );
618618
});
619619

620+
/**
621+
* Returns the last element in an array for which a predicate function returns a truthy value.
622+
*
623+
* @private
624+
* @name findLast
625+
* @memberof TypedArray.prototype
626+
* @type {Function}
627+
* @param {Function} predicate - test function
628+
* @param {*} [thisArg] - predicate function execution context
629+
* @throws {TypeError} `this` must be a typed array
630+
* @throws {TypeError} first argument must be a function
631+
* @returns {(*|void)} array element or undefined
632+
*/
633+
setReadOnly( TypedArray.prototype, 'findLast', function findLast( predicate, thisArg ) {
634+
var buf;
635+
var v;
636+
var i;
637+
638+
if ( !isTypedArray( this ) ) {
639+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
640+
}
641+
if ( !isFunction( predicate ) ) {
642+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
643+
}
644+
buf = this._buffer;
645+
for ( i = this._length-1; i >= 0; i-- ) {
646+
v = buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE );
647+
if ( predicate.call( thisArg, v, i, this ) ) {
648+
return v;
649+
}
650+
}
651+
});
652+
620653
/**
621654
* Invokes a function once for each array element.
622655
*
@@ -1072,39 +1105,6 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
10721105
return out.join( ',' );
10731106
});
10741107

1075-
/**
1076-
* Returns the last element in an array for which a predicate function returns a truthy value.
1077-
*
1078-
* @private
1079-
* @name findLast
1080-
* @memberof TypedArray.prototype
1081-
* @type {Function}
1082-
* @param {Function} predicate - test function
1083-
* @param {*} [thisArg] - predicate function execution context
1084-
* @throws {TypeError} `this` must be a typed array
1085-
* @throws {TypeError} first argument must be a function
1086-
* @returns {(*|void)} array element or undefined
1087-
*/
1088-
setReadOnly( TypedArray.prototype, 'findLast', function findLast( predicate, thisArg ) {
1089-
var buf;
1090-
var v;
1091-
var i;
1092-
1093-
if ( !isTypedArray( this ) ) {
1094-
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
1095-
}
1096-
if ( !isFunction( predicate ) ) {
1097-
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
1098-
}
1099-
buf = this._buffer;
1100-
for ( i = this._length-1; i >= 0; i-- ) {
1101-
v = buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE );
1102-
if ( predicate.call( thisArg, v, i, this ) ) {
1103-
return v;
1104-
}
1105-
}
1106-
});
1107-
11081108
/**
11091109
* Returns a new typed array with the element at a provided index replaced with a provided value.
11101110
*

0 commit comments

Comments
 (0)