Skip to content

Commit ce3ad9a

Browse files
authored
feat: add keys, values, and with methods to array/bool
PR-URL: #2590 Ref: #2304 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent e8932ee commit ce3ad9a

13 files changed

+1672
-0
lines changed

lib/node_modules/@stdlib/array/bool/README.md

+80
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,35 @@ var str = arr.join( '|' );
710710
// returns 'true|false|true'
711711
```
712712

713+
<a name="method-keys"></a>
714+
715+
#### BooleanArray.prototype.keys()
716+
717+
Returns an iterator for iterating over each index key in a typed array.
718+
719+
```javascript
720+
var arr = new BooleanArray( 2 );
721+
722+
arr.set( true, 0 );
723+
arr.set( false, 1 );
724+
725+
var iter = arr.keys();
726+
727+
var v = iter.next().value;
728+
// returns 0
729+
730+
v = iter.next().value;
731+
// returns 1
732+
733+
var bool = iter.next().done;
734+
// returns true
735+
```
736+
737+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
738+
739+
- **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the [iterator][mdn-iterator-protocol] is finished.
740+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
741+
713742
<a name="method-last-index-of"></a>
714743

715744
#### BooleanArray.prototype.lastIndexOf( searchElement\[, fromIndex] )
@@ -1337,6 +1366,55 @@ var str = arr.toString();
13371366
// returns 'true,false,true'
13381367
```
13391368

1369+
<a name="method-values"></a>
1370+
1371+
#### BooleanArray.prototype.values()
1372+
1373+
Returns an iterator for iterating over each value in a typed array.
1374+
1375+
```javascript
1376+
var arr = new BooleanArray( 2 );
1377+
1378+
arr.set( true, 0 );
1379+
arr.set( false, 1 );
1380+
1381+
var iter = arr.values();
1382+
1383+
var v = iter.next().value;
1384+
// returns true
1385+
1386+
v = iter.next().value;
1387+
// returns false
1388+
1389+
var bool = iter.next().done;
1390+
// returns true
1391+
```
1392+
1393+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
1394+
1395+
- **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the [iterator][mdn-iterator-protocol] is finished.
1396+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
1397+
1398+
<a name="method-with"></a>
1399+
1400+
#### BooleanArray.prototype.with( index, value )
1401+
1402+
Returns a new typed array with the element at a provided index replaced with a provided value.
1403+
1404+
```javascript
1405+
var arr = new BooleanArray( 3 );
1406+
1407+
arr.set( true, 0 );
1408+
arr.set( false, 1 );
1409+
arr.set( true, 1 );
1410+
1411+
var out = arr.with( 0, false );
1412+
// returns <BooleanArray>
1413+
1414+
var v = out.get( 0 );
1415+
// returns false
1416+
```
1417+
13401418
</section>
13411419

13421420
<!-- /.usage -->
@@ -1419,6 +1497,8 @@ console.log( '%s', false );
14191497

14201498
<section class="links">
14211499

1500+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
1501+
14221502
[@stdlib/array/typed]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/typed
14231503

14241504
[@stdlib/array/buffer]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/buffer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 bench = require( '@stdlib/bench' );
24+
var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
25+
var pkg = require( './../package.json' ).name;
26+
var BooleanArray = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+':keys', function benchmark( b ) {
32+
var iter;
33+
var arr;
34+
var i;
35+
36+
arr = new BooleanArray( [ true, false ] );
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
iter = arr.keys();
41+
if ( typeof iter !== 'object' ) {
42+
b.fail( 'should return an object' );
43+
}
44+
}
45+
b.toc();
46+
if ( !isIteratorLike( iter ) ) {
47+
b.fail( 'should return an iterator protocol-compliant object' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
26+
var pkg = require( './../package.json' ).name;
27+
var BooleanArray = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Creates a benchmark function.
34+
*
35+
* @private
36+
* @param {PositiveInteger} len - array length
37+
* @returns {Function} benchmark function
38+
*/
39+
function createBenchmark( len ) {
40+
var arr;
41+
var i;
42+
43+
arr = [];
44+
for ( i = 0; i < len; i++ ) {
45+
arr.push( true );
46+
}
47+
arr = new BooleanArray( arr );
48+
49+
return benchmark;
50+
51+
/**
52+
* Benchmark function.
53+
*
54+
* @private
55+
* @param {Benchmark} b - benchmark instance
56+
*/
57+
function benchmark( b ) {
58+
var iter;
59+
var i;
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
iter = arr.keys();
64+
if ( typeof iter !== 'object' ) {
65+
b.fail( 'should return an object' );
66+
}
67+
}
68+
b.toc();
69+
if ( !isIteratorLike( iter ) ) {
70+
b.fail( 'should return an iterator protocol-compliant object' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
}
75+
}
76+
77+
78+
// MAIN //
79+
80+
/**
81+
* Main execution sequence.
82+
*
83+
* @private
84+
*/
85+
function main() {
86+
var len;
87+
var min;
88+
var max;
89+
var f;
90+
var i;
91+
92+
min = 1; // 10^min
93+
max = 6; // 10^max
94+
95+
for ( i = min; i <= max; i++ ) {
96+
len = pow( 10, i );
97+
f = createBenchmark( len );
98+
bench( pkg+':keys:len='+len, f );
99+
}
100+
}
101+
102+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 bench = require( '@stdlib/bench' );
24+
var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
25+
var pkg = require( './../package.json' ).name;
26+
var BooleanArray = require('./../lib');
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+':values', function benchmark( b ) {
32+
var iter;
33+
var arr;
34+
var i;
35+
36+
arr = new BooleanArray( [ true, false ] );
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
iter = arr.values();
41+
if ( typeof iter !== 'object' ) {
42+
b.fail( 'should return an object' );
43+
}
44+
}
45+
b.toc();
46+
if ( !isIteratorLike( iter ) ) {
47+
b.fail( 'should return an iterator protocol-compliant object' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});

0 commit comments

Comments
 (0)