diff --git a/lib/node_modules/@stdlib/array/bool/README.md b/lib/node_modules/@stdlib/array/bool/README.md index 84ca762e6ba3..5af463d1749c 100644 --- a/lib/node_modules/@stdlib/array/bool/README.md +++ b/lib/node_modules/@stdlib/array/bool/README.md @@ -865,6 +865,82 @@ A few notes: - If a target array cannot accommodate all values (i.e., the length of source array plus `i` exceeds the target array length), the method throws an error. - If provided a [typed array][@stdlib/array/typed] which shares an [`ArrayBuffer`][@stdlib/array/buffer] with the target array, the method will intelligently copy the source range to the destination range. + + +#### BooleanArray.prototype.slice( \[start\[, end]] ) + +Copies a portion of a typed array to a new typed array. + +```javascript +var arr = new BooleanArray( 5 ); + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); +arr.set( false, 3 ); +arr.set( true, 4 ); + +var out = arr.slice(); +// returns + +var len = out.length; +// returns 5 + +var bool = out.get( 0 ); +// returns true + +bool = out.get( len-1 ); +// returns true +``` + +By default, the method returns a typed array beginning with the first array element. To specify an alternative array index at which to begin, provide a `start` index (inclusive). + +```javascript +var arr = new BooleanArray( 5 ); + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); +arr.set( false, 3 ); +arr.set( true, 4 ); + +var out = arr.slice( 1 ); +// returns + +var len = out.length; +// returns 4 + +var bool = out.get( 0 ); +// returns false + +bool = out.get( len-1 ); +// returns true +``` + +By default, the method returns a typed array which includes all array elements after `start`. To limit the number of array elements after `start`, provide an `end` index (exclusive). + +```javascript +var arr = new BooleanArray( 5 ); + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); +arr.set( false, 3 ); +arr.set( true, 4 ); + +var out = arr.slice( 1, -2 ); +// returns + +var len = out.length; +// returns 2 + +var bool = out.get( 0 ); +// returns false + +bool = out.get( len-1 ); +// returns true +``` + #### BooleanArray.prototype.some( predicate\[, thisArg] ) @@ -968,6 +1044,82 @@ The function should return a number where: + + +#### BooleanArray.prototype.subarray( \[begin\[, end]] ) + +Creates a new typed array view over the same underlying [`ArrayBuffer`][@stdlib/array/buffer] and with the same underlying data type as the host array. + +```javascript +var arr = new BooleanArray( 5 ); + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); +arr.set( false, 3 ); +arr.set( true, 4 ); + +var subarr = arr.subarray(); +// returns + +var len = subarr.length; +// returns 5 + +var bool = subarr.get( 0 ); +// returns true + +bool = subarr.get( len-1 ); +// returns true +``` + +By default, the method creates a typed array view beginning with the first array element. To specify an alternative array index at which to begin, provide a `begin` index (inclusive). + +```javascript +var arr = new BooleanArray( 5 ); + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); +arr.set( false, 3 ); +arr.set( true, 4 ); + +var subarr = arr.subarray( 1 ); +// returns + +var len = subarr.length; +// returns 4 + +var bool = subarr.get( 0 ); +// returns false + +bool = subarr.get( len-1 ); +// returns true +``` + +By default, the method creates a typed array view which includes all array elements after `begin`. To limit the number of array elements after `begin`, provide an `end` index (exclusive). + +```javascript +var arr = new BooleanArray( 5 ); + +arr.set( true, 0 ); +arr.set( false, 1 ); +arr.set( true, 2 ); +arr.set( false, 3 ); +arr.set( true, 4 ); + +var subarr = arr.subarray( 1, -2 ); +// returns + +var len = subarr.length; +// returns 2 + +var bool = subarr.get( 0 ); +// returns false + +bool = subarr.get( len-1 ); +// returns true +``` + #### 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.slice.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.slice.js new file mode 100644 index 000000000000..06f31754d2ff --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.slice.js @@ -0,0 +1,51 @@ +/** +* @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+':slice', 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.slice(); + 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.slice.length.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.slice.length.js new file mode 100644 index 000000000000..5b077fe4f4d9 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.slice.length.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 Boolean = require( '@stdlib/boolean/ctor' ); +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( Boolean( i%2 ) ); + } + 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.slice(); + 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+':slice:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.subarray.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.subarray.js new file mode 100644 index 000000000000..60f38ba51637 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.subarray.js @@ -0,0 +1,51 @@ +/** +* @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+':subarray', 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.subarray(); + 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.subarray.length.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.subarray.length.js new file mode 100644 index 000000000000..17ec983b8825 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.subarray.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 isBooleanArray = require( '@stdlib/assert/is-booleanarray' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var Boolean = require( '@stdlib/boolean/ctor' ); +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( Boolean( i%2 ) ); + } + 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.subarray(); + 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+':subarray: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 f676f1cd7a8a..885f03d44f53 100644 --- a/lib/node_modules/@stdlib/array/bool/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/bool/docs/repl.txt @@ -665,6 +665,38 @@ true +{{alias}}.prototype.slice( [start[, end]] ) + Copies a portion of a typed array to a new typed array. + + Parameters + ---------- + 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 + New typed array. + + Examples + -------- + > var arr = new {{alias}}( [ true, false, true, false, true ] ) + + > var out = arr.slice( 1 ) + + > var len = out.length + 4 + > var v = out.get( 0 ) + false + > v = out.get( 1 ) + true + + {{alias}}.prototype.some( predicate[, thisArg] ) Returns a boolean indicating whether at least one element passes a test. @@ -735,6 +767,39 @@ false +{{alias}}.prototype.subarray( [begin[, end]] ) + Creates a new typed array view over the same underlying `ArrayBuffer` and + with the same underlying data type as the host array. + + Parameters + ---------- + begin: 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 + New typed array view. + + Examples + -------- + > var arr = new {{alias}}( [ true, false, true, false, true ] ) + + > var out = arr.subarray( 1, 3 ) + + > var len = out.length + 2 + > var v = out.get( 0 ) + false + > v = out.get( 1 ) + 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 652a61db8c34..62cba3aedfdc 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 @@ -585,6 +585,49 @@ declare class BooleanArray implements BooleanArrayInterface { */ set( value: ArrayLike | any, i?: number ): void; + /** + * Copies a portion of a typed array to a new typed array. + * + * @param start - starting index (inclusive) + * @param end - ending index (exclusive) + * @throws indices must be integers + * @returns output array + * + * @example + * var arr = new BooleanArray( 5 ); + * + * arr.set( true, 0 ); + * arr.set( false, 1 ); + * arr.set( true, 2 ); + * arr.set( false, 3 ); + * arr.set( true, 4 ); + * + * var out = arr.slice(); + * // returns + * + * var len = out.length; + * // returns 5 + * + * var bool = out.get( 0 ); + * // returns true + * + * bool = out.get( len-1 ); + * // returns true + * + * out = arr.slice( 1, -2 ); + * // returns + * + * len = out.length; + * // returns 2 + * + * bool = out.get( 0 ); + * // returns false + * + * bool = out.get( len-1 ); + * // returns true + */ + slice( start?: number, end?: number ): BooleanArray; + /** * Tests whether at least one element in an array passes a test implemented by a predicate function. * @@ -647,6 +690,49 @@ declare class BooleanArray implements BooleanArrayInterface { */ sort( compareFcn: CompareFcn ): BooleanArray; + /** + * Creates a new typed array view over the same underlying `ArrayBuffer` and with the same underlying data type as the host array. + * + * @param begin - starting index (inclusive) + * @param end - ending index (exclusive) + * @throws indices must be integers + * @returns subarray + * + * @example + * var arr = new BooleanArray( 5 ); + * + * arr.set( true, 0 ); + * arr.set( false, 1 ); + * arr.set( true, 2 ); + * arr.set( false, 3 ); + * arr.set( true, 4 ); + * + * var subarr = arr.subarray(); + * // returns + * + * var len = subarr.length; + * // returns 5 + * + * var bool = subarr.get( 0 ); + * // returns true + * + * bool = subarr.get( len-1 ); + * // returns true + * + * subarr = arr.subarray( 1, -2 ); + * // returns + * + * len = subarr.length; + * // returns 2 + * + * bool = subarr.get( 0 ); + * // returns false + * + * bool = subarr.get( len-1 ); + * // returns true + */ + subarray( begin?: number, end?: number ): BooleanArray; + /** * 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 10034d18d19e..e3d7d5e14f98 100644 --- a/lib/node_modules/@stdlib/array/bool/lib/main.js +++ b/lib/node_modules/@stdlib/array/bool/lib/main.js @@ -1131,6 +1131,107 @@ setReadOnly( BooleanArray.prototype, 'set', function set( value ) { buf[ idx ] = ( value ) ? 1 : 0; }); +/** +* Copies a portion of a typed array to a new typed array. +* +* @name slice +* @memberof BooleanArray.prototype +* @type {Function} +* @param {integer} [begin] - start index (inclusive) +* @param {integer} [end] - end index (exclusive) +* @throws {TypeError} `this` must be a boolean array +* @throws {TypeError} first argument must be integer +* @throws {TypeError} second argument must be integer +* @returns {BooleanArray} boolean array +* +* @example +* var arr = new BooleanArray( 5 ); +* +* arr.set( true, 0 ); +* arr.set( false, 1 ); +* arr.set( true, 2 ); +* arr.set( false, 3 ); +* arr.set( true, 4 ); +* +* var out = arr.slice(); +* // returns +* +* var len = out.length; +* // returns 5 +* +* var bool = out.get( 0 ); +* // returns true +* +* bool = out.get( len-1 ); +* // returns true +* +* out = arr.slice( 1, -2 ); +* // returns +* +* len = out.length; +* // returns 2 +* +* bool = out.get( 0 ); +* // returns false +* +* bool = out.get( len-1 ); +* // returns true +*/ +setReadOnly( BooleanArray.prototype, 'slice', function slice( begin, end ) { + var outlen; + var outbuf; + var out; + var buf; + var len; + var i; + + if ( !isBooleanArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a boolean array.' ); + } + buf = this._buffer; + len = this._length; + if ( arguments.length === 0 ) { + begin = 0; + end = len; + } else { + if ( !isInteger( begin ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', begin ) ); + } + if ( begin < 0 ) { + begin += len; + if ( begin < 0 ) { + begin = 0; + } + } + if ( arguments.length === 1 ) { + end = len; + } else { + if ( !isInteger( end ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', end ) ); + } + if ( end < 0 ) { + end += len; + if ( end < 0 ) { + end = 0; + } + } else if ( end > len ) { + end = len; + } + } + } + if ( begin < end ) { + outlen = end - begin; + } else { + outlen = 0; + } + out = new this.constructor( outlen ); + outbuf = out._buffer; // eslint-disable-line no-underscore-dangle + for ( i = 0; i < outlen; i++ ) { + outbuf[ i ] = buf[ i+begin ]; + } + return out; +}); + /** * Tests whether at least one element in an array passes a test implemented by a predicate function. * @@ -1248,6 +1349,104 @@ setReadOnly( BooleanArray.prototype, 'sort', function sort( compareFcn ) { } }); +/** +* Creates a new typed array view over the same underlying `ArrayBuffer` and with the same underlying data type as the host array. +* +* @name subarray +* @memberof BooleanArray.prototype +* @type {Function} +* @param {integer} [begin] - start index (inclusive) +* @param {integer} [end] - end index (exclusive) +* @throws {TypeError} `this` must be a boolean array +* @throws {TypeError} first argument must be an integer +* @throws {TypeError} second argument must be an integer +* @returns {BooleanArray} subarray +* +* @example +* var arr = new BooleanArray( 5 ); +* +* arr.set( true, 0 ); +* arr.set( false, 1 ); +* arr.set( true, 2 ); +* arr.set( false, 3 ); +* arr.set( true, 4 ); +* +* var subarr = arr.subarray(); +* // returns +* +* var len = subarr.length; +* // returns 5 +* +* var bool = subarr.get( 0 ); +* // returns true +* +* bool = subarr.get( len-1 ); +* // returns true +* +* subarr = arr.subarray( 1, -2 ); +* // returns +* +* len = subarr.length; +* // returns 2 +* +* bool = subarr.get( 0 ); +* // returns false +* +* bool = subarr.get( len-1 ); +* // returns true +*/ +setReadOnly( BooleanArray.prototype, 'subarray', function subarray( begin, end ) { + var offset; + var buf; + var len; + + if ( !isBooleanArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a boolean array.' ); + } + buf = this._buffer; + len = this._length; + if ( arguments.length === 0 ) { + begin = 0; + end = len; + } else { + if ( !isInteger( begin ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', begin ) ); + } + if ( begin < 0 ) { + begin += len; + if ( begin < 0 ) { + begin = 0; + } + } + if ( arguments.length === 1 ) { + end = len; + } else { + if ( !isInteger( end ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', end ) ); + } + if ( end < 0 ) { + end += len; + if ( end < 0 ) { + end = 0; + } + } else if ( end > len ) { + end = len; + } + } + } + if ( begin >= len ) { + len = 0; + offset = buf.byteLength; + } else if ( begin >= end ) { + len = 0; + offset = buf.byteOffset + ( begin*BYTES_PER_ELEMENT ); + } else { + len = end - begin; + offset = buf.byteOffset + ( begin*BYTES_PER_ELEMENT ); + } + return new this.constructor( buf.buffer, offset, ( len < 0 ) ? 0 : len ); +}); + /** * Returns a new typed array containing the elements in reversed order. * diff --git a/lib/node_modules/@stdlib/array/bool/test/test.slice.js b/lib/node_modules/@stdlib/array/bool/test/test.slice.js new file mode 100644 index 000000000000..50cff728f3b4 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/test/test.slice.js @@ -0,0 +1,271 @@ +/** +* @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 `slice` method', function test( t ) { + t.strictEqual( hasOwnProp( BooleanArray.prototype, 'slice' ), true, 'has property' ); + t.strictEqual( isFunction( BooleanArray.prototype.slice ), 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.slice.call( value ); + }; + } +}); + +tape( 'the method throws an error if provided a first 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, + {}, + [] + ]; + 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.slice( 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, + {}, + [] + ]; + 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.slice( 0, value ); + }; + } +}); + +tape( 'the method returns an empty typed array if operating on an empty boolean array', function test( t ) { + var arr; + var out; + + arr = new BooleanArray(); + out = arr.slice(); + + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'if called without arguments, the method returns a typed array containing the same elements as the original array', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ true, false, true, false, true ] ); + expected = new Uint8Array( [ 1, 0, 1, 0, 1 ] ); + actual = arr.slice(); + + 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(); +}); + +tape( 'if called with one argument, the method returns a typed array containing elements starting from a specified beginning index (inclusive)', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ true, false, true, false, true ] ); + expected = new Uint8Array( [ 0, 1, 0, 1 ] ); + actual = arr.slice( 1 ); + + 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(); +}); + +tape( 'if provided two arguments, the method returns a typed array containing elements starting from a specified beginning index (inclusive) and ending at a specified stop index (exclusive)', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ true, false, true, false, true ] ); + expected = new Uint8Array( [ 0, 1 ] ); + actual = arr.slice( 1, 3 ); + + 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' ); + + arr = new BooleanArray( [ true, false, true, false, true ] ); + expected = new Uint8Array( [ 0, 1, 0, 1 ] ); + actual = arr.slice( 1, 30 ); + + 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(); +}); + +tape( 'the method resolves negative indices relative to the last element', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ true, false, true, false, true ] ); + + expected = new Uint8Array( [ 1, 0 ] ); + actual = arr.slice( -3, -1 ); + 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' ); + + expected = new Uint8Array( [ 1, 0, 1 ] ); + actual = arr.slice( -30, -2 ); + 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(); +}); + +tape( 'the method returns an empty typed array if a resolved beginning index exceeds a resolved ending index', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ true, false, true, false, true ] ); + expected = new Uint8Array( [] ); + actual = arr.slice( 2, 0 ); + + 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(); +}); + +tape( 'the method returns an empty typed array if a resolved beginning index exceeds the maximum array index', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ true, false, true, false ] ); + expected = new Uint8Array( [] ); + actual = arr.slice( 5 ); + + 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(); +}); + +tape( 'the method returns an empty typed array if a resolved ending index is less than or equal to zero', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0, 4.0, -4.0 ] ); + expected = new Uint8Array( [] ); + + actual = arr.slice( 2, -8 ); + t.strictEqual( instanceOf( actual, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length/2, 'returns expected value' ); + t.deepEqual( reinterpretBoolean( actual, 0 ), expected, 'returns expected value' ); + + actual = arr.slice( 1, 0 ); + t.strictEqual( instanceOf( actual, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length/2, 'returns expected value' ); + t.deepEqual( reinterpretBoolean( actual, 0 ), expected, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/bool/test/test.subarray.js b/lib/node_modules/@stdlib/array/bool/test/test.subarray.js new file mode 100644 index 000000000000..448043ec24e2 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/test/test.subarray.js @@ -0,0 +1,272 @@ +/** +* @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 `subarray` method', function test( t ) { + t.strictEqual( hasOwnProp( BooleanArray.prototype, 'subarray' ), true, 'has property' ); + t.strictEqual( isFunction( BooleanArray.prototype.subarray ), 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.subarray.call( value ); + }; + } +}); + +tape( 'the method throws an error if provided a first 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, + {}, + [] + ]; + 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.subarray( 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, + {}, + [] + ]; + 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.subarray( 0, value ); + }; + } +}); + +tape( 'the method returns empty array if operating on an empty boolean array', function test( t ) { + var arr; + var out; + + arr = new BooleanArray(); + out = arr.subarray(); + + t.strictEqual( out.buffer, arr.buffer, 'returns expected value' ); + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'if called without arguments, the method returns a view containing the same elements as the original array', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ true, false, true, false, true ] ); + expected = new Uint8Array( [ 1, 0, 1, 0, 1 ] ); + actual = arr.subarray(); + + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + 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(); +}); + +tape( 'if called with one argument, the method returns a view containing elements starting from a specified beginning index (inclusive)', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ true, false, true, false, true ] ); + expected = new Uint8Array( [ 0, 1, 0, 1 ] ); + actual = arr.subarray( 1 ); + + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + 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(); +}); + +tape( 'if provided two arguments, the method returns a view containing elements starting from a specified beginning index (inclusive) and ending at a specified stop index (exclusive)', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ true, false, true, false, true ] ); + expected = new Uint8Array( [ 0, 1 ] ); + actual = arr.subarray( 1, 3 ); + + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + 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(); +}); + +tape( 'the method resolves negative indices relative to the last element', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ true, false, true, false, true ] ); + + expected = new Uint8Array( [ 1, 0 ] ); + actual = arr.subarray( -3, -1 ); + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + 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' ); + + expected = new Uint8Array( [ 1, 0, 1 ] ); + actual = arr.subarray( -30, -2 ); + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + 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(); +}); + +tape( 'the method returns an empty view if a resolved beginning index exceeds a resolved ending index', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ true, false, true, false, true ] ); + expected = new Uint8Array( [] ); + actual = arr.subarray( 2, 0 ); + + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + 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(); +}); + +tape( 'the method returns an empty view if a resolved beginning index exceeds the maximum array index', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ true, false, true, false ] ); + expected = new Uint8Array( [] ); + actual = arr.subarray( 5 ); + + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + 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(); +}); + +tape( 'the method returns an empty view if a resolved ending index is less than or equal to zero', function test( t ) { + var expected; + var actual; + var arr; + + arr = new BooleanArray( [ true, false, true, false, true ] ); + expected = new Uint8Array( [] ); + + actual = arr.subarray( 2, -8 ); + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + 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' ); + + actual = arr.subarray( 1, 0 ); + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + 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(); +});