From f30232756ed804ff601656ea67da67757c874d82 Mon Sep 17 00:00:00 2001 From: Jaysukh-409 Date: Mon, 15 Jul 2024 21:46:45 +0530 Subject: [PATCH 1/8] feat: add at, fill, filter, toLocalestring to array/bool --- lib/node_modules/@stdlib/array/bool/README.md | 178 ++++++++++- .../array/bool/benchmark/benchmark.at.js | 85 ++++++ .../bool/benchmark/benchmark.every.length.js | 3 +- .../array/bool/benchmark/benchmark.fill.js | 56 ++++ .../bool/benchmark/benchmark.fill.length.js | 100 +++++++ .../array/bool/benchmark/benchmark.filter.js | 55 ++++ .../bool/benchmark/benchmark.filter.length.js | 115 +++++++ .../benchmark/benchmark.to_locale_string.js | 50 ++++ .../benchmark.to_locale_string.length.js | 101 +++++++ .../@stdlib/array/bool/docs/repl.txt | 127 ++++++++ .../@stdlib/array/bool/docs/types/index.d.ts | 97 ++++++ .../@stdlib/array/bool/lib/main.js | 243 +++++++++++++++ .../@stdlib/array/bool/test/test.at.js | 136 +++++++++ .../@stdlib/array/bool/test/test.fill.js | 280 ++++++++++++++++++ .../@stdlib/array/bool/test/test.filter.js | 188 ++++++++++++ .../array/bool/test/test.to_locale_string.js | 187 ++++++++++++ 16 files changed, 1997 insertions(+), 4 deletions(-) create mode 100644 lib/node_modules/@stdlib/array/bool/benchmark/benchmark.at.js create mode 100644 lib/node_modules/@stdlib/array/bool/benchmark/benchmark.fill.js create mode 100644 lib/node_modules/@stdlib/array/bool/benchmark/benchmark.fill.length.js create mode 100644 lib/node_modules/@stdlib/array/bool/benchmark/benchmark.filter.js create mode 100644 lib/node_modules/@stdlib/array/bool/benchmark/benchmark.filter.length.js create mode 100644 lib/node_modules/@stdlib/array/bool/benchmark/benchmark.to_locale_string.js create mode 100644 lib/node_modules/@stdlib/array/bool/benchmark/benchmark.to_locale_string.length.js create mode 100644 lib/node_modules/@stdlib/array/bool/test/test.at.js create mode 100644 lib/node_modules/@stdlib/array/bool/test/test.fill.js create mode 100644 lib/node_modules/@stdlib/array/bool/test/test.filter.js create mode 100644 lib/node_modules/@stdlib/array/bool/test/test.to_locale_string.js diff --git a/lib/node_modules/@stdlib/array/bool/README.md b/lib/node_modules/@stdlib/array/bool/README.md index 1d566cd9712..46b3d16d819 100644 --- a/lib/node_modules/@stdlib/array/bool/README.md +++ b/lib/node_modules/@stdlib/array/bool/README.md @@ -334,6 +334,38 @@ var len = arr.length; // returns 4 ``` + + +#### BooleanArray.prototype.at( i ) + +Returns an array element located at integer position (index) `i`, with support for both nonnegative and negative integer positions. + +```javascript +var arr = new BooleanArray( 3 ); + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); + +var v = arr.at( 0 ); +// returns true + +v = arr.at( -1 ); +// returns true +``` + +If provided an out-of-bounds index, the method returns `undefined`. + +```javascript +var arr = new BooleanArray( 10 ); + +var v = arr.at( 100 ); +// returns undefined + +v = arr.at( -100 ); +// returns undefined +``` + #### BooleanArray.prototype.every( predicate\[, thisArg] ) @@ -386,6 +418,126 @@ var count = context.count; // returns 3 ``` + + +#### BooleanArray.prototype.fill( value\[, start\[, end]] ) + +Returns a modified typed array filled with a fill value. + +```javascript +var arr = new BooleanArray( 3 ); + +// Set all elements to the same value: +arr.fill( true ); + +var v = arr.get( 0 ); +// returns true + +v = arr.get( 1 ); +// returns true + +v = arr.get( 2 ); +// returns true + +// Fill all elements starting from the second element: +arr.fill( false, 1 ); + +v = arr.get( 1 ); +// returns false + +v = arr.get( 2 ); +// returns false + +// Fill all elements from first element until the second-to-last element: +arr.fill( false, 0, 2 ); + +v = arr.get( 0 ); +// returns false + +v = arr.get( 1 ); +// returns false +``` + +When a `start` and/or `end` index is negative, the respective index is determined relative to the last array element. + +```javascript +var arr = new BooleanArray( 3 ); + +// Set all array elements, except the last element, to the same value: +arr.fill( true, 0, -1 ); + +var v = arr.get( 0 ); +// returns true + +v = arr.get( 2 ); +// returns false +``` + + + +#### BooleanArray.prototype.filter( predicate\[, thisArg] ) + +Returns a new array containing the elements of an array which pass a test implemented by a predicate function. + +```javascript +function predicate( v ) { + return ( v === true ); +} + +var arr = new BooleanArray( 3 ); + +// Set the first three elements: +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); + +var out = arr.filter( predicate ); +// returns + +var len = out.length; +// returns 2 + +var v = out.get( 0 ); +// returns true + +v = out.get( 1 ); +// return true +``` + +The `predicate` 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 predicate( v, i ) { + this.count += 1; + return ( v === true ); +} + +var arr = new BooleanArray( 3 ); + +var context = { + 'count': 0 +}; + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); + +var out = arr.filter( predicate, context ); +// returns + +var len = out.length; +// returns 2 + +var count = context.count; +// returns 3 +``` + #### BooleanArray.prototype.find( predicate\[, thisArg] ) @@ -1197,8 +1349,6 @@ The function should return a number where: - a positive value indicates that `a` should come after `b`. - zero or `NaN` indicates that `a` and `b` are considered equal. - - #### BooleanArray.prototype.subarray( \[begin\[, end]] ) @@ -1275,6 +1425,30 @@ bool = subarr.get( len-1 ); // returns true ``` + + +#### BooleanArray.prototype.toLocaleString( \[locales\[, options]] ) + +Serializes an array as a locale-specific string. + +```javascript +var arr = new BooleanArray( 3 ); + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); + +var str = arr.toLocaleString(); +// returns 'true,false,true' +``` + +The method supports the following arguments: + +- **locales**: a string with a BCP 47 language tag or an array of such strings. +- **options**: configuration properties. + + + #### BooleanArray.prototype.toReversed() Returns a new typed array containing the elements in reversed order. diff --git a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.at.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.at.js new file mode 100644 index 00000000000..674a02850af --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.at.js @@ -0,0 +1,85 @@ +/** +* @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+'::nonnegative_indices:at', function benchmark( b ) { + var arr; + var N; + var v; + var i; + + arr = []; + for ( i = 0; i < 10; i++ ) { + arr.push( true ); + } + arr = new BooleanArray( arr ); + N = arr.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr.at( i%N ); + if ( typeof v !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( v ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::negative_indices:at', function benchmark( b ) { + var arr; + var N; + var v; + var i; + + arr = []; + for ( i = 0; i < 10; i++ ) { + arr.push( true ); + } + arr = new BooleanArray( arr ); + N = arr.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr.at( -(i%N)-1 ); + if ( typeof v !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( v ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.every.length.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.every.length.js index b42918f8644..ac36b2c8bc8 100644 --- a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.every.length.js +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.every.length.js @@ -23,7 +23,6 @@ var bench = require( '@stdlib/bench' ); var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; var pow = require( '@stdlib/math/base/special/pow' ); -var Boolean = require( '@stdlib/boolean/ctor' ); var pkg = require( './../package.json' ).name; var BooleanArray = require( './../lib' ); @@ -56,7 +55,7 @@ function createBenchmark( len ) { arr = []; for ( i = 0; i < len; i++ ) { - arr.push( Boolean( 1 ) ); + arr.push( true ); } arr = new BooleanArray( arr ); diff --git a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.fill.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.fill.js new file mode 100644 index 00000000000..388e8cff97a --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.fill.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 isBooleanArray = require( '@stdlib/assert/is-booleanarray' ); +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// MAIN // + +bench( pkg+':fill', function benchmark( b ) { + var values; + var arr; + var out; + var i; + + values = [ + true, + false + ]; + arr = new BooleanArray( 5 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.fill( values[ i%values.length ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isBooleanArray( out ) ) { + b.fail( 'should return a BooleanArray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.fill.length.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.fill.length.js new file mode 100644 index 00000000000..bac49a2cb53 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.fill.length.js @@ -0,0 +1,100 @@ +/** +* @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 isBooleanArray = require( '@stdlib/assert/is-booleanarray' ); +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 = new BooleanArray( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var values; + var out; + var i; + + values = [ + true, + false + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.fill( values[ i%values.length ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isBooleanArray( out ) ) { + b.fail( 'should return a BooleanArray' ); + } + 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+':fill:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.filter.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.filter.js new file mode 100644 index 00000000000..aa04c6ac445 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.filter.js @@ -0,0 +1,55 @@ +/** +* @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 isBooleanArray = require( '@stdlib/assert/is-booleanarray' ); +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// MAIN // + +bench( pkg+':filter', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new BooleanArray( [ true, false, true ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.filter( predicate ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isBooleanArray( out ) ) { + b.fail( 'should return a BooleanArray' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function predicate( v ) { + return ( v === true ); + } +}); diff --git a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.filter.length.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.filter.length.js new file mode 100644 index 00000000000..d9c3986d723 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.filter.length.js @@ -0,0 +1,115 @@ +/** +* @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 isBooleanArray = require( '@stdlib/assert/is-booleanarray' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var BooleanArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Predicate function. +* +* @private +* @param {boolean} value - array element +* @param {NonNegativeInteger} idx - array element index +* @param {BooleanArray} arr - array instance +* @returns {boolean} boolean indicating whether a value passes a test +*/ +function predicate( value ) { + return value === true; +} + +/** +* 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 out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.filter( predicate ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isBooleanArray( out ) ) { + b.fail( 'should return a BooleanArray' ); + } + 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+':filter:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.to_locale_string.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.to_locale_string.js new file mode 100644 index 00000000000..67d577c7c88 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.to_locale_string.js @@ -0,0 +1,50 @@ +/** +* @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+':toLocaleString', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new BooleanArray( [ true, false, false, true ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.toLocaleString(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.to_locale_string.length.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.to_locale_string.length.js new file mode 100644 index 00000000000..431b75bd58f --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.to_locale_string.length.js @@ -0,0 +1,101 @@ +/** +* @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 out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.toLocaleString(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + 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+':toLocaleString:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/bool/docs/repl.txt b/lib/node_modules/@stdlib/array/bool/docs/repl.txt index d419825551c..1a483fa6dde 100644 --- a/lib/node_modules/@stdlib/array/bool/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/bool/docs/repl.txt @@ -286,6 +286,33 @@ 10 +{{alias}}.prototype.at( i ) + Returns an array element located at integer position (index) `i`, with + support for noth nonnegative and negative integer positions. + + If provided an index outside the array index range, the method returns + `undefined`. + + Parameters + ---------- + i: integer + Element index. + + Returns + ------- + out: boolean|void + An array element. + + Examples + -------- + > var arr = new {{alias}}( [ true, false, false, true ] ) + + > var v = arr.at( 1 ) + false + > v = arr.at( -1 ) + true + + {{alias}}.prototype.every( predicate[, thisArg] ) Returns a boolean indicating whether all elements in the array pass a test. @@ -319,6 +346,82 @@ true +{{alias}}.prototype.fill( value[, start[, end]] ) + Returns a modified typed array filled with a fill value. + + Parameters + ---------- + value: boolean + Fill value. + + start: integer (optional) + Start index. If less than zero, the start index is resolved relative to + the last array element. Default: 0. + + end: integer (optional) + End index (non-inclusive). If less than zero, the end index is resolved + relative to the last array element. Default: out.length. + + Returns + ------- + out: BooleanArray + Modified array. + + Examples + -------- + > var arr = new {{alias}}( 3 ) + + > arr.fill( true ); + > var v = arr.get( 0 ) + true + > v = arr.get( 1 ) + true + > v = arr.get( 2 ) + true + + +{{alias}}.prototype.filter( predicate[, thisArg] ) + Returns a new array containing the elements of an array which pass a test + implemented by a predicate function. + + A predicate function is provided the following arguments: + + - value: current array element. + - index: current array element index. + - arr: the array on which the method was called. + + The returned array has the same data type as the host array. + + Parameters + ---------- + predicate: Function + Predicate function which filters array elements. If a predicate function + returns a truthy value, an array element is included in the output + array; otherwise, an array element is not included in the output array. + + thisArg: Any (optional) + Execution context. + + Returns + ------- + out: BooleanArray + A new typed array. + + Examples + -------- + > function predicate( v ) { return ( v === true ); }; + > var arr = new {{alias}}( [ true, false, true ] ) + + > var out = arr.filter( predicate ) + + > var len = out.length + 2 + > var v = out.get( 0 ) + true + > v = out.get( 1 ) + true + + {{alias}}.prototype.find( predicate[, thisArg] ) Returns the first element in an array for which a predicate function returns a truthy value. @@ -929,6 +1032,30 @@ true +{{alias}}.prototype.toLocaleString( [locales[, options]] ) + Serializes an array as a locale-specific string. + + Parameters + ---------- + locales: string|Array (optional) + Locale identifier(s). + + options: Object (optional) + An object containing serialization options. + + Returns + ------- + str: string + Local-specific string. + + Examples + -------- + > var arr = new {{alias}}( [ true, false, true ] ) + + > var str = arr.toLocaleString() + 'true,false,true' + + {{alias}}.prototype.toReversed() Returns a new typed array containing the elements in reversed order. diff --git a/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts index e57578f2962..2413ccead4c 100644 --- a/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts @@ -281,6 +281,31 @@ declare class BooleanArray implements BooleanArrayInterface { */ constructor( arg?: number | ArrayLike | ArrayBuffer | Iterable, byteOffset?: number, length?: number ); + /** + * Returns an array element with support for both nonnegative and negative integer indices. + * + * @param i - element index + * @throws index argument must be a integer + * @returns array element + * + * @example + * var arr = new BooleanArray( 3 ); + * + * arr.set( true, 0 ); + * arr.set( false, 1 ); + * arr.set( true, 2 ); + * + * var v = arr.at( 0 ); + * // returns true + * + * v = arr.at( -1 ); + * // returns true + * + * v = arr.at( 100 ); + * // returns undefined + */ + at( i: number ): boolean | void; + /** * Length (in bytes) of the array. * @@ -348,6 +373,59 @@ declare class BooleanArray implements BooleanArrayInterface { */ every( predicate: Predicate, thisArg?: ThisParameterType> ): boolean; + /** + * Returns a modified typed array filled with a fill value. + * + * @param value - fill value + * @param start - starting index (inclusive) + * @param end - ending index (exclusive) + * @returns modified typed array + * + * @example + * var arr = new BooleanArray( 3 ); + * + * arr.fill( true, 1 ); + * + * var v = arr.get( 1 ); + * // returns true + * + * v = arr.get( 2 ); + * // returns true + */ + fill( value: boolean, start?: number, end?: number ): BooleanArray; + + /** + * Returns a new array containing the elements of an array which pass a test implemented by a predicate function. + * + * @param predicate - test function + * @param thisArg - execution context + * @returns new array containing elements which pass a test implemented by a predicate function + * + * @example + * function predicate( v ) { + * return ( v === true ); + * } + * + * var arr = new BooleanArray( 3 ); + * + * arr.set( true, 0 ); + * arr.set( false, 1 ); + * arr.set( true, 2 ); + * + * var out = arr.filter( predicate ); + * // returns + * + * var len = out.length; + * // returns 2 + * + * var v = out.get( 0 ); + * // returns true + * + * v = out.get( 1 ); + * // returns true + */ + filter( predicate: Predicate, thisArg?: ThisParameterType> ): BooleanArray; + /** * Returns the first element in an array for which a predicate function returns a truthy value. * @@ -884,6 +962,25 @@ declare class BooleanArray implements BooleanArrayInterface { */ subarray( begin?: number, end?: number ): BooleanArray; + /** + * Serializes an array as a locale-specific string. + * + * @param locales - locale identifier(s) + * @param options - configuration options + * @returns string + * + * @example + * var arr = new BooleanArray( 3 ); + * + * arr.set( true, 0 ); + * arr.set( false, 1 ); + * arr.set( true, 1 ); + * + * var str = arr.toLocaleString(); + * // returns 'true,false,true' + */ + toLocaleString( locales?: string | Array, options?: LocaleOptions ): string; + /** * Returns a new typed array containing the elements in reversed order. * diff --git a/lib/node_modules/@stdlib/array/bool/lib/main.js b/lib/node_modules/@stdlib/array/bool/lib/main.js index 0e55dd32a0e..15ba4c067e3 100644 --- a/lib/node_modules/@stdlib/array/bool/lib/main.js +++ b/lib/node_modules/@stdlib/array/bool/lib/main.js @@ -30,6 +30,7 @@ var isFunction = require( '@stdlib/assert/is-function' ); var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives; var hasIteratorSymbolSupport = require( '@stdlib/assert/has-iterator-symbol-support' ); var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' ); var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); @@ -381,6 +382,54 @@ setReadOnly( BooleanArray, 'of', function of() { return new this( args ); }); +/** +* Returns an array element with support for both nonnegative and negative integer indices. +* +* @name at +* @memberof BooleanArray.prototype +* @type {Function} +* @param {integer} idx - element index +* @throws {TypeError} `this` must be a boolean array +* @throws {TypeError} must provide an integer +* @returns {(boolean|void)} array element +* +* @example +* var arr = new BooleanArray( 3 ); +* +* arr.set( true, 0 ); +* arr.set( false, 1 ); +* arr.set( true, 2 ); +* +* var v = arr.at( 0 ); +* // returns true +* +* v = arr.at( -1 ); +* // returns true +* +* v = arr.at( 100 ); +* // returns undefined +*/ +setReadOnly( BooleanArray.prototype, 'at', function at( idx ) { + var buf; + var len; + + if ( !isBooleanArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a boolean array.' ); + } + if ( !isInteger( idx ) ) { + throw new TypeError( format( 'invalid argument. Must provide an integer. Value: `%s`.', idx ) ); + } + len = this._length; + buf = this._buffer; + if ( idx < 0 ) { + idx += len; + } + if ( idx < 0 || idx >= len ) { + return; + } + return Boolean( buf[ idx ] ); +}); + /** * Pointer to the underlying data buffer. * @@ -497,6 +546,145 @@ setReadOnly( BooleanArray.prototype, 'every', function every( predicate, thisArg return true; }); +/** +* Returns a modified typed array filled with a fill value. +* +* @name fill +* @memberof BooleanArray.prototype +* @type {Function} +* @param {boolean} value - fill value +* @param {integer} [start=0] - starting index (inclusive) +* @param {integer} [end] - ending index (exclusive) +* @throws {TypeError} `this` must be a boolean array +* @throws {TypeError} first argument must be a boolean +* @throws {TypeError} second argument must be an integer +* @throws {TypeError} third argument must be an integer +* @returns {BooleanArray} modified array +* +* @example +* var arr = new BooleanArray( 3 ); +* +* arr.fill( true, 1 ); +* +* var v = arr.get( 1 ); +* // returns true +* +* v = arr.get( 2 ); +* // returns true +*/ +setReadOnly( BooleanArray.prototype, 'fill', function fill( value, start, end ) { + var buf; + var len; + var val; + var i; + + if ( !isBooleanArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a boolean array.' ); + } + if ( !isBoolean( value ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a boolean. Value: `%s`.', value ) ); + } + buf = this._buffer; + len = this._length; + if ( arguments.length > 1 ) { + if ( !isInteger( start ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', start ) ); + } + if ( start < 0 ) { + start += len; + if ( start < 0 ) { + start = 0; + } + } + if ( arguments.length > 2 ) { + if ( !isInteger( end ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be an integer. Value: `%s`.', end ) ); + } + if ( end < 0 ) { + end += len; + if ( end < 0 ) { + end = 0; + } + } + if ( end > len ) { + end = len; + } + } else { + end = len; + } + } else { + start = 0; + end = len; + } + if ( value ) { + val = 1; + } else { + val = 0; + } + for ( i = start; i < end; i++ ) { + buf[ i ] = val; + } + return this; +}); + +/** +* Returns a new array containing the elements of an array which pass a test implemented by a predicate function. +* +* @name filter +* @memberof BooleanArray.prototype +* @type {Function} +* @param {Function} predicate - test function +* @param {*} [thisArg] - predicate function execution context +* @throws {TypeError} `this` must be a boolean array +* @throws {TypeError} first argument must be a function +* @returns {BooleanArray} boolean array +* +* @example +* function predicate( v ) { +* return ( v === true ); +* } +* +* var arr = new BooleanArray( 3 ); +* +* arr.set( true, 0 ); +* arr.set( false, 1 ); +* arr.set( true, 2 ); +* +* var out = arr.filter( predicate ); +* // returns +* +* var len = out.length; +* // returns 2 +* +* var v = out.get( 0 ); +* // returns true +* +* v = out.get( 1 ); +* // returns true +*/ +setReadOnly( BooleanArray.prototype, 'filter', function filter( predicate, thisArg ) { + var buf; + var out; + var i; + var v; + + if ( !isBooleanArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a boolean array.' ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); + } + buf = this._buffer; + out = []; + for ( i = 0; i < this._length; i++ ) { + v = Boolean( buf[ i ] ); + if ( predicate.call( thisArg, v, i, this ) ) { + out.push( v ); + } + } + return new this.constructor( out ); +}); + /** * Returns the first element in an array for which a predicate function returns a truthy value. * @@ -1719,6 +1907,61 @@ setReadOnly( BooleanArray.prototype, 'subarray', function subarray( begin, end ) return new this.constructor( buf.buffer, offset, ( len < 0 ) ? 0 : len ); }); +/** +* Serializes an array as a locale-specific string. +* +* @name toLocaleString +* @memberof BooleanArray.prototype +* @type {Function} +* @param {(string|Array)} [locales] - locale identifier(s) +* @param {Object} [options] - configuration options +* @throws {TypeError} `this` must be a boolean array +* @throws {TypeError} first argument must be a string or an array of strings +* @throws {TypeError} options argument must be an object +* @returns {string} string representation +* +* @example +* var arr = new BooleanArray( 3 ); +* +* arr.set( true, 0 ); +* arr.set( false, 1 ); +* arr.set( true, 2 ); +* +* var str = arr.toLocaleString(); +* // returns 'true,false,true' +*/ +setReadOnly( BooleanArray.prototype, 'toLocaleString', function toLocaleString( locales, options ) { + var opts; + var loc; + var out; + var buf; + var i; + + if ( !isBooleanArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a boolean array.' ); + } + if ( arguments.length === 0 ) { + loc = []; + } else if ( isString( locales ) || isStringArray( locales ) ) { + loc = locales; + } else { + throw new TypeError( 'invalid argument. First argument must be a string or an array of strings. Value: `' + locales + '`.' ); + } + if ( arguments.length < 2 ) { + opts = {}; + } else if ( isObject( options ) ) { + opts = options; + } else { + throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' ); + } + buf = this._buffer; + out = []; + for ( i = 0; i < this._length; i++ ) { + out.push( Boolean( buf[ i ] ).toLocaleString( loc, opts ) ); + } + return out.join( ',' ); +}); + /** * Returns a new typed array containing the elements in reversed order. * diff --git a/lib/node_modules/@stdlib/array/bool/test/test.at.js b/lib/node_modules/@stdlib/array/bool/test/test.at.js new file mode 100644 index 00000000000..e43dd6d7d5e --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/test/test.at.js @@ -0,0 +1,136 @@ +/** +* @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 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 `at` method for returning an array element', function test( t ) { + t.strictEqual( hasOwnProp( BooleanArray.prototype, 'at' ), true, 'has property' ); + t.strictEqual( isFunction( BooleanArray.prototype.at ), 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.at.call( value, 0 ); + }; + } +}); + +tape( 'the method throws an error if provided an index argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 10 ); + + values = [ + '5', + 3.14, + 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.at( value ); + }; + } +}); + +tape( 'the method returns `undefined` if provided an index which exceeds array dimensions', function test( t ) { + var arr; + var v; + var i; + + arr = new BooleanArray( 10 ); + for ( i = -arr.length; i < arr.length; i++ ) { + if ( i < 0 ) { + v = arr.at( i-arr.length ); + t.strictEqual( v, void 0, 'returns expected value for index '+(i-arr.length) ); + } else { + v = arr.at( arr.length+i ); + t.strictEqual( v, void 0, 'returns expected value for index '+(arr.length+i) ); + } + } + t.end(); +}); + +tape( 'the method returns an array element', function test( t ) { + var expected; + var arr; + var v; + var i; + + arr = new BooleanArray( [ true, false, true, false, true ] ); + expected = [ true, false, true, false, true, true, false, true, false, true ]; + + for ( i = -arr.length; i < arr.length; i++ ) { + v = arr.at( i ); + t.strictEqual( v, expected[ i+arr.length ], 'returns expected value' ); + } + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/bool/test/test.fill.js b/lib/node_modules/@stdlib/array/bool/test/test.fill.js new file mode 100644 index 00000000000..f42474c6143 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/test/test.fill.js @@ -0,0 +1,280 @@ +/** +* @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 Uint8Array = require( '@stdlib/array/uint8' ); +var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' ); +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 `fill` method', function test( t ) { + t.strictEqual( hasOwnProp( BooleanArray.prototype, 'fill' ), true, 'has property' ); + t.strictEqual( isFunction( BooleanArray.prototype.fill ), 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.fill.call( value, true ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a boolean', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 5 ); + + values = [ + '5', + 5, + NaN, + 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.fill( value ); + }; + } +}); + +tape( 'the method throws an error if provided a second argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 10 ); + + values = [ + '5', + 3.14, + 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.fill( true, value ); + }; + } +}); + +tape( 'the method throws an error if provided a third argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 10 ); + + values = [ + '5', + 3.14, + 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.fill( true, 0, value ); + }; + } +}); + +tape( 'the method returns an empty array if operating on an empty boolean array', function test( t ) { + var arr; + var out; + + arr = new BooleanArray(); + out = arr.fill( true ); + + t.strictEqual( out instanceof BooleanArray, true, 'returns expected value' ); + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'the method does not change the array length', function test( t ) { + var arr; + var out; + + arr = new BooleanArray( 10 ); + out = arr.fill( true ); + + t.strictEqual( out, arr, 'returns expected value' ); + t.strictEqual( arr.length, 10, 'returns expected value' ); + t.end(); +}); + +tape( 'if called with one argument, the method sets each array element to the provided value', function test( t ) { + var expected; + var arr; + + arr = new BooleanArray( 3 ); + expected = new Uint8Array( [ 1, 1, 1 ] ); + + arr.fill( true ); + + t.deepEqual( reinterpretBoolean( arr, 0 ), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if called with two arguments, the method sets each array element to the provided value starting from a start index (inclusive)', function test( t ) { + var expected; + var arr; + + arr = new BooleanArray( 3 ); + expected = new Uint8Array( [ 0, 1, 1 ] ); + + arr.fill( true, 1 ); + + t.deepEqual( reinterpretBoolean( arr, 0 ), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if called with three arguments, the method sets each array element to the provided value starting from a start index (inclusive) until a specified end index (exclusive)', function test( t ) { + var expected; + var arr; + + arr = new BooleanArray( 3 ); + expected = new Uint8Array( [ 1, 1, 0 ] ); + + arr.fill( true, 0, 2 ); + + t.deepEqual( reinterpretBoolean( arr, 0 ), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method supports negative indices', function test( t ) { + var expected; + var arr; + + arr = new BooleanArray( 3 ); + expected = new Uint8Array( [ 1, 1, 0 ] ); + + arr.fill( true, -3, -1 ); + + t.deepEqual( reinterpretBoolean( arr, 0 ), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if a provided start index resolves to a negative index, the method fills an array starting from the first element', function test( t ) { + var expected; + var arr; + + arr = new BooleanArray( 3 ); + expected = new Uint8Array( [ 1, 1, 1 ] ); + + arr.fill( true, -10 ); + + t.deepEqual( reinterpretBoolean( arr, 0 ), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if a provided end index resolves to an index exceeding the last array element index, the method fills an array until the last element (inclusive)', function test( t ) { + var expected; + var arr; + + arr = new BooleanArray( 3 ); + expected = new Uint8Array( [ 0, 1, 1 ] ); + + arr.fill( true, 1, 10 ); + + t.deepEqual( reinterpretBoolean( arr, 0 ), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if a provided start index resolves to an index which is greater than or equal to a resolved end index, the method does not fill an array', function test( t ) { + var expected; + var arr; + + arr = new BooleanArray( 3 ); + expected = new Uint8Array( [ 0, 0, 0 ] ); + + arr.fill( true, 2, 1 ); + + t.deepEqual( reinterpretBoolean( arr, 0 ), expected, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/bool/test/test.filter.js b/lib/node_modules/@stdlib/array/bool/test/test.filter.js new file mode 100644 index 00000000000..d52cfaaa0f7 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/test/test.filter.js @@ -0,0 +1,188 @@ +/** +* @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 instanceOf = require( '@stdlib/assert/instance-of' ); +var Uint8Array = require( '@stdlib/array/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 `filter` method', function test( t ) { + t.strictEqual( hasOwnProp( BooleanArray.prototype, 'filter' ), true, 'has property' ); + t.strictEqual( isFunction( BooleanArray.prototype.filter ), 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.filter.call( value, predicate ); + }; + } + + function predicate( v ) { + return ( v === true ); + } +}); + +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.filter( value ); + }; + } +}); + +tape( 'the method returns an empty array if operating on an empty boolean array', function test( t ) { + var arr; + var out; + + arr = new BooleanArray(); + out = arr.filter( predicate ); + + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v === true ); + } +}); + +tape( 'the method returns a new boolean array containing only those elements which satisfy a test condition', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ true, false, true ] ); + expected = new Uint8Array( [ 1, 1 ] ); + actual = arr.filter( predicate ); + + t.strictEqual( instanceOf( actual, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( reinterpretBoolean( actual, 0 ), expected, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v === true ); + } +}); + +tape( 'the method copies all elements to a new array if all elements satisfy a test condition', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ true, false, true, false ] ); + expected = new Uint8Array( [ 1, 1 ] ); + actual = arr.filter( predicate ); + + t.strictEqual( instanceOf( actual, BooleanArray ), true, 'returns expected value' ); + t.notEqual( actual, arr, 'returns a new instance' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( reinterpretBoolean( actual, 0 ), expected, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v === true ); + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var expected; + var actual; + var arr; + var ctx; + + arr = new BooleanArray( [ true, false, true ] ); + expected = new Uint8Array( [ 1, 1 ] ); + ctx = { + 'count': 0 + }; + actual = arr.filter( predicate, ctx ); + + t.strictEqual( instanceOf( actual, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( reinterpretBoolean( actual, 0 ), expected, 'returns expected value' ); + t.strictEqual( ctx.count, 3, 'returns expected value' ); + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( v === true ); + } +}); diff --git a/lib/node_modules/@stdlib/array/bool/test/test.to_locale_string.js b/lib/node_modules/@stdlib/array/bool/test/test.to_locale_string.js new file mode 100644 index 00000000000..99b398dfd14 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/test/test.to_locale_string.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 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 `toLocaleString` method', function test( t ) { + t.strictEqual( hasOwnProp( BooleanArray.prototype, 'toLocaleString' ), true, 'has property' ); + t.strictEqual( isFunction( BooleanArray.prototype.toLocaleString ), 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.toLocaleString.call( value ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a string or an array of strings', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray(); + + values = [ + 5, + NaN, + true, + false, + null, + void 0, + {}, + [ 1, 2, 3 ], + 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.toLocaleString( value ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a string or an array of strings (options)', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray(); + + values = [ + 5, + NaN, + true, + false, + null, + void 0, + {}, + [ 1, 2, 3 ], + 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.toLocaleString( value, {} ); + }; + } +}); + +tape( 'the method throws an error if provided a second argument which is not an object', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray(); + + values = [ + 5, + NaN, + true, + false, + null, + void 0, + 'beep', + [], + 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.toLocaleString( 'en-GB', value ); + }; + } +}); + +tape( 'the method returns an empty string if invoked on an empty array', function test( t ) { + var str; + var arr; + + arr = new BooleanArray(); + str = arr.toLocaleString(); + + t.strictEqual( str, '', 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns a string representation of a boolean array', function test( t ) { + var expected; + var str; + var arr; + + arr = new BooleanArray( [ true, false, true ] ); + expected = 'true,false,true'; + + str = arr.toLocaleString(); + + t.strictEqual( str, expected, 'returns expected value' ); + t.end(); +}); From 4fe06694082ed942d88a29145c6d231bded6bea0 Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 15 Jul 2024 15:54:42 -0700 Subject: [PATCH 2/8] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts index 2413ccead4c..368ee6b1903 100644 --- a/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts @@ -282,7 +282,7 @@ declare class BooleanArray implements BooleanArrayInterface { constructor( arg?: number | ArrayLike | ArrayBuffer | Iterable, byteOffset?: number, length?: number ); /** - * Returns an array element with support for both nonnegative and negative integer indices. + * Returns an array element located at integer position (index) `i`, with support for both nonnegative and negative integer indices. * * @param i - element index * @throws index argument must be a integer From 324f6973ce9d09698b41864d2cc05d9d7df39269 Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 15 Jul 2024 15:56:02 -0700 Subject: [PATCH 3/8] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts index 368ee6b1903..c118d67f3db 100644 --- a/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts @@ -386,7 +386,10 @@ declare class BooleanArray implements BooleanArrayInterface { * * arr.fill( true, 1 ); * - * var v = arr.get( 1 ); + * var v = arr.get( 0 ); + * // returns false + * + * v = arr.get( 1 ); * // returns true * * v = arr.get( 2 ); From 8893f643cb2a7a26e8ac0298fef1efebcfdfb3ea Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 15 Jul 2024 15:57:16 -0700 Subject: [PATCH 4/8] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/bool/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/bool/lib/main.js b/lib/node_modules/@stdlib/array/bool/lib/main.js index 15ba4c067e3..3521881bd08 100644 --- a/lib/node_modules/@stdlib/array/bool/lib/main.js +++ b/lib/node_modules/@stdlib/array/bool/lib/main.js @@ -383,7 +383,7 @@ setReadOnly( BooleanArray, 'of', function of() { }); /** -* Returns an array element with support for both nonnegative and negative integer indices. +* Returns an array element located at integer position (index) `i`, with support for both nonnegative and negative integer indices. * * @name at * @memberof BooleanArray.prototype From 49a10965283e0d2ec198f097722cfa745ce6cae1 Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 15 Jul 2024 15:58:16 -0700 Subject: [PATCH 5/8] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/bool/lib/main.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/bool/lib/main.js b/lib/node_modules/@stdlib/array/bool/lib/main.js index 3521881bd08..88ab9ba479d 100644 --- a/lib/node_modules/@stdlib/array/bool/lib/main.js +++ b/lib/node_modules/@stdlib/array/bool/lib/main.js @@ -566,7 +566,10 @@ setReadOnly( BooleanArray.prototype, 'every', function every( predicate, thisArg * * arr.fill( true, 1 ); * -* var v = arr.get( 1 ); +* var v = arr.get( 0 ); +* // returns false +* +* v = arr.get( 1 ); * // returns true * * v = arr.get( 2 ); From d3c5e44df6f2172a99ef265d9976429e59b15d79 Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 15 Jul 2024 16:01:43 -0700 Subject: [PATCH 6/8] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/bool/lib/main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/bool/lib/main.js b/lib/node_modules/@stdlib/array/bool/lib/main.js index 88ab9ba479d..c2688c02bea 100644 --- a/lib/node_modules/@stdlib/array/bool/lib/main.js +++ b/lib/node_modules/@stdlib/array/bool/lib/main.js @@ -1948,14 +1948,14 @@ setReadOnly( BooleanArray.prototype, 'toLocaleString', function toLocaleString( } else if ( isString( locales ) || isStringArray( locales ) ) { loc = locales; } else { - throw new TypeError( 'invalid argument. First argument must be a string or an array of strings. Value: `' + locales + '`.' ); + throw new TypeError( format( 'invalid argument. First argument must be a string or an array of strings. Value: `%s`.', locales ) ); } if ( arguments.length < 2 ) { opts = {}; } else if ( isObject( options ) ) { opts = options; } else { - throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' ); + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); } buf = this._buffer; out = []; From 1947a4715b067861ededafb6fee5fa906424d144 Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 15 Jul 2024 16:11:11 -0700 Subject: [PATCH 7/8] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/bool/test/test.filter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/bool/test/test.filter.js b/lib/node_modules/@stdlib/array/bool/test/test.filter.js index d52cfaaa0f7..98244fa191d 100644 --- a/lib/node_modules/@stdlib/array/bool/test/test.filter.js +++ b/lib/node_modules/@stdlib/array/bool/test/test.filter.js @@ -147,8 +147,8 @@ tape( 'the method copies all elements to a new array if all elements satisfy a t var actual; var arr; - arr = new BooleanArray( [ true, false, true, false ] ); - expected = new Uint8Array( [ 1, 1 ] ); + arr = new BooleanArray( [ true, true, true, true ] ); + expected = new Uint8Array( [ 1, 1, 1, 1 ] ); actual = arr.filter( predicate ); t.strictEqual( instanceOf( actual, BooleanArray ), true, 'returns expected value' ); From 92a3dde94e402e02772dbe3a803ce13922068495 Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 15 Jul 2024 16:12:12 -0700 Subject: [PATCH 8/8] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/bool/test/test.filter.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/node_modules/@stdlib/array/bool/test/test.filter.js b/lib/node_modules/@stdlib/array/bool/test/test.filter.js index 98244fa191d..3cdb2c360df 100644 --- a/lib/node_modules/@stdlib/array/bool/test/test.filter.js +++ b/lib/node_modules/@stdlib/array/bool/test/test.filter.js @@ -162,6 +162,26 @@ tape( 'the method copies all elements to a new array if all elements satisfy a t } }); +tape( 'the method returns an empty array if no elements satisfy a test condition', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ false, false, false, false ] ); + expected = new Uint8Array( [] ); + actual = arr.filter( predicate ); + + t.strictEqual( instanceOf( actual, BooleanArray ), true, 'returns expected value' ); + t.notEqual( actual, arr, 'returns a new instance' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( reinterpretBoolean( actual, 0 ), expected, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v === true ); + } +}); + tape( 'the method supports providing an execution context', function test( t ) { var expected; var actual;