Skip to content

Commit e8a99f5

Browse files
committed
feat: add every method to array/fixed-endian-factory
1 parent 35f0975 commit e8a99f5

File tree

3 files changed

+271
-0
lines changed

3 files changed

+271
-0
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,55 @@ v = arr.at( -100 );
338338
// returns undefined
339339
```
340340

341+
<a name="method-every"></a>
342+
343+
#### TypedArrayFE.prototype.every( predicate\[, thisArg] )
344+
345+
Tests whether all the elements in an array pass a test implemented by a predicate function.
346+
347+
```javascript
348+
function isNegative( v ) {
349+
return v < 0;
350+
}
351+
352+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
353+
354+
var arr = new Float64ArrayFE( 'little-endian', [ -1.0, -2.0, -3.0, -4.0 ] );
355+
356+
var bool = arr.every( isNegative );
357+
// returns true
358+
```
359+
360+
The invoked function is provided three arguments:
361+
362+
- **value**: current array element.
363+
- **index**: current array element index.
364+
- **arr**: the array on which this method was called.
365+
366+
To set the function execution context, provide a `thisArg`.
367+
368+
```javascript
369+
function isPositive( v, i ) {
370+
this.count += 1;
371+
return v > 0;
372+
}
373+
374+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
375+
376+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, -3.0 ] );
377+
378+
var context = {
379+
'count': 0
380+
};
381+
382+
var bool = arr.every( isPositive, context );
383+
// returns false
384+
385+
var count = context.count;
386+
// returns 3
387+
```
388+
389+
341390
<a name="method-for-each"></a>
342391

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

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,37 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
514514
return this._buffer.byteLength;
515515
});
516516

517+
/**
518+
* Tests whether all elements in an array pass a test implemented by a predicate function.
519+
*
520+
* @name every
521+
* @memberof TypedArray.prototype
522+
* @type {Function}
523+
* @param {Function} predicate - predicate function
524+
* @param {*} [thisArg] - function invocation context
525+
* @throws {TypeError} `this` must be a typed array instance
526+
* @throws {TypeError} first argument must be a function
527+
* @returns {boolean} boolean indicating whether all elements pass a test
528+
*/
529+
setReadOnly( TypedArray.prototype, 'every', function every( predicate, thisArg ) {
530+
var buf;
531+
var i;
532+
533+
if ( !isTypedArray( this ) ) {
534+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
535+
}
536+
if ( !isFunction( predicate ) ) {
537+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
538+
}
539+
buf = this._buffer;
540+
for ( i = 0; i < this._length; i++ ) {
541+
if ( !predicate.call( thisArg, buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ), i, this ) ) {
542+
return false;
543+
}
544+
}
545+
return true;
546+
});
547+
517548
/**
518549
* Offset (in bytes) of the array from the start of its underlying `ArrayBuffer`.
519550
*
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 tape = require( 'tape' );
24+
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
25+
var isFunction = require( '@stdlib/assert/is-function' );
26+
var factory = require( './../lib' );
27+
28+
29+
// TESTS //
30+
31+
tape( 'main export is a function', function test( t ) {
32+
t.ok( true, __filename );
33+
t.strictEqual( typeof factory, 'function', 'main export is a function' );
34+
t.end();
35+
});
36+
37+
tape( 'the function returns a function', function test( t ) {
38+
var ctor = factory( 'float64' );
39+
t.strictEqual( isFunction( ctor ), true, 'returns expected value' );
40+
t.end();
41+
});
42+
43+
tape( 'attached to the prototype of the returned function is an `every` method', function test( t ) {
44+
var ctor = factory( 'float64' );
45+
t.strictEqual( hasOwnProp( ctor.prototype, 'every' ), true, 'returns expected value' );
46+
t.strictEqual( isFunction( ctor.prototype.every ), true, 'returns expected value' );
47+
t.end();
48+
});
49+
50+
tape( 'the method throws an error if invoked with a `this` context which is not a typed array instance', function test( t ) {
51+
var values;
52+
var ctor;
53+
var arr;
54+
var i;
55+
56+
ctor = factory( 'float64' );
57+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
58+
59+
values = [
60+
'5',
61+
5,
62+
NaN,
63+
true,
64+
false,
65+
null,
66+
void 0,
67+
{},
68+
[],
69+
function noop() {}
70+
];
71+
for ( i = 0; i < values.length; i++ ) {
72+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
73+
}
74+
t.end();
75+
76+
function badValue( value ) {
77+
return function badValue() {
78+
return arr.every.call( value, predicate );
79+
};
80+
}
81+
function predicate( v ) {
82+
return v < 0;
83+
}
84+
});
85+
86+
tape( 'the method throws an error if provided a first arguement which is not a function', function test( t ) {
87+
var values;
88+
var ctor;
89+
var arr;
90+
var i;
91+
92+
ctor = factory( 'float64' );
93+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] );
94+
95+
values = [
96+
'5',
97+
3.14,
98+
NaN,
99+
true,
100+
false,
101+
null,
102+
void 0,
103+
{},
104+
[]
105+
];
106+
for ( i = 0; i < values.length; i++ ) {
107+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
108+
}
109+
t.end();
110+
111+
function badValue( value ) {
112+
return function badValue() {
113+
return arr.every( value );
114+
};
115+
}
116+
});
117+
118+
tape( 'the method returns `true` if operating on an empty boolean array', function test( t ) {
119+
var bool;
120+
var ctor;
121+
var arr;
122+
123+
ctor = factory( 'float64' );
124+
arr = new ctor( 'little-endian' );
125+
bool = arr.every( predicate );
126+
127+
t.strictEqual( bool, true, 'returns expected value' );
128+
t.end();
129+
130+
function predicate() {
131+
t.fail( 'should not be invoked' );
132+
}
133+
});
134+
135+
tape( 'the method returns `true` if all elements pass a test', function test( t ) {
136+
var bool;
137+
var ctor;
138+
var arr;
139+
140+
ctor = factory( 'float64' );
141+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
142+
bool = arr.every( predicate );
143+
144+
t.strictEqual( bool, true, 'returns expected value' );
145+
t.end();
146+
147+
function predicate( v ) {
148+
return v > 0;
149+
}
150+
});
151+
152+
tape( 'the method returns `false` if one or more elements fail a test', function test( t ) {
153+
var bool;
154+
var ctor;
155+
var arr;
156+
157+
ctor = factory( 'float64' );
158+
arr = new ctor( 'little-endian', [ -1.0, 2.0, -3.0, 4.0, -5.0 ] );
159+
bool = arr.every( predicate );
160+
161+
t.strictEqual( bool, false, 'returns expected value' );
162+
t.end();
163+
164+
function predicate( v ) {
165+
return v > 0;
166+
}
167+
});
168+
169+
tape( 'the method supports providing an execution context', function test( t ) {
170+
var bool;
171+
var ctor;
172+
var arr;
173+
var ctx;
174+
175+
ctor = factory( 'float64' );
176+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
177+
ctx = {
178+
'count': 0
179+
};
180+
bool = arr.every( predicate, ctx );
181+
182+
t.strictEqual( bool, true, 'returns expected value' );
183+
t.strictEqual( ctx.count, 5, 'returns expected value' );
184+
185+
t.end();
186+
187+
function predicate( v ) {
188+
this.count += 1; // eslint-disable-line no-invalid-this
189+
return v > 0;
190+
}
191+
});

0 commit comments

Comments
 (0)