Skip to content

Commit d035aff

Browse files
committed
fixup! feat: add some method to array/fixed-endian-factory #3241
1 parent d22a6a9 commit d035aff

File tree

5 files changed

+107
-56
lines changed

5 files changed

+107
-56
lines changed

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

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -357,26 +357,6 @@ var bool = arr.every( isNegative );
357357
// returns true
358358
```
359359

360-
361-
<a name="method-some"></a>
362-
363-
#### TypedArrayFE.prototype.some( predicate\[, thisArg] )
364-
365-
Tests whether any element in an array pass a test implemented by a predicate function.
366-
367-
```javascript
368-
function isPositive( v ) {
369-
return v > 0;
370-
}
371-
372-
var Float64ArrayFE = fixedEndianFactory( 'float64' );
373-
374-
var arr = new Float64ArrayFE( 'little-endian', [ -1.0, 2.0, -3.0, -4.0 ] );
375-
376-
var bool = arr.some( isPositive );
377-
// returns true
378-
```
379-
380360
The invoked function is provided three arguments:
381361

382362
- **value**: current array element.
@@ -547,6 +527,52 @@ A few notes:
547527
- If a target array cannot accommodate all values (i.e., the length of source array plus `i` exceeds the target array length), the method throws an error.
548528
- If provided a typed array which shares an [`ArrayBuffer`][@stdlib/array/buffer] with the target array, the method will intelligently copy the source range to the destination range.
549529

530+
<a name="method-some"></a>
531+
532+
#### TypedArrayFE.prototype.some( predicate\[, thisArg] )
533+
534+
Tests whether at least one element in an array passes a test implemented by a predicate function.
535+
536+
```javascript
537+
function isPositive( v ) {
538+
return v > 0;
539+
}
540+
541+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
542+
var arr = new Float64ArrayFE( 'little-endian', [ -1.0, 2.0, -3.0, -4.0 ] );
543+
var bool = arr.some( isPositive );
544+
// returns true
545+
```
546+
547+
The invoked function is provided three arguments:
548+
549+
- **value**: current array element.
550+
- **index**: current array element index.
551+
- **arr**: the array on which this method was called.
552+
553+
To set the function execution context, provide a `thisArg`.
554+
555+
```javascript
556+
function isPositive( v, i ) {
557+
this.count += 1;
558+
return v > 0;
559+
}
560+
561+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
562+
563+
var arr = new Float64ArrayFE( 'little-endian', [ -1.0, -2.0, -3.0 ] );
564+
565+
var context = {
566+
'count': 0
567+
};
568+
569+
var bool = arr.some( isPositive, context );
570+
// returns false
571+
572+
var count = context.count;
573+
// returns 3
574+
```
575+
550576
<a name="method-to-string"></a>
551577

552578
#### TypedArrayFE.prototype.toString()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ bench( pkg+':some', function benchmark( b ) {
3838
var arr;
3939
var i;
4040

41-
arr = new Float64ArrayFE( 'little-endian', [ 1.0, -2.0, 2.0, 1.0 ] );
41+
arr = new Float64ArrayFE( 'little-endian', [ -1.0, -2.0, -2.0, 1.0 ] );
4242

4343
b.tic();
4444
for ( i = 0; i < b.iterations; i++ ) {

lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.some.length.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var Float64ArrayFE = factory( 'float64' );
4545
* @returns {boolean} boolean indicating whether a value passes a test
4646
*/
4747
function predicate( value ) {
48-
return value >= 1;
48+
return value < 0;
4949
}
5050

5151
/**
@@ -106,7 +106,7 @@ function main() {
106106
for ( i = min; i <= max; i++ ) {
107107
len = pow( 10, i );
108108
f = createBenchmark( len );
109-
bench( pkg+':every:len='+len, f );
109+
bench( pkg+':some:len='+len, f );
110110
}
111111
}
112112

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

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

572-
/**
573-
* Tests whether at least one element in the typed array passes the test implemented by the provided function.
574-
*
575-
* @name some
576-
* @memberof TypedArray.prototype
577-
* @type {Function}
578-
* @param {Function} fcn - 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 any elements pass a test
583-
*/
584-
setReadOnly( TypedArray.prototype, 'some', function some( fcn, 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( fcn ) ) {
592-
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
593-
}
594-
buf = this._buffer;
595-
for ( i = 0; i < this._length; i++ ) {
596-
if ( fcn.call( thisArg, buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ), i, this ) ) {
597-
return true;
598-
}
599-
}
600-
return false;
601-
});
602-
603572
/**
604573
* Invokes a function once for each array element.
605574
*
@@ -760,6 +729,37 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
760729
buf[ SETTER ]( idx*BYTES_PER_ELEMENT, value, this._isLE );
761730
});
762731

732+
/**
733+
* Tests whether at least one element in the typed array passes a test implemented by a predicate function.
734+
*
735+
* @name some
736+
* @memberof TypedArray.prototype
737+
* @type {Function}
738+
* @param {Function} predicate - predicate function
739+
* @param {*} [thisArg] - function invocation context
740+
* @throws {TypeError} `this` must be a typed array instance
741+
* @throws {TypeError} first argument must be a function
742+
* @returns {boolean} boolean indicating whether at least one element passes a test
743+
*/
744+
setReadOnly( TypedArray.prototype, 'some', function some( predicate, thisArg ) {
745+
var buf;
746+
var i;
747+
748+
if ( !isTypedArray( this ) ) {
749+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
750+
}
751+
if ( !isFunction( predicate ) ) {
752+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
753+
}
754+
buf = this._buffer;
755+
for ( i = 0; i < this._length; i++ ) {
756+
if ( predicate.call( thisArg, buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ), i, this ) ) {
757+
return true;
758+
}
759+
}
760+
return false;
761+
});
762+
763763
/**
764764
* Serializes an array as a string.
765765
*

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ tape( 'the function returns a function', function test( t ) {
4040
t.end();
4141
});
4242

43-
tape( 'attached to the prototype of the returned function is an `some` method', function test( t ) {
43+
tape( 'attached to the prototype of the returned function is a `some` method', function test( t ) {
4444
var ctor = factory( 'float64' );
4545
t.strictEqual( hasOwnProp( ctor.prototype, 'some' ), true, 'returns expected value' );
4646
t.strictEqual( isFunction( ctor.prototype.some ), true, 'returns expected value' );
@@ -132,7 +132,7 @@ tape( 'the method returns `false` if operating on an empty array', function test
132132
}
133133
});
134134

135-
tape( 'the method returns `true` if any element pass a test', function test( t ) {
135+
tape( 'the method returns `true` if at least one element passes a test', function test( t ) {
136136
var bool;
137137
var ctor;
138138
var arr;
@@ -166,6 +166,31 @@ tape( 'the method returns `false` if all elements fail a test', function test( t
166166
}
167167
});
168168

169+
tape( 'the method stops executing upon encountering the first element which passes a test', function test( t ) {
170+
var bool;
171+
var ctor;
172+
var arr;
173+
var ctx;
174+
175+
ctx = {
176+
'count': 0
177+
};
178+
179+
ctor = factory( 'float64' );
180+
arr = new ctor( 'little-endian', [ -1.0, -2.0, -3.0, 4.0, -5.0 ] );
181+
bool = arr.some( predicate, ctx );
182+
183+
t.strictEqual( bool, true, 'returns expected value' );
184+
t.strictEqual( ctx.count, 4, 'returns expected value' );
185+
186+
t.end();
187+
188+
function predicate( v ) {
189+
this.count += 1; // eslint-disable-line no-invalid-this
190+
return v > 0;
191+
}
192+
});
193+
169194
tape( 'the method supports providing an execution context', function test( t ) {
170195
var bool;
171196
var ctor;

0 commit comments

Comments
 (0)