From ea0e5dfeafbf8dbec7a550d0ae02ee208fe29c83 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 19 Jul 2024 09:39:48 +0000 Subject: [PATCH] Auto-generated commit --- CHANGELOG.md | 4 +- bool/README.md | 175 +++++++++++++ bool/benchmark/benchmark.copy_within.js | 56 +++++ .../benchmark/benchmark.copy_within.length.js | 103 ++++++++ bool/benchmark/benchmark.entries.js | 54 ++++ bool/benchmark/benchmark.for_each.js | 56 +++++ bool/benchmark/benchmark.for_each.length.js | 106 ++++++++ bool/docs/repl.txt | 83 ++++++ bool/docs/types/index.d.ts | 136 +++++++++- bool/lib/main.js | 190 ++++++++++++++ bool/test/test.copy_within.js | 215 ++++++++++++++++ bool/test/test.entries.js | 237 ++++++++++++++++++ bool/test/test.for_each.js | 187 ++++++++++++++ 13 files changed, 1600 insertions(+), 2 deletions(-) create mode 100644 bool/benchmark/benchmark.copy_within.js create mode 100644 bool/benchmark/benchmark.copy_within.length.js create mode 100644 bool/benchmark/benchmark.entries.js create mode 100644 bool/benchmark/benchmark.for_each.js create mode 100644 bool/benchmark/benchmark.for_each.length.js create mode 100644 bool/test/test.copy_within.js create mode 100644 bool/test/test.entries.js create mode 100644 bool/test/test.for_each.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 00b0e939..33f152c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@
-## Unreleased (2024-07-18) +## Unreleased (2024-07-19)
@@ -1324,6 +1324,7 @@ This release closes the following issue: ##### Features +- [`cb20ecc`](https://github.com/stdlib-js/stdlib/commit/cb20ecc5555cabbdfa9ad4807e096f8768f9f780) - add `copyWithin`, `entries`, `forEach` to `array/bool` [(#2611)](https://github.com/stdlib-js/stdlib/pull/2611) - [`7c17308`](https://github.com/stdlib-js/stdlib/commit/7c17308587cd0fbac9812f258e3cd774cbdfc8da) - add `at`, `fill`, `filter`, and `toLocalestring` methods to `array/bool` [(#2607)](https://github.com/stdlib-js/stdlib/pull/2607) - [`ce3ad9a`](https://github.com/stdlib-js/stdlib/commit/ce3ad9a98468829b294708ca188ec669056e58ed) - add `keys`, `values`, and `with` methods to `array/bool` [(#2590)](https://github.com/stdlib-js/stdlib/pull/2590) - [`5a66b4b`](https://github.com/stdlib-js/stdlib/commit/5a66b4bb677cdbc4706811ea9f776343297c9f87) - add `join` and `toString` methods to `array/bool` [(#2557)](https://github.com/stdlib-js/stdlib/pull/2557) @@ -2437,6 +2438,7 @@ A total of 13 people contributed to this release. Thank you to the following con
+- [`cb20ecc`](https://github.com/stdlib-js/stdlib/commit/cb20ecc5555cabbdfa9ad4807e096f8768f9f780) - **feat:** add `copyWithin`, `entries`, `forEach` to `array/bool` [(#2611)](https://github.com/stdlib-js/stdlib/pull/2611) _(by Jaysukh Makvana, Athan Reines)_ - [`ba35d8f`](https://github.com/stdlib-js/stdlib/commit/ba35d8f2e923d26640828a0f035333bd778389b7) - **refactor:** update paths _(by Athan Reines)_ - [`0406147`](https://github.com/stdlib-js/stdlib/commit/04061476d1036e1b8b786736b1ba1653eddff1ef) - **refactor:** update paths _(by Athan Reines)_ - [`b9703b5`](https://github.com/stdlib-js/stdlib/commit/b9703b569dc2f0a67e604e388a0d188b08138b48) - **refactor:** update paths _(by Athan Reines)_ diff --git a/bool/README.md b/bool/README.md index 5954ab5f..db6a0349 100644 --- a/bool/README.md +++ b/bool/README.md @@ -366,6 +366,121 @@ v = arr.at( -100 ); // returns undefined ``` + + +#### BooleanArray.prototype.copyWithin( target, start\[, end] ) + +Copies a sequence of elements within the array starting at `start` and ending at `end` (non-inclusive) to the position starting at `target`. + +```javascript +var arr = new BooleanArray( 4 ); + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( false, 2 ); +arr.set( true, 3 ); + +var v = arr.get( 0 ); +// returns true + +v = arr.get( 1 ); +// returns false + +// Copy the last two elements to the first two elements: +arr.copyWithin( 0, 2 ); + +v = arr.get( 0 ); +// returns false + +v = arr.get( 1 ); +// returns true +``` + +By default, `end` equals the number of array elements (i.e., one more than the last array index). To limit the sequence length, provide an `end` argument. + +```javascript +var arr = new BooleanArray( 4 ); + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( false, 2 ); +arr.set( true, 3 ); + +var v = arr.get( 2 ); +// returns false + +v = arr.get( 3 ); +// returns true + +// Copy the first two elements to the last two elements: +arr.copyWithin( 2, 0, 2 ); + +v = arr.get( 2 ); +// returns true + +v = arr.get( 3 ); +// returns false +``` + +When a `target`, `start`, and/or `end` index is negative, the respective index is determined relative to the last array element. The following example achieves the same behavior as the previous example: + +```javascript +var arr = new BooleanArray( 4 ); + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( false, 2 ); +arr.set( true, 3 ); + +var v = arr.get( 2 ); +// returns false + +v = arr.get( 3 ); +// returns true + +// Copy the first two elements to the last two elements using negative indices: +arr.copyWithin( -2, -4, -2 ); + +v = arr.get( 2 ); +// returns true + +v = arr.get( 3 ); +// returns false +``` + + + +#### BooleanArray.prototype.entries() + +Returns an iterator for iterating over array key-value pairs. + +```javascript +var arr = new BooleanArray( 3 ); + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); + +var it = arr.entries(); + +var v = it.next().value; +// returns [ 0, true ] + +v = it.next().value; +// returns [ 1, false ] + +v = it.next().value; +// returns [ 2, true ] + +var bool = it.next().done; +// returns true +``` + +The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties: + +- **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. +- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object. + #### BooleanArray.prototype.every( predicate\[, thisArg] ) @@ -746,6 +861,66 @@ var count = context.count; // returns 3 ``` + + +#### BooleanArray.prototype.forEach( callbackFn\[, thisArg] ) + +Invokes a function once for each array element. + +```javascript +function log( v, i ) { + console.log( '%s: %s', i, v.toString() ); +} + +var arr = new BooleanArray( 3 ); + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); + +arr.forEach( log ); +/* => + 0: true + 1: false + 2: true +*/ +``` + +The invoked function is provided three arguments: + +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. + +To set the function execution context, provide a `thisArg`. + +```javascript +function fcn( v, i ) { + this.count += 1; + console.log( '%s: %s', i, v.toString() ); +} + +var arr = new BooleanArray( 3 ); + +var context = { + 'count': 0 +}; + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); + +arr.forEach( fcn, context ); +/* => + 0: 1 + 1i + 1: 2 + 2i + 2: 3 + 3i +*/ + +var count = context.count; +// returns 3 +``` + #### BooleanArray.prototype.get( i ) diff --git a/bool/benchmark/benchmark.copy_within.js b/bool/benchmark/benchmark.copy_within.js new file mode 100644 index 00000000..b2675cfd --- /dev/null +++ b/bool/benchmark/benchmark.copy_within.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// MAIN // + +bench( pkg+':copyWithin', function benchmark( b ) { + var arr; + var buf; + var i; + + arr = []; + for ( i = 0; i < 5; i++ ) { + arr.push( i%2 ); + } + arr = new BooleanArray( arr ); + buf = arr.buffer; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buf[ 0 ] = i%2; + arr = arr.copyWithin( 1, 0 ); + if ( buf[ 0 ] !== i%2 ) { + b.fail( 'unexpected result' ); + } + } + b.toc(); + if ( buf[ 0 ] !== buf[ 0 ] ) { + b.fail( 'should not be NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/bool/benchmark/benchmark.copy_within.length.js b/bool/benchmark/benchmark.copy_within.length.js new file mode 100644 index 00000000..259434b7 --- /dev/null +++ b/bool/benchmark/benchmark.copy_within.length.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr; + var buf; + var i; + + arr = []; + for ( i = 0; i < len+1; i++ ) { + arr.push( i%2 ); + } + arr = new BooleanArray( arr ); + buf = arr.buffer; + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buf[ 0 ] = i%2; + arr.copyWithin( 1, 0 ); + if ( buf[ 0 ] !== i%2 ) { + b.fail( 'unexpected result' ); + } + } + b.toc(); + if ( buf[ 0 ] !== buf[ 0 ] ) { + b.fail( 'should not be NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':copyWithin:len='+len, f ); + } +} + +main(); diff --git a/bool/benchmark/benchmark.entries.js b/bool/benchmark/benchmark.entries.js new file mode 100644 index 00000000..01781590 --- /dev/null +++ b/bool/benchmark/benchmark.entries.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// MAIN // + +bench( pkg+':entries', function benchmark( b ) { + var iter; + var arr; + var i; + + arr = []; + for ( i = 0; i < 10; i++ ) { + arr.push( true ); + } + arr = new BooleanArray( arr ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + iter = arr.entries(); + if ( typeof iter !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof iter !== 'object' || typeof iter.next !== 'function' ) { + b.fail( 'should return an iterator protocol-compliant object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/bool/benchmark/benchmark.for_each.js b/bool/benchmark/benchmark.for_each.js new file mode 100644 index 00000000..284f9555 --- /dev/null +++ b/bool/benchmark/benchmark.for_each.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// MAIN // + +bench( pkg+':forEach', function benchmark( b ) { + var arr; + var i; + + arr = new BooleanArray( [ true, false, false, true ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr.forEach( check ); + if ( arr.length !== 4 ) { + b.fail( 'should not change an array length' ); + } + } + b.toc(); + if ( arr.length !== 4 ) { + b.fail( 'should not change an array length' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function check( v ) { + if ( !isBoolean( v ) ) { + b.fail( 'should be a boolean' ); + } + } +}); diff --git a/bool/benchmark/benchmark.for_each.length.js b/bool/benchmark/benchmark.for_each.length.js new file mode 100644 index 00000000..62b02b9a --- /dev/null +++ b/bool/benchmark/benchmark.for_each.length.js @@ -0,0 +1,106 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr; + var i; + + arr = []; + for ( i = 0; i < len; i++ ) { + arr.push( true ); + } + arr = new BooleanArray( arr ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr.forEach( callback ); + if ( arr.length !== len ) { + b.fail( 'should not change an array length' ); + } + } + b.toc(); + if ( arr.length !== len ) { + b.fail( 'should not change an array length' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function callback( value ) { + if ( value !== true ) { + throw new Error( 'something went wrong' ); + } + } + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':forEach:len='+len, f ); + } +} + +main(); diff --git a/bool/docs/repl.txt b/bool/docs/repl.txt index 1a483fa6..88ca25a1 100644 --- a/bool/docs/repl.txt +++ b/bool/docs/repl.txt @@ -313,6 +313,61 @@ true +{{alias}}.prototype.copyWithin( target, start[, end] ) + Copies a sequence of elements within the array starting at `start` and + ending at `end` (non-inclusive) to the position starting at `target`. + + Parameters + ---------- + target: integer + Target start index position. + + start: integer + Source start index position. + + end: integer (optional) + Source end index position. Default: out.length. + + Returns + ------- + out: BooleanArray + Modified array. + + Examples + -------- + > var arr = new {{alias}}( [ true, false, false, true ] ) + + > arr.copyWithin( 0, 2 ) + + > var v = arr.get( 0 ) + false + > v = arr.get( 1 ) + true + + +{{alias}}.prototype.entries() + Returns an iterator for iterating over array key-value pairs. + + Returns + ------- + iterator: Iterator + Iterator for iterating over array key-value pairs. + + Examples + -------- + > var arr = new {{alias}}( [ true, false, true ] ) + + > var it = arr.entries(); + > var v = it.next().value + [ 0, true ] + > v = it.next().value + [ 1, false ] + > v = it.next().value + [ 2, true ] + > var bool = it.next().done + true + + {{alias}}.prototype.every( predicate[, thisArg] ) Returns a boolean indicating whether all elements in the array pass a test. @@ -562,6 +617,34 @@ 2 +{{alias}}.prototype.forEach( clbk[, thisArg] ) + Invokes a function once for each array element. + + A callback function is provided the following arguments: + + - value: current array element. + - index: current array element index. + - arr: the array on which the method was called. + + Parameters + ---------- + clbk: Function + Function to invoke for each array element. + + thisArg: Any (optional) + Execution context. + + Examples + -------- + > var str = '%'; + > function clbk( v ) { str += v.toString() + '%'; }; + > var arr = new {{alias}}( [ true, false, false, true ] ) + + > arr.forEach( clbk ); + > str + '%true%false%false%true%' + + {{alias}}.prototype.get( i ) Returns an array element located at integer position (index) `i`. diff --git a/bool/docs/types/index.d.ts b/bool/docs/types/index.d.ts index e20de9a9..952b8fc1 100644 --- a/bool/docs/types/index.d.ts +++ b/bool/docs/types/index.d.ts @@ -22,13 +22,23 @@ /// -import { Iterator as Iter, IterableIterator } from '@stdlib/types/iter'; +import { Iterator as Iter, IterableIterator, TypedIterator } from '@stdlib/types/iter'; import { ArrayLike, BooleanArray as BooleanArrayInterface } from '@stdlib/types/array'; import ArrayBuffer = require( './../../../buffer' ); // Define a union type representing both iterable and non-iterable iterators: type Iterator = Iter | IterableIterator; +/** +* Locale-specific configuration options. +*/ +interface LocaleOptions { + /** + * Configuration property. + */ + [ key: string | symbol | number ]: T | undefined; +}; + /** * Callback invoked for each element in a source object. * @@ -106,6 +116,50 @@ type TernaryPredicate = ( this: U, value: boolean, index: number, arr: Boolea */ type Predicate = NullaryPredicate | UnaryPredicate | BinaryPredicate | TernaryPredicate; +/** +* Callback invoked for each element in an array. +* +* @returns undefined +*/ +type NullaryCallback = ( this: U ) => void; + +/** +* Callback invoked for each element in an array. +* +* @param value - current array element +* @returns undefined +*/ +type UnaryCallback = ( this: U, value: boolean ) => void; + +/** +* Callback invoked for each element in an array. +* +* @param value - current array element +* @param index - current array element index +* @returns undefined +*/ +type BinaryCallback = ( this: U, value: boolean, index: number ) => void; + +/** +* Callback invoked for each element in an array. +* +* @param value - current array element +* @param index - current array element index +* @param arr - array on which the method was called +* @returns undefined +*/ +type TernaryCallback = ( this: U, value: boolean, index: number, arr: BooleanArray ) => void; + +/** +* Callback invoked for each element in an array. +* +* @param value - current array element +* @param index - current array element index +* @param arr - array on which the method was called +* @returns undefined +*/ +type Callback = NullaryCallback | UnaryCallback | BinaryCallback | TernaryCallback; + /** * Callback invoked for each element in an array. * @@ -350,6 +404,61 @@ declare class BooleanArray implements BooleanArrayInterface { */ readonly length: number; + /** + * Copies a sequence of elements within the array to the position starting at `target`. + * + * @param target - index at which to start copying elements + * @param start - source index at which to copy elements from + * @param end - source index at which to stop copying elements from + * @returns modified array + * + * @example + * var arr = new BooleanArray( 4 ); + * + * arr.set( true, 0 ); + * arr.set( false, 1 ); + * arr.set( false, 2 ); + * arr.set( true, 3 ); + * + * // Copy the first two elements to the last two elements: + * arr.copyWithin( 2, 0, 2 ); + * + * var v = arr.get( 2 ); + * // returns true + * + * v = arr.get( 3 ); + * // returns false + */ + copyWithin( target: number, start: number, end?: number ): BooleanArray; + + /** + * Returns an iterator for iterating over array key-value pairs. + * + * @returns iterator + * + * @example + * var arr = new BooleanArray( 3 ); + * + * arr.set( true, 0 ); + * arr.set( false, 1 ); + * arr.set( true, 2 ); + * + * var it = arr.entries(); + * + * var v = it.next().value; + * // returns [ 0, true ] + * + * v = it.next().value; + * // returns [ 1, false ] + * + * v = it.next().value; + * // returns [ 2, true ] + * + * var bool = it.next().done; + * // returns true + */ + entries(): TypedIterator<[number, boolean]>; + /** * Tests whether all elements in an array pass a test implemented by a predicate function. * @@ -521,6 +630,31 @@ declare class BooleanArray implements BooleanArrayInterface { */ findLastIndex( predicate: Predicate, thisArg?: ThisParameterType> ): number; + /** + * Invokes a function once for each array element. + * + * @param fcn - function to invoke + * @param thisArg - execution context + * @returns undefined + * + * @example + * function log( v, i ) { + * console.log( '%s: %s', i, v.toString() ); + * } + * + * var arr = new BooleanArray( 3 ); + * + * arr.set( true, 0 ); + * arr.set( false, 1 ); + * arr.set( true, 2 ); + * + * arr.forEach( log ); + * // => 0: true + * // => 1: false + * // => 2: true + */ + forEach( fcn: Callback, thisArg?: ThisParameterType> ): void; + /** * Returns an array element. * diff --git a/bool/lib/main.js b/bool/lib/main.js index 48649376..965e4973 100644 --- a/bool/lib/main.js +++ b/bool/lib/main.js @@ -501,6 +501,156 @@ setReadOnlyAccessor( BooleanArray.prototype, 'byteOffset', function get() { */ setReadOnly( BooleanArray.prototype, 'BYTES_PER_ELEMENT', BooleanArray.BYTES_PER_ELEMENT ); +/** +* Copies a sequence of elements within the array to the position starting at `target`. +* +* @name copyWithin +* @memberof BooleanArray.prototype +* @type {Function} +* @param {integer} target - index at which to start copying elements +* @param {integer} start - source index at which to copy elements from +* @param {integer} [end] - source index at which to stop copying elements from +* @throws {TypeError} `this` must be a boolean array +* @returns {BooleanArray} modified array +* +* @example +* var arr = new BooleanArray( 4 ); +* +* arr.set( true, 0 ); +* arr.set( false, 1 ); +* arr.set( false, 2 ); +* arr.set( true, 3 ); +* +* // Copy the first two elements to the last two elements: +* arr.copyWithin( 2, 0, 2 ); +* +* var v = arr.get( 2 ); +* // returns true +* +* v = arr.get( 3 ); +* // returns false +*/ +setReadOnly( BooleanArray.prototype, 'copyWithin', function copyWithin( target, start ) { + if ( !isBooleanArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a boolean array.' ); + } + // FIXME: prefer a functional `copyWithin` implementation which addresses lack of universal browser support (e.g., IE11 and Safari) or ensure that typed arrays are polyfilled + if ( arguments.length === 2 ) { + this._buffer.copyWithin( target, start ); + } else { + this._buffer.copyWithin( target, start, arguments[2] ); + } + return this; +}); + +/** +* Returns an iterator for iterating over array key-value pairs. +* +* @name entries +* @memberof BooleanArray.prototype +* @type {Function} +* @throws {TypeError} `this` must be a boolean array +* @returns {Iterator} iterator +* +* @example +* var arr = new BooleanArray( 3 ); +* +* arr.set( true, 0 ); +* arr.set( false, 1 ); +* arr.set( true, 2 ); +* +* var it = arr.entries(); +* +* var v = it.next().value; +* // returns [ 0, true ] +* +* v = it.next().value; +* // returns [ 1, false ] +* +* v = it.next().value; +* // returns [ 2, true ] +* +* var bool = it.next().done; +* // returns true +*/ +setReadOnly( BooleanArray.prototype, 'entries', function entries() { + var self; + var iter; + var len; + var buf; + var FLG; + var i; + + if ( !isBooleanArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a boolean array.' ); + } + self = this; + buf = this._buffer; + len = this._length; + + // Initialize an iteration index: + i = -1; + + // Create an iterator protocol-compliant object: + iter = {}; + setReadOnly( iter, 'next', next ); + setReadOnly( iter, 'return', end ); + + if ( ITERATOR_SYMBOL ) { + setReadOnly( iter, ITERATOR_SYMBOL, factory ); + } + return iter; + + /** + * Returns an iterator protocol-compliant object containing the next iterated value. + * + * @private + * @returns {Object} iterator protocol-compliant object + */ + function next() { + i += 1; + if ( FLG || i >= len ) { + return { + 'done': true + }; + } + return { + 'value': [ i, Boolean( buf[ i ] ) ], + 'done': false + }; + } + + /** + * Finishes an iterator. + * + * @private + * @param {*} [value] - value to return + * @returns {Object} iterator protocol-compliant object + */ + function end( value ) { + FLG = true; + if ( arguments.length ) { + return { + 'value': value, + 'done': true + }; + } + return { + 'done': true + }; + } + + /** + * Returns a new iterator. + * + * @private + * @returns {Iterator} iterator + */ + function factory() { + return self.entries(); + } +}); + /** * Tests whether all elements in an array pass a test implemented by a predicate function. * @@ -874,6 +1024,46 @@ setReadOnly( BooleanArray.prototype, 'findLastIndex', function findLastIndex( pr return -1; }); +/** +* Invokes a function once for each array element. +* +* @name forEach +* @memberof BooleanArray.prototype +* @type {Function} +* @param {Function} fcn - function to invoke +* @param {*} [thisArg] - function invocation context +* @throws {TypeError} `this` must be a boolean array +* @throws {TypeError} first argument must be a function +* +* @example +* function log( v, i ) { +* console.log( '%s: %s', i, v.toString() ); +* } +* +* var arr = new BooleanArray( 3 ); +* +* arr.set( true, 0 ); +* arr.set( false, 1 ); +* arr.set( true, 2 ); +* +* arr.forEach( log ); +*/ +setReadOnly( BooleanArray.prototype, 'forEach', function forEach( fcn, thisArg ) { + var buf; + var i; + + if ( !isBooleanArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a boolean array.' ); + } + if ( !isFunction( fcn ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) ); + } + buf = this._buffer; + for ( i = 0; i < this._length; i++ ) { + fcn.call( thisArg, Boolean( buf[ i ] ), i, this ); + } +}); + /** * Returns an array element. * diff --git a/bool/test/test.copy_within.js b/bool/test/test.copy_within.js new file mode 100644 index 00000000..863bc4c5 --- /dev/null +++ b/bool/test/test.copy_within.js @@ -0,0 +1,215 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' ); +var Uint8Array = require( './../../uint8' ); +var BooleanArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof BooleanArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `copyWithin` method for copying a sequence of array elements within a boolean array', function test( t ) { + t.strictEqual( hasOwnProp( BooleanArray.prototype, 'copyWithin' ), true, 'has property' ); + t.strictEqual( isFunction( BooleanArray.prototype.copyWithin ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a boolean array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.copyWithin.call( value, 3, 0 ); + }; + } +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a boolean array instance (end)', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.copyWithin.call( value, 3, 0, 5 ); + }; + } +}); + +tape( 'the method copies a sequence of elements within an array', function test( t ) { + var expected; + var arr; + + arr = new BooleanArray( [ true, false, false, true, true, false, false, true ] ); + arr.copyWithin( 0, 5 ); + + expected = new Uint8Array( [ 0, 0, 1, 1, 1, 0, 0, 1 ] ); + + t.deepEqual( reinterpretBoolean( arr, 0 ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the method copies a sequence of elements within an array (negative target)', function test( t ) { + var expected; + var arr; + + arr = new BooleanArray( [ true, false, false, true, true, false, false, true ] ); + arr.copyWithin( -arr.length, 5 ); + + expected = new Uint8Array( [ 0, 0, 1, 1, 1, 0, 0, 1 ] ); + + t.deepEqual( reinterpretBoolean( arr, 0 ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the method copies a sequence of elements within an array (negative start)', function test( t ) { + var expected; + var arr; + + arr = new BooleanArray( [ true, false, false, true, true, false, false, true ] ); + arr.copyWithin( 0, -3 ); + + expected = new Uint8Array( [ 0, 0, 1, 1, 1, 0, 0, 1 ] ); + + t.deepEqual( reinterpretBoolean( arr, 0 ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the method copies a sequence of elements within an array (end=length)', function test( t ) { + var expected; + var arr; + + arr = new BooleanArray( [ true, false, false, true, true, false, false, true ] ); + arr.copyWithin( 0, 5, arr.length ); + + expected = new Uint8Array( [ 0, 0, 1, 1, 1, 0, 0, 1 ] ); + + t.deepEqual( reinterpretBoolean( arr, 0 ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the method copies a sequence of elements within an array (non-inclusive end)', function test( t ) { + var expected; + var arr; + + arr = new BooleanArray( [ true, false, false, true, true, false, false, true ] ); + arr.copyWithin( 2, 0, 4 ); + + expected = new Uint8Array( [ 1, 0, 1, 0, 0, 1, 0, 1 ] ); + + t.deepEqual( reinterpretBoolean( arr, 0 ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the method copies a sequence of elements within an array (negative end)', function test( t ) { + var expected; + var arr; + + arr = new BooleanArray( [ true, false, false, true, true, false, false, true ] ); + arr.copyWithin( 2, 0, -4 ); + + expected = new Uint8Array( [ 1, 0, 1, 0, 0, 1, 0, 1 ] ); + + t.deepEqual( reinterpretBoolean( arr, 0 ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the method copies a sequence of elements within an array (target >= length)', function test( t ) { + var expected; + var arr; + + arr = new BooleanArray( [ true, false, false, true, true, false, false, true ] ); + arr.copyWithin( arr.length, 5 ); + + expected = new Uint8Array( [ 1, 0, 0, 1, 1, 0, 0, 1 ] ); + + t.deepEqual( reinterpretBoolean( arr, 0 ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the method copies a sequence of elements within an array (target > start)', function test( t ) { + var expected; + var arr; + + arr = new BooleanArray( [ true, false, false, true, true, false, false, true ] ); + arr.copyWithin( 2, 0 ); + + expected = new Uint8Array( [ 1, 0, 1, 0, 0, 1, 1, 0 ] ); + + t.deepEqual( reinterpretBoolean( arr, 0 ), expected, 'returns expected value' ); + + t.end(); +}); diff --git a/bool/test/test.entries.js b/bool/test/test.entries.js new file mode 100644 index 00000000..1d85b375 --- /dev/null +++ b/bool/test/test.entries.js @@ -0,0 +1,237 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var isArray = require( '@stdlib/assert/is-array' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ); +var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' ); +var BooleanArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof BooleanArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is an `entries` method for returning an iterator for iterating over array key-value pairs', function test( t ) { + t.strictEqual( hasOwnProp( BooleanArray.prototype, 'entries' ), true, 'has property' ); + t.strictEqual( isFunction( BooleanArray.prototype.entries ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a boolean array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.entries.call( value ); + }; + } +}); + +tape( 'the method returns an iterator protocol-compliant object', function test( t ) { + var buf; + var arr; + var it; + var v; + var i; + + buf = [ true, false, false, true ]; + arr = new BooleanArray( buf ); + + it = arr.entries(); + t.strictEqual( it.next.length, 0, 'has zero arity' ); + + for ( i = 0; i < arr.length; i++ ) { + v = it.next(); + t.strictEqual( isArray( v.value ), true, 'returns expected value' ); + t.strictEqual( v.value[ 0 ], i, 'returns expected value' ); + t.strictEqual( isBoolean( v.value[ 1 ] ), true, 'returns expected value' ); + t.strictEqual( v.value[ 1 ], buf[ i ], 'returns expected value' ); + t.strictEqual( typeof v.done, 'boolean', 'returns expected value' ); + } + t.end(); +}); + +tape( 'the method returns an iterator which has a `return` method for closing an iterator (no argument)', function test( t ) { + var buf; + var arr; + var it; + var v; + + buf = [ true, false, false, true ]; + arr = new BooleanArray( buf ); + + it = arr.entries(); + + v = it.next(); + t.strictEqual( v.value[ 1 ], buf[ 0 ], 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value[ 1 ], buf[ 1 ], 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.return(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the method returns an iterator which has a `return` method for closing an iterator (argument)', function test( t ) { + var buf; + var arr; + var it; + var v; + + buf = [ true, false, false, true ]; + arr = new BooleanArray( buf ); + + it = arr.entries(); + + v = it.next(); + t.strictEqual( v.value[ 1 ], buf[ 0 ], 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value[ 1 ], buf[ 1 ], 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.return( 'finished' ); + t.strictEqual( v.value, 'finished', 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if an environment supports `Symbol.iterator`, the method returns an iterable', function test( t ) { + var BooleanArray; + var arr; + var buf; + var it1; + var it2; + var v1; + var v2; + var i; + + BooleanArray = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': '__ITERATOR_SYMBOL__' + }); + + buf = [ true, false, false, true ]; + arr = new BooleanArray( buf ); + + it1 = arr.entries(); + t.strictEqual( typeof it1[ '__ITERATOR_SYMBOL__' ], 'function', 'has method' ); + t.strictEqual( it1[ '__ITERATOR_SYMBOL__' ].length, 0, 'has zero arity' ); + + it2 = it1[ '__ITERATOR_SYMBOL__' ](); + t.strictEqual( typeof it2, 'object', 'returns an object' ); + t.strictEqual( typeof it2.next, 'function', 'has `next` method' ); + t.strictEqual( typeof it2.return, 'function', 'has `return` method' ); + + for ( i = 0; i < arr.length; i++ ) { + v1 = it1.next().value; + v2 = it2.next().value; + t.strictEqual( v1[ 0 ], v2[ 0 ], 'returns expected value' ); + t.strictEqual( v1[ 1 ], v2[ 1 ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'if an environment does not support `Symbol.iterator`, the method does not return an "iterable"', function test( t ) { + var BooleanArray; + var arr; + var buf; + var it; + + BooleanArray = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': false + }); + + buf = [ true, false, false, true ]; + arr = new BooleanArray( buf ); + + it = arr.entries(); + t.strictEqual( it[ ITERATOR_SYMBOL ], void 0, 'does not have property' ); + + t.end(); +}); diff --git a/bool/test/test.for_each.js b/bool/test/test.for_each.js new file mode 100644 index 00000000..4251b2a7 --- /dev/null +++ b/bool/test/test.for_each.js @@ -0,0 +1,187 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var BooleanArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof BooleanArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `forEach` method', function test( t ) { + t.strictEqual( hasOwnProp( BooleanArray.prototype, 'forEach' ), true, 'has property' ); + t.strictEqual( isFunction( BooleanArray.prototype.forEach ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a boolean array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.forEach.call( value, fcn ); + }; + } + + function fcn( v ) { + if ( !isBoolean( v ) ) { + t.fail( 'should be a boolean' ); + } + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.forEach( value ); + }; + } +}); + +tape( 'the method should not invoke a provided callback function if operating on an empty boolean array', function test( t ) { + var arr; + + arr = new BooleanArray(); + arr.forEach( fcn ); + + t.end(); + + function fcn() { + t.fail( 'should not be invoked' ); + } +}); + +tape( 'the method returns `undefined`', function test( t ) { + var arr; + var out; + + arr = new BooleanArray( [ true, false, false, true ] ); + out = arr.forEach( fcn ); + + t.strictEqual( out, void 0, 'returns expected value' ); + t.end(); + + function fcn( v ) { + if ( !isBoolean( v ) ) { + t.fail( 'should be a boolean' ); + } + } +}); + +tape( 'the method invokes a provided function for each element in an array', function test( t ) { + var indices; + var values; + var arrays; + var arr; + + indices = []; + values = []; + arrays = []; + + arr = new BooleanArray( [ true, false, false, true ] ); + arr.forEach( fcn ); + + t.deepEqual( values, [ true, false, false, true ], 'returns expected value' ); + t.deepEqual( indices, [ 0, 1, 2, 3 ], 'returns expected value' ); + t.strictEqual( arrays[ 0 ], arr, 'returns expected value' ); + t.strictEqual( arrays[ 1 ], arr, 'returns expected value' ); + t.strictEqual( arrays[ 2 ], arr, 'returns expected value' ); + t.strictEqual( arrays[ 3 ], arr, 'returns expected value' ); + + t.end(); + + function fcn( v, i, arr ) { + values.push( v ); + indices.push( i ); + arrays.push( arr ); + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var ctx; + var arr; + + ctx = { + 'count': 0 + }; + arr = new BooleanArray( [ true, false, false, true ] ); + arr.forEach( fcn, ctx ); + + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function fcn() { + this.count += 1; // eslint-disable-line no-invalid-this + } +});