diff --git a/CHANGELOG.md b/CHANGELOG.md index 9991973a..8a5f9496 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@
-## Unreleased (2024-09-24) +## Unreleased (2024-09-25)
@@ -166,6 +166,40 @@ +
+ +#### [@stdlib/array/base/cuany-by-right](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/cuany-by-right) + +
+ +
+ +##### Features + +- [`5742947`](https://github.com/stdlib-js/stdlib/commit/5742947f31198648b2f753e1f9322367c9e3ae06) - add `array/base/cuany-by-right` [(#2667)](https://github.com/stdlib-js/stdlib/pull/2667) + +
+ + + +
+ +##### Closed Issues + +This release closes the following issue: + +[#2327](https://github.com/stdlib-js/stdlib/issues/2327) + +
+ + + +
+ +
+ + +
#### [@stdlib/array/base/cuevery-by](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/cuevery-by) @@ -444,13 +478,26 @@ +
+ +### Closed Issues + +This release closes the following issue: + +[#2327](https://github.com/stdlib-js/stdlib/issues/2327) + +
+ + +
### Contributors -A total of 6 people contributed to this release. Thank you to the following contributors: +A total of 7 people contributed to this release. Thank you to the following contributors: - Athan Reines +- DebashisMaharana - HarshaNP - Kaif Mohd - Philipp Burckhardt @@ -467,6 +514,7 @@ A total of 6 people contributed to this release. Thank you to the following cont
+- [`5742947`](https://github.com/stdlib-js/stdlib/commit/5742947f31198648b2f753e1f9322367c9e3ae06) - **feat:** add `array/base/cuany-by-right` [(#2667)](https://github.com/stdlib-js/stdlib/pull/2667) _(by DebashisMaharana, Philipp Burckhardt)_ - [`8f72b43`](https://github.com/stdlib-js/stdlib/commit/8f72b432c0fc81a78641d5689722ecc9671c6f02) - **style:** add missing spaces around parentheses _(by Philipp Burckhardt)_ - [`73266bf`](https://github.com/stdlib-js/stdlib/commit/73266bf9d7f82198bd752dafccedbc017d8c8bb1) - **feat:** add `array/base/cuany-by` [(#2818)](https://github.com/stdlib-js/stdlib/pull/2818) _(by yaswanth, Philipp Burckhardt)_ - [`e3078e5`](https://github.com/stdlib-js/stdlib/commit/e3078e56df8858a8f79fb3f3ded3fc76da955db5) - **feat:** add `array/base/cuevery-by` package _(by Kaif Mohd, Philipp Burckhardt)_ diff --git a/base/cuany-by-right/README.md b/base/cuany-by-right/README.md new file mode 100644 index 00000000..301eb769 --- /dev/null +++ b/base/cuany-by-right/README.md @@ -0,0 +1,152 @@ + + +# cuanyByRight + +> Cumulatively test whether at least one element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left. + +
+ +## Usage + +```javascript +var cuanyByRight = require( '@stdlib/array/base/cuany-by-right' ); +``` + +#### cuanyByRight( x, predicate\[, thisArg] ) + +Cumulatively tests whether at least one element in a provided array passes a test implemented by a `predicate` function, while iterating from right-to-left. + +```javascript +function isPositive( value ) { + return ( value > 0 ); +} + +var x = [ 0, 0, 0, 1, 0 ]; + +var y = cuanyByRight( x, isPositive ); +// returns [ false, true, true, true, true ] +``` + +The `predicate` function is provided three arguments: + +- **value**: current array element. +- **index**: current array element index. +- **arr**: input array. + +To set the `predicate` function execution context, provide a `thisArg`. + +```javascript +function isPositive( value ) { + this.count += 1; + return ( value > 0 ); +} + +var x = [ 0, 1, 0, 0, 0 ]; + +var context = { + 'count': 0 +}; + +var out = cuanyByRight( x, isPositive, context ); +// returns [ false, false, false, true, true ] + +var cnt = context.count; +// returns 4 +``` + +#### cuanyByRight.assign( x, out, stride, offset, predicate\[, thisArg] ) + +Cumulatively tests whether at least one element in a provided array passes a test implemented by a `predicate` function, while iterating from right-to-left, and assigns results to a provided output array. + +```javascript +function isPositive( value ) { + return ( value > 0 ); +} + +var x = [ 0, 1, 0, 0, 0 ]; +var y = [ false, null, false, null, false, null, false, null, false, null ]; + +var out = cuanyByRight.assign( x, y, 2, 0, isPositive ); +// returns [ false, null, false, null, false, null, true, null, true, null ] + +var bool = ( out === y ); +// returns true +``` + +The function supports the following parameters: + +- **x**: input array. +- **out**: output array. +- **stride**: output array stride. +- **offset**: output array offset. +- **predicate**: test function. + +
+ + + +
+ +
+ + + +
+ +## Examples + + + +```javascript +function isPositive( value ) { + return ( value > 0 ); +} + +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var cuanyByRight = require( '@stdlib/array/base/cuany-by-right' ); + +// Create an array of random values: +var x = bernoulli( 10, 0.1 ); +console.log( x ); + +var out = cuanyByRight( x, isPositive ); +console.log( out ); +``` + +
+ + + + + + + + + + + + + + diff --git a/base/cuany-by-right/benchmark/benchmark.assign.length.js b/base/cuany-by-right/benchmark/benchmark.assign.length.js new file mode 100644 index 00000000..8a5f7298 --- /dev/null +++ b/base/cuany-by-right/benchmark/benchmark.assign.length.js @@ -0,0 +1,99 @@ +/** +* @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 isArray = require( '@stdlib/assert/is-array' ); +var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; +var filled = require( './../../../base/filled' ); +var pkg = require( './../package.json' ).name; +var cuanyByRight = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = filled( 1.5, len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var y; + var v; + var i; + + y = filled( false, len ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = cuanyByRight.assign( x, y, 1, 0, isPositiveInteger ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + 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+':assign:len='+len, f ); + } +} + +main(); diff --git a/base/cuany-by-right/benchmark/benchmark.js b/base/cuany-by-right/benchmark/benchmark.js new file mode 100644 index 00000000..e6515d80 --- /dev/null +++ b/base/cuany-by-right/benchmark/benchmark.js @@ -0,0 +1,52 @@ +/** +* @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 isArray = require( '@stdlib/assert/is-array' ); +var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var cuanyByRight = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var x; + var i; + var v; + + x = [ 1, 1, 0, 0, 0 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = cuanyByRight( x, isPositiveInteger ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/base/cuany-by-right/benchmark/benchmark.length.js b/base/cuany-by-right/benchmark/benchmark.length.js new file mode 100644 index 00000000..339bcf3d --- /dev/null +++ b/base/cuany-by-right/benchmark/benchmark.length.js @@ -0,0 +1,96 @@ +/** +* @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 isArray = require( '@stdlib/assert/is-array' ); +var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; +var filled = require( './../../../base/filled' ); +var pkg = require( './../package.json' ).name; +var cuanyByRight = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = filled( 0, len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = cuanyByRight( x, isPositiveInteger ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + 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+':len='+len, f ); + } +} + +main(); diff --git a/base/cuany-by-right/docs/repl.txt b/base/cuany-by-right/docs/repl.txt new file mode 100644 index 00000000..d22386ea --- /dev/null +++ b/base/cuany-by-right/docs/repl.txt @@ -0,0 +1,83 @@ +{{alias}}( x, predicate[, thisArg] ) + Cumulatively tests whether at least one array element in a provided array + passes a test implemented by a predicate function, while iterating from + right-to-left. + + The predicate function is provided three arguments: + + - value: current array element. + - index: current array element index. + - arr: the input array. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + predicate: Function + Predicate function. + + thisArg: any (optional) + Execution context. + + Returns + ------- + out: Array + Output array. + + Examples + -------- + > function isPositive( v ) { return ( v > 0 ); }; + > var x = [ 1, 1, 0, 0, 0 ]; + > var y = {{alias}}( x, isPositive ) + [ false, false, false, true, true ] + + +{{alias}}.assign( x, out, stride, offset, predicate[, thisArg] ) + Cumulatively tests whether at least one array element in a provided array + passes a test implemented by a predicate function, while iterating from + right-to-left, and assigns the results to the provided output array. + + The predicate function is provided three arguments: + + - value: current array element. + - index: current array element index. + - arr: the input array. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + out: ArrayLikeObject + Output array. + + stride: integer + Output array stride. + + offset: integer + Output array offset. + + predicate: Function + Predicate function. + + thisArg: any (optional) + Execution context. + + Returns + ------- + out: ArrayLikeObject + Output array. + + Examples + -------- + > function isPositive( v ) { return ( v > 0 ); }; + > var x = [ 1, 1, 0, 0 ]; + > var out = [ false, null, false, null, false, null, false, null ]; + > var arr = {{alias}}.assign( x, out, 2, 0, isPositive ) + [ false, null, ..., true, null ] + > var bool = ( arr === out ) + true + + See Also + -------- diff --git a/base/cuany-by-right/docs/types/index.d.ts b/base/cuany-by-right/docs/types/index.d.ts new file mode 100644 index 00000000..571ed727 --- /dev/null +++ b/base/cuany-by-right/docs/types/index.d.ts @@ -0,0 +1,197 @@ +/* +* @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. +*/ + +/// + +import { Collection, AccessorArrayLike, TypedArray, BooleanArray } from '@stdlib/types/array'; + +/** +* Checks whether an element in a collection passes a test. +* +* @returns boolean indicating whether an element in a collection passes a test +*/ +type Nullary = ( this: U ) => boolean; + +/** +* Checks whether an element in a collection passes a test. +* +* @param value - collection value +* @returns boolean indicating whether an element in a collection passes a test +*/ +type Unary = ( this: U, value: T ) => boolean; + +/** +* Checks whether an element in a collection passes a test. +* +* @param value - collection value +* @param index - collection index +* @returns boolean indicating whether an element in a collection passes a test +*/ +type Binary = ( this: U, value: T, index: number ) => boolean; + +/** +* Checks whether an element in a collection passes a test. +* +* @param value - collection value +* @param index - collection index +* @param collection - input collection +* @returns boolean indicating whether an element in a collection passes a test +*/ +type Ternary = ( this: U, value: T, index: number, collection: Collection ) => boolean; + +/** +* Checks whether an element in a collection passes a test. +* +* @param value - collection value +* @param index - collection index +* @param collection - input collection +* @returns boolean indicating whether an element in a collection passes a test +*/ +type Predicate = Nullary | Unary | Binary | Ternary; + +/** +* Interface describing `cuanyByRight`. +*/ +interface CuanyByRight { + /** + * Cumulatively tests whether at least one array element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left. + * + * @param x - input array + * @param predicate - test function + * @param thisArg - execution context + * @returns output array + * + * @example + * function isPositive( v ) { + * return v > 0; + * } + * var x = [ 0, 1, 1, 0, 0 ]; + * + * var y = cuanyByRight( x, isPositive ); + * // returns [ false, false, true, true, true ] + */ + ( x: Collection | AccessorArrayLike, predicate: Predicate, thisArg?: ThisParameterType> ): Array; + + /** + * Cumulatively tests whether at least one array element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left, and assigns the results to the provided output array. + * + * @param x - input array + * @param y - output array + * @param stride - output array stride + * @param offset - output array offset + * @param predicate - test function + * @param thisArg - execution context + * @returns output array + * + * @example + * function isPositive( v ) { + * return v > 0; + * } + * var x = [ 1, 1, 0, 0, 0 ]; + * var y = [ false, null, false, null, false, null, false, null, false, null ]; + * + * var arr = cuanyByRight.assign( x, y, 2, 0, isPositive ); + * // returns [ false, null, false, null, false, null, true, null, true, null ] + */ + assign( x: Collection | AccessorArrayLike, out: Array, stride: number, offset: number, predicate: Predicate, thisArg?: ThisParameterType> ): Array; + + /** + * Cumulatively tests whether at least one array element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left, and assigns the results to the provided output array. + * + * @param x - input array + * @param out - output array + * @param stride - output array stride + * @param offset - output array offset + * @param predicate - test function + * @param thisArg - execution context + * @returns output array + * + * @example + * var BooleanArray = require( './../../../../bool' ); + * + * function isPositive( v ) { + * return ( v > 0 ); + * } + * var x = [ 0, 0, 0, 1, 0 ]; + * var y = new BooleanArray( [ false, false, false, false, false, false, false, false, false, false ] ); + * + * var arr = cuanyByRight.assign( x, y, 2, 0, isPositive ); + * // returns + * + * var v = arr.get( 6 ); + * // returns true + */ + assign( x: Collection | AccessorArrayLike, out: U, stride: number, offset: number, predicate: Predicate, thisArg?: ThisParameterType> ): U; + + /** + * Cumulatively tests whether at least one array element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left, and assigns the results to the provided output array. + * + * @param x - input array + * @param out - output array + * @param stride - output array stride + * @param offset - output array offset + * @param predicate - test function + * @param thisArg - execution context + * @returns output array + * + * @example + * function isPositive( v ) { + * return ( v > 0 ); + * } + * var x = [ 0, 0, 0, 1, 0 ]; + * var y = [ false, null, false, null, false, null, false, null, false, null ]; + * + * var arr = cuanyByRight.assign( x, y, 2, 0, isPositive ); + * // returns [ false, null, false, null, false, null, true, null, true, null ] + */ + assign( x: Collection | AccessorArrayLike, out: Collection | AccessorArrayLike, stride: number, offset: number, predicate: Predicate, thisArg?: ThisParameterType> ): Collection | AccessorArrayLike; +} + +/** +* Cumulatively tests whether at least one array element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left. +* +* @param x - input array +* @param predicate - test function +* @param thisArg - execution context +* @returns output array +* +* @example +* function isPositive( v ) { +* return v > 0; +* } +* var x = [ 1, 1, 0, 0, 0 ]; +* +* var result = cuanyByRight( x, isPositive ); +* // returns [ false, false, false, true, true ] +* +* @example +* function isPositive( v ) { +* return v > 0; +* } +* var x = [ 0, 1, 1, 0, 0 ]; +* var y = [ false, null, false, null, false, null, false, null, false, null ]; +* +* var arr = cuanyByRight.assign( x, y, 2, 0, isPositive ); +* // returns [ false, null, false, null, false, null, true, null, true, null ] +*/ +declare var cuanyByRight: CuanyByRight; + + +// EXPORTS // + +export = cuanyByRight; diff --git a/base/cuany-by-right/docs/types/test.ts b/base/cuany-by-right/docs/types/test.ts new file mode 100644 index 00000000..8a012f9a --- /dev/null +++ b/base/cuany-by-right/docs/types/test.ts @@ -0,0 +1,220 @@ +/* +* @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. +*/ + + +import cuanyByRight = require( './index' ); + +/** +* Tests whether a value is positive. +* +* @param value - input value +* @returns boolean indicating whether an element is positive +*/ +function isPositive( value: number ): boolean { + return ( value > 0 ); +} + + +// TESTS // + +// The function returns an array... +{ + cuanyByRight( [ 1, 2, 3 ], isPositive ); // $ExpectType boolean[] + cuanyByRight( new Float64Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType boolean[] + cuanyByRight( new Float32Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType boolean[] + cuanyByRight( new Int32Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType boolean[] + cuanyByRight( new Int16Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType boolean[] + cuanyByRight( new Int8Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType boolean[] + cuanyByRight( new Uint32Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType boolean[] + cuanyByRight( new Uint16Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType boolean[] + cuanyByRight( new Uint8Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType boolean[] + cuanyByRight( new Uint8ClampedArray( [ 1, 2, 3 ] ), isPositive ); // $ExpectType boolean[] + + cuanyByRight( [ 1, 2, 3 ], isPositive, {} ); // $ExpectType boolean[] + cuanyByRight( new Float64Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType boolean[] + cuanyByRight( new Float32Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType boolean[] + cuanyByRight( new Int32Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType boolean[] + cuanyByRight( new Int16Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType boolean[] + cuanyByRight( new Int8Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType boolean[] + cuanyByRight( new Uint32Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType boolean[] + cuanyByRight( new Uint16Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType boolean[] + cuanyByRight( new Uint8Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType boolean[] + cuanyByRight( new Uint8ClampedArray( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType boolean[] +} + +// The compiler throws an error if the function is provided a first argument which is not like a function.. +{ + const x = [ 1, 2, 3, 4 ]; + + cuanyByRight( x, null ); // $ExpectError + cuanyByRight( x, {} ); // $ExpectError + cuanyByRight( x, [] ); // $ExpectError + cuanyByRight( x, '' ); // $ExpectError + cuanyByRight( x, undefined ); // $ExpectError + + cuanyByRight( x, null, {} ); // $ExpectError + cuanyByRight( x, {}, {} ); // $ExpectError + cuanyByRight( x, [], {} ); // $ExpectError + cuanyByRight( x, '', {} ); // $ExpectError + cuanyByRight( x, undefined, {} ); // $ExpectError + +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + cuanyByRight(); // $ExpectError + cuanyByRight( [] ); // $ExpectError + cuanyByRight( [ 1, 2 ], isPositive, {}, '' ); // $ExpectError +} + +// Attached to the main export is an `assign` method which returns a collection... +{ + const x = [ 0, 0, 0, 1, 0 ]; + const y = [ false, null, false, null, false, null, false, null, false, null ]; + + cuanyByRight.assign( x, y, 2, 0, isPositive ); // $ExpectType (boolean | null)[] + cuanyByRight.assign( x, y, 2, 0, isPositive, {} ); // $ExpectType (boolean | null)[] + cuanyByRight.assign( x, new Float64Array( 4 ), 1, 0, isPositive ); // $ExpectType Float64Array + cuanyByRight.assign( x, new Float32Array( 4 ), 1, 0, isPositive ); // $ExpectType Float32Array + cuanyByRight.assign( x, new Int32Array( 4 ), 1, 0, isPositive ); // $ExpectType Int32Array + cuanyByRight.assign( x, new Int16Array( 4 ), 1, 0, isPositive ); // $ExpectType Int16Array + cuanyByRight.assign( x, new Int8Array( 4 ), 1, 0, isPositive ); // $ExpectType Int8Array + cuanyByRight.assign( x, new Uint32Array( 4 ), 1, 0, isPositive ); // $ExpectType Uint32Array + cuanyByRight.assign( x, new Uint16Array( 4 ), 1, 0, isPositive ); // $ExpectType Uint16Array + cuanyByRight.assign( x, new Uint8Array( 4 ), 1, 0, isPositive ); // $ExpectType Uint8Array + cuanyByRight.assign( x, new Uint8ClampedArray( 4 ), 1, 0, isPositive ); // $ExpectType Uint8ClampedArray + + cuanyByRight.assign( x, [ 0, 0, 0, 0 ], 1, 0, isPositive, {} ); // $ExpectType (number | boolean)[] + cuanyByRight.assign( x, new Float64Array( 4 ), 1, 0, isPositive, {} ); // $ExpectType Float64Array + cuanyByRight.assign( x, new Float32Array( 4 ), 1, 0, isPositive, {} ); // $ExpectType Float32Array + cuanyByRight.assign( x, new Int32Array( 4 ), 1, 0, isPositive, {} ); // $ExpectType Int32Array + cuanyByRight.assign( x, new Int16Array( 4 ), 1, 0, isPositive, {} ); // $ExpectType Int16Array + cuanyByRight.assign( x, new Int8Array( 4 ), 1, 0, isPositive, {} ); // $ExpectType Int8Array + cuanyByRight.assign( x, new Uint32Array( 4 ), 1, 0, isPositive, {} ); // $ExpectType Uint32Array + cuanyByRight.assign( x, new Uint16Array( 4 ), 1, 0, isPositive, {} ); // $ExpectType Uint16Array + cuanyByRight.assign( x, new Uint8Array( 4 ), 1, 0, isPositive, {} ); // $ExpectType Uint8Array + cuanyByRight.assign( x, new Uint8ClampedArray( 4 ), 1, 0, isPositive, {} ); // $ExpectType Uint8ClampedArray +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an array-like object... +{ + const x = [ false, false, true, false, false ]; + + cuanyByRight.assign( 1, x, 2, 0, isPositive ); // $ExpectError + cuanyByRight.assign( true, x, 2, 0, isPositive ); // $ExpectError + cuanyByRight.assign( false, x, 2, 0, isPositive ); // $ExpectError + cuanyByRight.assign( null, x, 2, 0, isPositive ); // $ExpectError + cuanyByRight.assign( void 0, x, 2, 0, isPositive ); // $ExpectError + cuanyByRight.assign( {}, x, 2, 0, isPositive ); // $ExpectError + + cuanyByRight.assign( 1, x, 2, 0, isPositive, {} ); // $ExpectError + cuanyByRight.assign( true, x, 2, 0, isPositive, {} ); // $ExpectError + cuanyByRight.assign( false, x, 2, 0, isPositive, {} ); // $ExpectError + cuanyByRight.assign( null, x, 2, 0, isPositive, {} ); // $ExpectError + cuanyByRight.assign( void 0, x, 2, 0, isPositive, {} ); // $ExpectError + cuanyByRight.assign( {}, x, 2, 0, isPositive, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not an array-like object... +{ + const x = [ false, false, true, false, false ]; + + cuanyByRight.assign( x, 1, 2, 0, isPositive ); // $ExpectError + cuanyByRight.assign( x, true, 2, 0, isPositive ); // $ExpectError + cuanyByRight.assign( x, false, 2, 0, isPositive ); // $ExpectError + cuanyByRight.assign( x, null, 2, 0, isPositive ); // $ExpectError + cuanyByRight.assign( x, void 0, 2, 0, isPositive ); // $ExpectError + cuanyByRight.assign( x, {}, 2, 0, isPositive ); // $ExpectError + + cuanyByRight.assign( x, 1, 2, 0, isPositive, {} ); // $ExpectError + cuanyByRight.assign( x, true, 2, 0, isPositive, {} ); // $ExpectError + cuanyByRight.assign( x, false, 2, 0, isPositive, {} ); // $ExpectError + cuanyByRight.assign( x, null, 2, 0, isPositive, {} ); // $ExpectError + cuanyByRight.assign( x, void 0, 2, 0, isPositive, {} ); // $ExpectError + cuanyByRight.assign( x, {}, 2, 0, isPositive, {} ); // $ExpectError +} + + +// The compiler throws an error if the `assign` method is provided a third argument which is not a number... +{ + const x = [ false, false, true, false, false ]; + const y = [ false, null, false, null, false, null, false, null, false, null ]; + + cuanyByRight.assign( x, y , '1', 0, isPositive ); // $ExpectError + cuanyByRight.assign( x, y , true, 0, isPositive ); // $ExpectError + cuanyByRight.assign( x, y , false, 0, isPositive ); // $ExpectError + cuanyByRight.assign( x, y , null, 0, isPositive ); // $ExpectError + cuanyByRight.assign( x, y , void 0, isPositive ); // $ExpectError + cuanyByRight.assign( x, y , {}, 0, isPositive ); // $ExpectError + cuanyByRight.assign( x, y , [], 0, isPositive ); // $ExpectError + + cuanyByRight.assign( x, y , '1', 0, isPositive, {} ); // $ExpectError + cuanyByRight.assign( x, y , true, 0, isPositive, {} ); // $ExpectError + cuanyByRight.assign( x, y , false, 0, isPositive, {} ); // $ExpectError + cuanyByRight.assign( x, y , null, 0, isPositive, {} ); // $ExpectError + cuanyByRight.assign( x, y , void 0, isPositive, {} ); // $ExpectError + cuanyByRight.assign( x, y , {}, 0, isPositive, {} ); // $ExpectError + cuanyByRight.assign( x, y , [], 0, isPositive, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a fourth argument which is not a number... +{ + const x = [ false, false, true, false, false ]; + const y = [ false, null, false, null, false, null, false, null, false, null ]; + + cuanyByRight.assign( x, y, 1, '1', isPositive ); // $ExpectError + cuanyByRight.assign( x, y, 1, true, isPositive ); // $ExpectError + cuanyByRight.assign( x, y, 1, false, isPositive ); // $ExpectError + cuanyByRight.assign( x, y, 1, null, isPositive ); // $ExpectError + cuanyByRight.assign( x, y, 1, void 0, isPositive ); // $ExpectError + cuanyByRight.assign( x, y, 1, {}, isPositive ); // $ExpectError + cuanyByRight.assign( x, y, 1, [], isPositive ); // $ExpectError + + cuanyByRight.assign( x, y, 1, '1', isPositive, {} ); // $ExpectError + cuanyByRight.assign( x, y, 1, true, isPositive, {} ); // $ExpectError + cuanyByRight.assign( x, y, 1, false, isPositive, {} ); // $ExpectError + cuanyByRight.assign( x, y, 1, null, isPositive, {} ); // $ExpectError + cuanyByRight.assign( x, y, 1, void 0, isPositive, {} ); // $ExpectError + cuanyByRight.assign( x, y, 1, {}, isPositive, {} ); // $ExpectError + cuanyByRight.assign( x, y, 1, [], isPositive, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a fourth argument which is not like a function... +{ + const x = [ false, false, true, false, false ]; + const y = [ false, null, false, null, false, null, false, null, false, null ]; + + cuanyByRight.assign( x, y, 1, 2, {} ); // $ExpectError + cuanyByRight.assign( x, y, 1, 2, void 0 ); // $ExpectError + cuanyByRight.assign( x, y, 1, 1, [] ); // $ExpectError + cuanyByRight.assign( x, y, 1, 1, '' ); // $ExpectError + + cuanyByRight.assign( x, y, 1, 2, {}, {} ); // $ExpectError + cuanyByRight.assign( x, y, 1, 2, void 0, {} ); // $ExpectError + cuanyByRight.assign( x, y, 1, 1, [], {} ); // $ExpectError + cuanyByRight.assign( x, y, 1, 1, '', {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + cuanyByRight.assign(); // $ExpectError + cuanyByRight.assign( [] ); // $ExpectError + cuanyByRight.assign( [], [] ); // $ExpectError + cuanyByRight.assign( [], [], 2 ); // $ExpectError + cuanyByRight.assign( [], [], 1, 1, isPositive, [], {} ); // $ExpectError +} diff --git a/base/cuany-by-right/examples/index.js b/base/cuany-by-right/examples/index.js new file mode 100644 index 00000000..07c56c9c --- /dev/null +++ b/base/cuany-by-right/examples/index.js @@ -0,0 +1,34 @@ +/** +* @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'; + +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var cuanyByRight = require( './../lib' ); + +function isPositive( value ) { + return ( value > 0 ); +} + +// Create an array of random values: +var x = bernoulli( 10, 0.1 ); +console.log( x ); + +// Cumulatively test whether at least one array element passes a test, while iterating from right-to-left: +var out = cuanyByRight( x, isPositive ); +console.log( out ); diff --git a/base/cuany-by-right/lib/assign.js b/base/cuany-by-right/lib/assign.js new file mode 100644 index 00000000..21c271c4 --- /dev/null +++ b/base/cuany-by-right/lib/assign.js @@ -0,0 +1,171 @@ +/** +* @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 arraylike2object = require( './../../../base/arraylike2object' ); + + +// FUNCTIONS // + +/** +* Cumulatively tests whether at least one array element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left, and assigns the results to elements in the provided output array. +* +* @private +* @param {Collection} x - input array +* @param {Collection} y - output array +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @param {Function} predicate - test function +* @param {*} thisArg - execution context +* @returns {Collection} output array +* +* @example +* function isPositive( value ) { +* return ( value > 0 ); +* } +* +* var x = [ 0, 1, 0, 0, 0 ]; +* +* var out = [ false, false, false, false, false ]; +* var y = indexed( x, out, 1, 0, isPositive ); +* // returns [ false, false, false, true, true ] +*/ +function indexed( x, y, stride, offset, predicate, thisArg ) { + var flg; + var io; + var i; + + flg = false; + io = offset; + for ( i = x.length - 1; i >= 0; i-- ) { + if ( !flg && predicate.call( thisArg, x[ i ], i, x ) ) { + flg = true; + } + y[ io ] = flg; + io += stride; + } + return y; +} + +/** +* Cumulatively tests whether at least one array element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left, and assigns the results to elements in the accessor output array. +* +* @private +* @param {Object} x - input array object +* @param {Object} y - output array object +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @param {Function} predicate - test function +* @param {*} thisArg - execution context +* @returns {Collection} output array +* +* @example +* function isPositive( value ) { +* return ( value > 0 ); +* } +* +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = toAccessorArray( [ 0, 1, 0, 0, 0 ] ); +* +* var out = toAccessorArray( [ false, false, false, false, false ] ); +* var y = accessors( arraylike2object( x ), arraylike2object( out ), 1, 0, isPositive ); +* +* var v = out.get( 4 ); +* // returns true +*/ +function accessors( x, y, stride, offset, predicate, thisArg ) { + var xdata; + var ydata; + var xget; + var yset; + var flg; + var io; + var i; + + xdata = x.data; + ydata = y.data; + + xget = x.accessors[ 0 ]; + yset = y.accessors[ 1 ]; + + flg = false; + io = offset; + for ( i = xdata.length - 1; i >= 0; i-- ) { + if ( !flg && predicate.call( thisArg, xget( xdata, i ), i, x ) ) { + flg = true; + } + yset( ydata, io, flg ); + io += stride; + } + return ydata; +} + + +// MAIN // + +/** +* Cumulatively tests whether at least one array element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left, and assigns the results to elements in the output array. +* +* @param {Collection} x - input array +* @param {Collection} out - output array +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @param {Function} predicate - test function +* @param {*} [thisArg] - execution context +* @returns {Collection} output array +* +* @example +* function isPositive( value ) { +* return ( value > 0 ); +* } +* +* var x = [ 0, 1, 0, 0 ]; +* +* var y = [ false, null, false, null, false, null, false, null ]; +* var out = assign( x, y, 2, 0, isPositive ); +* // returns [ false, null, ..., true, null ] +* +* var bool = ( y === out ); +* // returns true +*/ +function assign( x, out, stride, offset, predicate, thisArg ) { + var xo; + var oo; + + xo = arraylike2object( x ); + oo = arraylike2object( out ); + if ( + xo.accessorProtocol || + oo.accessorProtocol + ) { + accessors( xo, oo, stride, offset, predicate, thisArg ); + return out; + } + indexed( x, out, stride, offset, predicate, thisArg ); + return out; +} + + +// EXPORTS // + +module.exports = assign; diff --git a/base/cuany-by-right/lib/index.js b/base/cuany-by-right/lib/index.js new file mode 100644 index 00000000..e7108cf8 --- /dev/null +++ b/base/cuany-by-right/lib/index.js @@ -0,0 +1,72 @@ +/** +* @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'; + +/** +* Cumulatively test whether at least one array element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left. +* +* @module @stdlib/array/base/cuany-by-right +* +* @example +* var cuanyByRight = require( '@stdlib/array/base/cuany-by-right' ); +* +* function isPositive( value ) { +* return ( value > 0 ); +* } +* +* var x = [ 0, 1, 0, 0, 0 ]; +* +* var y = cuanyByRight( x, isPositive ); +* // returns [ false, false, false, true, true ] +* +* @example +* var cuanyByRight = require( '@stdlib/array/base/cuany-by-right' ); +* +* function isPositive( value ) { +* return ( value > 0 ); +* } +* +* var x = [ 0, 1, 0, 0, 0 ]; +* +* var y1 = cuanyByRight( x, isPositive ); +* // returns [ false, false, false, true, true ] +* +* var y2 = [ false, null, false, null, false, null, false, null, false, null ]; +* var out = cuanyByRight.assign( x, y2, 2, 0, isPositive ); +* // returns [ false, null, false, null, false, null, true, null, true, null ] +* +* var bool = ( out === y2 ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); + + +// EXPORTS // + +module.exports = main; diff --git a/base/cuany-by-right/lib/main.js b/base/cuany-by-right/lib/main.js new file mode 100644 index 00000000..0b9303fc --- /dev/null +++ b/base/cuany-by-right/lib/main.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 filled = require( './../../../base/filled' ); +var assign = require( './assign.js' ); + + +// MAIN // + +/** +* Cumulatively tests whether at least one array element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left. +* +* @param {Collection} x - input array +* @param {Function} predicate - test function +* @param {*} [thisArg] - execution context +* @returns {Array} output array +* +* @example +* function isPositive( value ) { +* return ( value > 0 ); +* } +* +* var x = [ 0, 1, 0, 0, 0 ]; +* +* var y = cuanyByRight( x, isPositive ); +* // returns [ false, false, false, true, true ] +*/ +function cuanyByRight( x, predicate, thisArg ) { + var out = filled( false, x.length ); + return assign( x, out, 1, 0, predicate, thisArg ); +} + + +// EXPORTS // + +module.exports = cuanyByRight; diff --git a/base/cuany-by-right/package.json b/base/cuany-by-right/package.json new file mode 100644 index 00000000..b9ae4cf3 --- /dev/null +++ b/base/cuany-by-right/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/array/base/cuany-by-right", + "version": "0.0.0", + "description": "Cumulatively test whether at least one array element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "utils", + "generic", + "array", + "cumulative", + "data", + "structure", + "test", + "predicate", + "any", + "some", + "array.some", + "array-like", + "validate" + ] +} diff --git a/base/cuany-by-right/test/test.assign.js b/base/cuany-by-right/test/test.assign.js new file mode 100644 index 00000000..dc7b223e --- /dev/null +++ b/base/cuany-by-right/test/test.assign.js @@ -0,0 +1,247 @@ +/** +* @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 Float64Array = require( './../../../float64' ); +var toAccessorArray = require( './../../../base/to-accessor-array' ); +var cuanyByRight = require( './../lib/assign.js' ); + + +// FUNCTIONS // + +function isPositive( v ) { + return v > 0; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof cuanyByRight, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function cumulatively tests whether at least one array element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left, and assigns the results to a provided output array (generic)', function test( t ) { + var expected; + var actual; + var x; + var y; + + x = [ 0, 0, 1, 1, 0 ]; + y = [ false, true, false, true, false ]; + + actual = cuanyByRight( x, y, 1, 0, isPositive ); + expected = [ false, true, true, true, true ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = [ 1, 1, 0, 0 ]; + y = [ false, null, false, null, false, null, false, null ]; + + actual = cuanyByRight( x, y, 2, 0, isPositive ); + expected = [ false, null, false, null, true, null, true, null ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = [ 1, 1, 0, 0, 0 ]; + y = [ true, false, false, true, true, true ]; + + actual = cuanyByRight( x, y, 1, 1, isPositive ); + expected = [ true, false, false, false, true, true ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = []; + y = [ false, false, false, false, false ]; + + actual = cuanyByRight( x, y, 1, 0, isPositive ); + expected = [ false, false, false, false, false ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = [ 0 ]; + y = [ false, false ]; + + actual = cuanyByRight( x, y, 1, 1, isPositive ); + expected = [ false, false ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function cumulatively tests whether at least one array element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left, and assigns the results to a provided output array (typed)', function test( t ) { + var expected; + var actual; + var x; + var y; + + x = new Float64Array( [ 1.0, 0.0, 1.0, 0.0, 0.0 ] ); + y = [ false, true, false, true, false ]; + + actual = cuanyByRight( x, y, 1, 0, isPositive ); + expected = [ false, false, true, true, true ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = new Float64Array( [ 1.0, 1.0, 0.0, 0.0 ] ); + y = [ false, null, false, null, false, null, false, null ]; + + actual = cuanyByRight( x, y, 2, 0, isPositive ); + expected = [ false, null, false, null, true, null, true, null ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = new Float64Array( [ 0.0, 1.0, 1.0, 0.0, 0.0 ] ); + y = [ true, false, false, true, true, true ]; + + actual = cuanyByRight( x, y, 1, 1, isPositive ); + expected = [ true, false, false, true, true, true ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = new Float64Array( [] ); + y = [ false, false, false, false, false ]; + + actual = cuanyByRight( x, y, 1, 0, isPositive ); + expected = [ false, false, false, false, false ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = new Float64Array( [ 0.0 ] ); + y = [ false, false ]; + + actual = cuanyByRight( x, y, 1, 1, isPositive ); + expected = [ false, false ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function cumulatively tests whether at least one array element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left, and assigns the results to a provided output array (accessor)', function test( t ) { + var expected; + var actual; + var ybuf; + var x; + var y; + + x = toAccessorArray( [ 1, 0, 0, 0, 0 ] ); + ybuf = [ false, true, false, true, false ]; + y = toAccessorArray( ybuf ); + + actual = cuanyByRight( x, y, 1, 0, isPositive ); + expected = [ false, false, false, false, true ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ybuf, expected, 'returns expected value' ); + + x = toAccessorArray( [ 0, 1, 1, 0 ] ); + ybuf = [ false, null, false, null, false, null, false, null ]; + y = toAccessorArray( ybuf ); + + actual = cuanyByRight( x, y, 2, 0, isPositive ); + expected = [ false, null, true, null, true, null, true, null ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ybuf, expected, 'returns expected value' ); + + x = toAccessorArray( [ 0, 1, 1, 0, 0 ] ); + ybuf = [ true, false, false, false, false, false ]; + y = toAccessorArray( ybuf ); + + actual = cuanyByRight( x, y, 1, 1, isPositive ); + expected = [ true, false, false, true, true, true ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ybuf, expected, 'returns expected value' ); + + x = toAccessorArray( [ 0, 0, 0, 0, 1 ] ); + ybuf = [ true, true, true, true, true ]; + y = toAccessorArray( ybuf ); + + actual = cuanyByRight( x, y, 1, 0, isPositive ); + expected = [ true, true, true, true, true ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ybuf, expected, 'returns expected value' ); + + x = toAccessorArray( [] ); + ybuf = [ false, false, false, false, false ]; + y = toAccessorArray( ybuf ); + + actual = cuanyByRight( x, y, 1, 0, isPositive ); + expected = [ false, false, false, false, false ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ybuf, expected, 'returns expected value' ); + + x = toAccessorArray( [ 0 ] ); + ybuf = [ false, false ]; + y = toAccessorArray( ybuf ); + + actual = cuanyByRight( x, y, 1, 1, isPositive ); + expected = [ false, false ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ybuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing an execution context', function test( t ) { + var expected; + var actual; + var ctx; + var x; + var y; + + ctx = { + 'count': 0 + }; + + x = [ 0, 1, 0, 0, 0 ]; + y = [ false, null, false, null, false, null, false, null, false, null ]; + + actual = cuanyByRight( x, y, 2, 0, predicate, ctx ); + expected = [ false, null, false, null, false, null, true, null, true, null ]; + + t.deepEqual( actual, expected, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( v > 0 ); + } +}); diff --git a/base/cuany-by-right/test/test.js b/base/cuany-by-right/test/test.js new file mode 100644 index 00000000..6a65e1ab --- /dev/null +++ b/base/cuany-by-right/test/test.js @@ -0,0 +1,41 @@ +/** +* @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 hasMethod = require( '@stdlib/assert/is-method' ); +var cuanyByRight = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof cuanyByRight, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( hasOwnProp( cuanyByRight, 'assign' ), true, 'returns expected value' ); + t.strictEqual( hasMethod( cuanyByRight, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/base/cuany-by-right/test/test.main.js b/base/cuany-by-right/test/test.main.js new file mode 100644 index 00000000..968ef0aa --- /dev/null +++ b/base/cuany-by-right/test/test.main.js @@ -0,0 +1,161 @@ +/** +* @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 Float64Array = require( './../../../float64' ); +var toAccessorArray = require( './../../../base/to-accessor-array' ); +var cuanyByRight = require( './../lib' ); + + +// FUNCTIONS // + +function isPositive( v ) { + return v > 0; +} + +function isNotNull( v ) { + return v !== null; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof cuanyByRight, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function cumulatively tests whether at least one array element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left (generic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ 1, 1, 0, 0, 0 ]; + + actual = cuanyByRight( x, isPositive ); + expected = [ false, false, false, true, true ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + x = [ 0.0, 0.0, 0.0, 1.0, 1.0 ]; + actual = cuanyByRight( x, isPositive ); + expected = [ true, true, true, true, true ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + x = [ {}, {}, null ]; + actual = cuanyByRight( x, isNotNull ); + expected = [ false, true, true ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function cumulatively tests whether at least one array element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left (typed)', function test( t ) { + var expected; + var actual; + var x; + + x = new Float64Array( [ 0.0, 0.0, 1.0, 1.0, 0.0 ] ); + actual = cuanyByRight( x, isPositive ); + expected = [ false, true, true, true, true ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + x = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ); + actual = cuanyByRight( x, isPositive ); + expected = [ false, false, false, false, false ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + x = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] ); + actual = cuanyByRight( x, isPositive ); + expected = [ true, true, true, true, true ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + x = new Float64Array( [ 0.0, 1.0, 0.0 ] ); + actual = cuanyByRight( x, isPositive ); + expected = [ false, true, true ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + x = new Float64Array( [ 0.0, 1.0, 0.0, 0.0, 0.0 ] ); + actual = cuanyByRight( x, isPositive ); + expected = [ false, false, false, true, true ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function cumulatively tests whether at least one array element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left (accessor)', function test( t ) { + var expected; + var actual; + var x; + + x = toAccessorArray( [ 1, 1, 0, 0, 0 ] ); + + actual = cuanyByRight( x, isPositive ); + expected = [ false, false, false, true, true ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + x = toAccessorArray( [ 0, 0, 0, 0, 0 ] ); + actual = cuanyByRight( x, isPositive ); + expected = [ false, false, false, false, false ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + x = toAccessorArray( [ 1, 1, 1, 1, 1 ] ); + actual = cuanyByRight( x, isPositive ); + expected = [ true, true, true, true, true ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + x = toAccessorArray( [ 0, 0, 1 ] ); + actual = cuanyByRight( x, isPositive ); + expected = [ true, true, true ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + x = toAccessorArray( [ 1, 0, 0, 0, 0 ] ); + actual = cuanyByRight( x, isPositive ); + expected = [ false, false, false, false, true ]; + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing an execution context', function test( t ) { + var expected; + var out; + var ctx; + var x; + + ctx = { + 'count': 0 + }; + + x = [ 1, -2, 0, -1 ]; + + out = cuanyByRight( x, predicate, ctx ); + expected = [ false, false, false, true ]; + + t.deepEqual( out, expected, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( v > 0 ); + } +});