From fc1e303a1de334e4b35913304b47f8f17b95d363 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Fri, 30 May 2025 14:51:54 +0000 Subject: [PATCH 01/18] feat: add `stats/array/max-by` --- .../@stdlib/stats/array/max-by/README.md | 155 ++++++++++ .../stats/array/max-by/benchmark/benchmark.js | 107 +++++++ .../@stdlib/stats/array/max-by/docs/repl.txt | 44 +++ .../stats/array/max-by/docs/types/index.d.ts | 109 +++++++ .../stats/array/max-by/docs/types/test.ts | 72 +++++ .../stats/array/max-by/examples/index.js | 34 +++ .../@stdlib/stats/array/max-by/lib/index.js | 46 +++ .../@stdlib/stats/array/max-by/lib/main.js | 82 ++++++ .../@stdlib/stats/array/max-by/package.json | 67 +++++ .../@stdlib/stats/array/max-by/test/test.js | 274 ++++++++++++++++++ 10 files changed, 990 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/array/max-by/README.md create mode 100644 lib/node_modules/@stdlib/stats/array/max-by/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/array/max-by/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/array/max-by/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/array/max-by/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/array/max-by/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/array/max-by/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/array/max-by/package.json create mode 100644 lib/node_modules/@stdlib/stats/array/max-by/test/test.js diff --git a/lib/node_modules/@stdlib/stats/array/max-by/README.md b/lib/node_modules/@stdlib/stats/array/max-by/README.md new file mode 100644 index 000000000000..af249b08dbc1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/max-by/README.md @@ -0,0 +1,155 @@ + + +# maxBy + +> Calculate the maximum value of an array via a callback function. + +
+ +
+ + + +
+ +## Usage + +```javascript +var maxBy = require( '@stdlib/stats/array/max-by' ); +``` + +#### maxBy( x, clbk\[, thisArg] ) + +Computes the maximum value of an array via a callback function. + +```javascript +function accessor( v ) { + return v * 2.0; +} + +var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; + +var v = maxBy( x, accessor ); +// returns 8.0 +``` + +The function has the following parameters: + +- **x**: input array. +- **clbk**: callback function. +- **thisArg**: execution context (_optional_). + +The invoked callback is provided four arguments: + +- **value**: array element. +- **aidx**: array index. +- **sidx**: strided index (`offset + aidx*stride`). +- **array**: input array/collection. + +To set the callback execution context, provide a `thisArg`. + +```javascript +function accessor( v ) { + this.count += 1; + return v * 2.0; +} + +var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; + +var context = { + 'count': 0 +}; + +var v = maxBy( x, accessor, context ); +// returns 8.0 + +var cnt = context.count; +// returns 8 +``` + +
+ + + +
+ +## Notes + +- If provided an empty array, the function returns `NaN`. +- A provided callback function should return a numeric value. +- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**. +- The function supports array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). +- When possible, prefer using [`dmax`][@stdlib/stats/array/dmax], [`smax`][@stdlib/stats/array/smax], and/or [`max`][@stdlib/stats/array/max], as, depending on the environment, these interfaces are likely to be significantly more performant. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var maxBy = require( '@stdlib/stats/array/max-by' ); + +function accessor( v ) { + return v * 2.0; +} + +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +console.log( x ); + +var v = maxBy( x, accessor ); +console.log( v ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/array/max-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/array/max-by/benchmark/benchmark.js new file mode 100644 index 000000000000..0df2dff8716b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/max-by/benchmark/benchmark.js @@ -0,0 +1,107 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 uniform = require( '@stdlib/random/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var maxBy = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Accessor function. +* +* @private +* @param {number} value - array element +* @returns {number} accessed value +*/ +function accessor( value ) { + return value * 2.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -10, 10, options ); + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = maxBy( x, accessor ); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt b/lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt new file mode 100644 index 000000000000..7feeebde5b8b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt @@ -0,0 +1,44 @@ + +{{alias}}( x ) + Computes the maximum value of an array via a callback function. + + If provided an empty array, the function returns `NaN`. + + The callback function is provided three arguments: + + - value: array element. + - aidx: array index. + - sidx: strided index (offset + aidx*stride). + - array: the input array. + + The callback function should return a numeric value. + + If the callback function does not return any value (or equivalently, + explicitly returns `undefined`), the value is ignored. + + Parameters + ---------- + x: Array|TypedArray + Input array. + + clbk: Function + Callback function. + + thisArg: any (optional) + Execution context. + + Returns + ------- + out: number + Maximum value. + + Examples + -------- + > function accessor( v ) { return v * 2.0; }; + > var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, -1.0, -3.0 ]; + > {{alias}}( x, accessor ) + 8.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/array/max-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/array/max-by/docs/types/index.d.ts new file mode 100644 index 000000000000..3404fae3af63 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/max-by/docs/types/index.d.ts @@ -0,0 +1,109 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; + +/** +* Returns an accessed value. +* +* @returns accessed value +*/ +type Nullary = ( this: U ) => number | void; + +/** +* Returns an accessed value. +* +* @param value - array element +* @returns accessed value +*/ +type Unary = ( this: U, value: T ) => number | void; + +/** +* Returns an accessed value. +* +* @param value - array element +* @param aidx - array index +* @returns accessed value +*/ +type Binary = ( this: U, value: T, aidx: number ) => number | void; + +/** +* Returns an accessed value. +* +* @param value - array element +* @param aidx - array index +* @param sidx - strided index (offset + aidx*stride) +* @returns accessed value +*/ +type Ternary = ( this: U, value: T, aidx: number, sidx: number ) => number | void; + +/** +* Returns an accessed value. +* +* @param value - array element +* @param aidx - array index +* @param sidx - strided index (offset + aidx*stride) +* @param array - input array +* @returns accessed value +*/ +type Quaternary = ( this: U, value: T, aidx: number, sidx: number, array: Collection ) => number | void; + +/** +* Returns an accessed value. +* +* @param value - array element +* @param aidx - array index +* @param sidx - strided index (offset + aidx*stride) +* @param array - input array +* @returns accessed value +*/ +type Callback = Nullary | Unary | Binary | Ternary | Quaternary; + +/** +* Computes the maximum value of an array via a callback function. +* +* @param x - input array +* @param clbk - callback +* @param thisArg - execution context +* @returns maximum value +* +* @example +* var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; +* +* function accessor( v ) { +* return v * 2.0; +* } +* +* var v = maxBy( x, accessor ); +* // returns 8.0 +*/ +declare function maxBy( x: InputArray, clbk: Callback, thisArg?: ThisParameterType> ): number; + + +// EXPORTS // + +export = maxBy; diff --git a/lib/node_modules/@stdlib/stats/array/max-by/docs/types/test.ts b/lib/node_modules/@stdlib/stats/array/max-by/docs/types/test.ts new file mode 100644 index 000000000000..47eb7140907f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/max-by/docs/types/test.ts @@ -0,0 +1,72 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 AccessorArray = require( '@stdlib/array/base/accessor' ); +import maxBy = require( './index' ); + +const accessor = (): number => { + return 5.0; +}; + + +// TESTS // + +// The function returns a number... +{ + const x = new Float64Array( 10 ); + + maxBy( x, accessor ); // $ExpectType number + maxBy( new AccessorArray( x ), accessor ); // $ExpectType number + + maxBy( x, accessor, {} ); // $ExpectType number + maxBy( new AccessorArray( x ), accessor, {} ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not a numeric array... +{ + maxBy( 10, accessor ); // $ExpectError + maxBy( '10', accessor ); // $ExpectError + maxBy( true, accessor ); // $ExpectError + maxBy( false, accessor ); // $ExpectError + maxBy( null, accessor ); // $ExpectError + maxBy( undefined, accessor ); // $ExpectError + maxBy( {}, accessor ); // $ExpectError + maxBy( ( x: number ): number => x, accessor ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a function... +{ + const x = new Float64Array( 10 ); + + maxBy( x, '10' ); // $ExpectError + maxBy( x, true ); // $ExpectError + maxBy( x, false ); // $ExpectError + maxBy( x, null ); // $ExpectError + maxBy( x, undefined ); // $ExpectError + maxBy( x, [] ); // $ExpectError + maxBy( x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + + maxBy(); // $ExpectError + maxBy( x ); // $ExpectError + maxBy( x, accessor, {}, 10 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/array/max-by/examples/index.js b/lib/node_modules/@stdlib/stats/array/max-by/examples/index.js new file mode 100644 index 000000000000..008699ccd996 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/max-by/examples/index.js @@ -0,0 +1,34 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var maxBy = require( './../lib' ); + +function accessor( v ) { + return v * 2.0; +} + +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +console.log( x ); + +var v = maxBy( x, accessor ); +console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/array/max-by/lib/index.js b/lib/node_modules/@stdlib/stats/array/max-by/lib/index.js new file mode 100644 index 000000000000..e7e5644db6c9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/max-by/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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'; + +/** +* Compute the maximum value of an array via a callback function. +* +* @module @stdlib/stats/array/max-by +* +* @example +* var maxBy = require( '@stdlib/stats/array/max-by' ); +* +* function accessor( v ) { +* return v * 2.0; +* } +* +* var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; +* +* var v = maxBy( x, accessor ); +* // returns 8.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js b/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js new file mode 100644 index 000000000000..c60be338acc9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 isCollection = require( '@stdlib/assert/is-collection' ); +var dtypes = require( '@stdlib/array/dtypes' ); +var dtype = require( '@stdlib/array/dtype' ); +var contains = require( '@stdlib/array/base/assert/contains' ); +var join = require( '@stdlib/array/base/join' ); +var strided = require( '@stdlib/stats/base/max-by' ).ndarray; +var format = require( '@stdlib/string/format' ); +var isFunction = require( '@stdlib/assert/is-function' ); + + +// VARIABLES // + +var IDTYPES = dtypes( 'real_and_generic' ); +var GENERIC_DTYPE = 'generic'; + + +// MAIN // + +/** +* Computes the maximum value of an array via a callback function. +* +* @param {NumericArray} x - input array +* @param {Callback} clbk - callback +* @param {*} [thisArg] - execution context +* @throws {TypeError} first argument must be an array-like object +* @throws {TypeError} first argument must have a supported data type +* @returns {number} maximum value +* +* @example +* var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; +* +* function accessor( v ) { +* return v * 2.0; +* } +* +* var v = maxBy( x, accessor ); +* // returns 8.0 +*/ +function maxBy( x, clbk, thisArg ) { + var dt; + if ( !isCollection( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an array-like object. Value: `%s`.', x ) ); + } + dt = dtype( x ) || GENERIC_DTYPE; + if ( !contains( IDTYPES, dt ) ) { + throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', join( IDTYPES, '", "' ), dt ) ); + } + if ( !isFunction( clbk ) ) { + throw new TypeError( format( 'invalid argument. Callback function must be a function. Value: `%s`.', clbk ) ); + } + if (arguments.length > 2) { + return strided( x.length, x, 1, 0, clbk, thisArg ); + } + return strided( x.length, x, 1, 0, clbk ); +} + + +// EXPORTS // + +module.exports = maxBy; diff --git a/lib/node_modules/@stdlib/stats/array/max-by/package.json b/lib/node_modules/@stdlib/stats/array/max-by/package.json new file mode 100644 index 000000000000..bfd8e097b985 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/max-by/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/stats/array/max-by", + "version": "0.0.0", + "description": "Calculate the maximum value of an array via a callback function.", + "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", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "maximum", + "max", + "range", + "extremes", + "domain", + "extent", + "array" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/array/max-by/test/test.js b/lib/node_modules/@stdlib/stats/array/max-by/test/test.js new file mode 100644 index 000000000000..e8f81252fcfe --- /dev/null +++ b/lib/node_modules/@stdlib/stats/array/max-by/test/test.js @@ -0,0 +1,274 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var BooleanArray = require( '@stdlib/array/bool' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var maxBy = require( './../lib/main.js' ); + + +// FUNCTIONS // + +function accessor( v ) { + if ( v === void 0 ) { + return; + } + return v * 2.0; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof maxBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 3', function test( t ) { + t.strictEqual( maxBy.length, 3, 'returns expected value' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an array-like object', function test( t ) { + var values; + var i; + + 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() { + maxBy( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which has an unsupported data type', function test( t ) { + var values; + var i; + + values = [ + new BooleanArray( 4 ), + new Complex128Array( 4 ) + ]; + 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() { + maxBy( value ); + }; + } +}); + +tape( 'the function throws an error if provided a callback function argument which is not a function', function test( t ) { + var values; + var i; + var x; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + values = [ + '5', + 5, + NaN, + null, + void 0, + true, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + maxBy( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a callback function argument which is not a function (options)', function test( t ) { + var values; + var i; + var x; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + values = [ + '5', + 5, + NaN, + null, + void 0, + true, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + maxBy( x, value, {} ); + }; + } +}); + +tape( 'the function calculates the maximum value of an array via a callback function', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = maxBy( x, accessor ); + t.strictEqual( v, 10.0, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = maxBy( x, accessor ); + t.strictEqual( v, -8.0, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = maxBy( x, accessor ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = maxBy( x, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = maxBy( x, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the maximum value of an array via a callback function (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = maxBy( toAccessorArray( x ), accessor ); + t.strictEqual( v, 10.0, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = maxBy( toAccessorArray( x ), accessor ); + t.strictEqual( v, -8.0, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = maxBy( toAccessorArray( x ), accessor ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = maxBy( toAccessorArray( x ), accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = maxBy( toAccessorArray( x ), accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the maximum value of an array (array-like object)', function test( t ) { + var x; + var v; + + x = { + 'length': 6, + '0': 1.0, + '1': -2.0, + '2': -4.0, + '3': 5.0, + '4': 0.0, + '5': 3.0 + }; + v = maxBy( x, accessor ); + t.strictEqual( v, 10.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a callback execution context', function test( t ) { + var ctx; + var x; + + x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + ctx = { + 'count': 0 + }; + maxBy( x, accessor, ctx ); + + t.strictEqual( ctx.count, x.length, 'returns expected value' ); + t.end(); + + function accessor( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v * 2.0; + } +}); + +tape( 'if provided an empty array, the function returns `NaN`', function test( t ) { + var v = maxBy( [], accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an empty array, the function returns `NaN` (accessors)', function test( t ) { + var v = maxBy( toAccessorArray( [] ), accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an array containing a single element, the function returns the first element applying callback function', function test( t ) { + var v = maxBy( [ 1.0 ], accessor ); + t.strictEqual( v, 2.0, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an array containing a single element, the function returns the first element applying callback function (accessors)', function test( t ) { + var v = maxBy( toAccessorArray( [ 1.0 ] ), accessor ); + t.strictEqual( v, 2.0, 'returns expected value' ); + t.end(); +}); From 1a280b1f1bc9115015d3de5c5395cd9455e658e3 Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 31 May 2025 15:53:21 -0700 Subject: [PATCH 02/18] docs: fix signature Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt b/lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt index 7feeebde5b8b..5b15b6301633 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt @@ -1,5 +1,5 @@ -{{alias}}( x ) +{{alias}}( x, clbk[, thisArg] ) Computes the maximum value of an array via a callback function. If provided an empty array, the function returns `NaN`. From a0a285d1afc598f4ffe026e5cb52e4fa157d4346 Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 31 May 2025 15:59:58 -0700 Subject: [PATCH 03/18] fix: update callback invocation logic to ensure correct arguments Signed-off-by: Athan --- .../@stdlib/stats/array/max-by/lib/main.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js b/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js index c60be338acc9..41c900e1eceb 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js +++ b/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js @@ -70,10 +70,21 @@ function maxBy( x, clbk, thisArg ) { if ( !isFunction( clbk ) ) { throw new TypeError( format( 'invalid argument. Callback function must be a function. Value: `%s`.', clbk ) ); } - if (arguments.length > 2) { - return strided( x.length, x, 1, 0, clbk, thisArg ); + return strided( x.length, x, 1, 0, wrapper ); + + /** + * Invokes a provided callback. + * + * @private + * @param {number} value - current element + * @param {NonNegativeInteger} aidx - current array index + * @param {NonNegativeInteger} sidx - current strided index + * @param {NumericArray} arr - input array + * @returns {number} callback return value + */ + function wrapper( value, aidx, sidx, arr ) { + return clbk.call( thisArg, value, aidx, arr ); } - return strided( x.length, x, 1, 0, clbk ); } From f1ffd8b94342d9677c32286b810cc7b25d05847e Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 31 May 2025 16:01:27 -0700 Subject: [PATCH 04/18] style: fix indentation Signed-off-by: Athan --- .../@stdlib/stats/array/max-by/lib/main.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js b/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js index 41c900e1eceb..ae135ea54e06 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js +++ b/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js @@ -73,15 +73,15 @@ function maxBy( x, clbk, thisArg ) { return strided( x.length, x, 1, 0, wrapper ); /** - * Invokes a provided callback. - * - * @private - * @param {number} value - current element - * @param {NonNegativeInteger} aidx - current array index + * Invokes a provided callback. + * + * @private + * @param {number} value - current element + * @param {NonNegativeInteger} aidx - current array index * @param {NonNegativeInteger} sidx - current strided index - * @param {NumericArray} arr - input array - * @returns {number} callback return value - */ + * @param {NumericArray} arr - input array + * @returns {number} callback return value + */ function wrapper( value, aidx, sidx, arr ) { return clbk.call( thisArg, value, aidx, arr ); } From c052abb3631f2b3da54f805439e076f67f791c4e Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 31 May 2025 16:02:02 -0700 Subject: [PATCH 05/18] style: fix indentation Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/array/max-by/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js b/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js index ae135ea54e06..e71b332d0ec8 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js +++ b/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js @@ -80,7 +80,7 @@ function maxBy( x, clbk, thisArg ) { * @param {NonNegativeInteger} aidx - current array index * @param {NonNegativeInteger} sidx - current strided index * @param {NumericArray} arr - input array - * @returns {number} callback return value + * @returns {number} callback return value */ function wrapper( value, aidx, sidx, arr ) { return clbk.call( thisArg, value, aidx, arr ); From a1854c0df1fa53d8413770130305e09222096b45 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Sun, 1 Jun 2025 06:33:01 +0000 Subject: [PATCH 06/18] docs: update docs as suggested in docs --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/stats/array/max-by/README.md | 8 -------- .../@stdlib/stats/array/max-by/docs/repl.txt | 1 - .../stats/array/max-by/docs/types/index.d.ts | 16 ++-------------- 3 files changed, 2 insertions(+), 23 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/array/max-by/README.md b/lib/node_modules/@stdlib/stats/array/max-by/README.md index af249b08dbc1..6aa7c605ecfe 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/README.md +++ b/lib/node_modules/@stdlib/stats/array/max-by/README.md @@ -61,7 +61,6 @@ The invoked callback is provided four arguments: - **value**: array element. - **aidx**: array index. -- **sidx**: strided index (`offset + aidx*stride`). - **array**: input array/collection. To set the callback execution context, provide a `thisArg`. @@ -97,7 +96,6 @@ var cnt = context.count; - A provided callback function should return a numeric value. - If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**. - The function supports array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). -- When possible, prefer using [`dmax`][@stdlib/stats/array/dmax], [`smax`][@stdlib/stats/array/smax], and/or [`max`][@stdlib/stats/array/max], as, depending on the environment, these interfaces are likely to be significantly more performant. @@ -144,12 +142,6 @@ console.log( v ); [@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor -[@stdlib/stats/array/dmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/array/dmax - -[@stdlib/stats/array/max]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/array/max - -[@stdlib/stats/array/smax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/array/smax - diff --git a/lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt b/lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt index 5b15b6301633..452099967b3e 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt @@ -8,7 +8,6 @@ - value: array element. - aidx: array index. - - sidx: strided index (offset + aidx*stride). - array: the input array. The callback function should return a numeric value. diff --git a/lib/node_modules/@stdlib/stats/array/max-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/array/max-by/docs/types/index.d.ts index 3404fae3af63..8d3a283e410a 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/array/max-by/docs/types/index.d.ts @@ -56,32 +56,20 @@ type Binary = ( this: U, value: T, aidx: number ) => number | void; * * @param value - array element * @param aidx - array index -* @param sidx - strided index (offset + aidx*stride) -* @returns accessed value -*/ -type Ternary = ( this: U, value: T, aidx: number, sidx: number ) => number | void; - -/** -* Returns an accessed value. -* -* @param value - array element -* @param aidx - array index -* @param sidx - strided index (offset + aidx*stride) * @param array - input array * @returns accessed value */ -type Quaternary = ( this: U, value: T, aidx: number, sidx: number, array: Collection ) => number | void; +type Ternary = ( this: U, value: T, aidx: number, array: Collection ) => number | void; /** * Returns an accessed value. * * @param value - array element * @param aidx - array index -* @param sidx - strided index (offset + aidx*stride) * @param array - input array * @returns accessed value */ -type Callback = Nullary | Unary | Binary | Ternary | Quaternary; +type Callback = Nullary | Unary | Binary | Ternary; /** * Computes the maximum value of an array via a callback function. From 10751e71aaf5bec4f9e8e1884806cfe21cdb9c55 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 1 Jun 2025 01:18:31 -0700 Subject: [PATCH 07/18] docs: update copy Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/array/max-by/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/array/max-by/README.md b/lib/node_modules/@stdlib/stats/array/max-by/README.md index 6aa7c605ecfe..622af3a319af 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/README.md +++ b/lib/node_modules/@stdlib/stats/array/max-by/README.md @@ -57,10 +57,10 @@ The function has the following parameters: - **clbk**: callback function. - **thisArg**: execution context (_optional_). -The invoked callback is provided four arguments: +The invoked callback is provided three arguments: -- **value**: array element. -- **aidx**: array index. +- **value**: current array element. +- **index**: current array index. - **array**: input array/collection. To set the callback execution context, provide a `thisArg`. From 5921c134d39bccdbd912fc3b4d08732b0a95d4d1 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 1 Jun 2025 01:18:56 -0700 Subject: [PATCH 08/18] docs: update copy Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/array/max-by/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/array/max-by/README.md b/lib/node_modules/@stdlib/stats/array/max-by/README.md index 622af3a319af..5969471869b7 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/README.md +++ b/lib/node_modules/@stdlib/stats/array/max-by/README.md @@ -61,7 +61,7 @@ The invoked callback is provided three arguments: - **value**: current array element. - **index**: current array index. -- **array**: input array/collection. +- **array**: input array. To set the callback execution context, provide a `thisArg`. From f749d9c0f42b41ca41c0a304f36aeffcdfde6403 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 1 Jun 2025 01:20:05 -0700 Subject: [PATCH 09/18] docs: update copy Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt b/lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt index 452099967b3e..2de80ac61249 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/array/max-by/docs/repl.txt @@ -6,8 +6,8 @@ The callback function is provided three arguments: - - value: array element. - - aidx: array index. + - value: current array element. + - index: current array index. - array: the input array. The callback function should return a numeric value. From 2eea8cf2b13233fda5c1636c2cf358c76630f153 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 1 Jun 2025 01:21:37 -0700 Subject: [PATCH 10/18] docs: update parameters and descriptions Signed-off-by: Athan --- .../stats/array/max-by/docs/types/index.d.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/array/max-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/array/max-by/docs/types/index.d.ts index 8d3a283e410a..e76473f1e4c1 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/array/max-by/docs/types/index.d.ts @@ -37,7 +37,7 @@ type Nullary = ( this: U ) => number | void; /** * Returns an accessed value. * -* @param value - array element +* @param value - current array element * @returns accessed value */ type Unary = ( this: U, value: T ) => number | void; @@ -45,27 +45,27 @@ type Unary = ( this: U, value: T ) => number | void; /** * Returns an accessed value. * -* @param value - array element -* @param aidx - array index +* @param value - current array element +* @param index - current array index * @returns accessed value */ -type Binary = ( this: U, value: T, aidx: number ) => number | void; +type Binary = ( this: U, value: T, index: number ) => number | void; /** * Returns an accessed value. * -* @param value - array element -* @param aidx - array index +* @param value - current array element +* @param index - current array index * @param array - input array * @returns accessed value */ -type Ternary = ( this: U, value: T, aidx: number, array: Collection ) => number | void; +type Ternary = ( this: U, value: T, index: number, array: Collection ) => number | void; /** * Returns an accessed value. * -* @param value - array element -* @param aidx - array index +* @param value - current array element +* @param index - current array index * @param array - input array * @returns accessed value */ From e02ac157df3ff564d4d42104c945484b8d904be2 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 1 Jun 2025 01:25:43 -0700 Subject: [PATCH 11/18] refactor: improve type specificity Signed-off-by: Athan --- .../@stdlib/stats/array/max-by/docs/types/index.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/array/max-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/array/max-by/docs/types/index.d.ts index e76473f1e4c1..c8ab935983d9 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/array/max-by/docs/types/index.d.ts @@ -32,7 +32,7 @@ type InputArray = NumericArray | Collection | AccessorArrayLike; * * @returns accessed value */ -type Nullary = ( this: U ) => number | void; +type Nullary = ( this: ThisArg ) => number | void; /** * Returns an accessed value. @@ -40,7 +40,7 @@ type Nullary = ( this: U ) => number | void; * @param value - current array element * @returns accessed value */ -type Unary = ( this: U, value: T ) => number | void; +type Unary = ( this: ThisArg, value: T ) => number | void; /** * Returns an accessed value. @@ -49,7 +49,7 @@ type Unary = ( this: U, value: T ) => number | void; * @param index - current array index * @returns accessed value */ -type Binary = ( this: U, value: T, index: number ) => number | void; +type Binary = ( this: ThisArg, value: T, index: number ) => number | void; /** * Returns an accessed value. @@ -59,7 +59,7 @@ type Binary = ( this: U, value: T, index: number ) => number | void; * @param array - input array * @returns accessed value */ -type Ternary = ( this: U, value: T, index: number, array: Collection ) => number | void; +type Ternary = ( this: ThisArg, value: T, index: number, array: U ) => number | void; /** * Returns an accessed value. @@ -69,7 +69,7 @@ type Ternary = ( this: U, value: T, index: number, array: Collection ) * @param array - input array * @returns accessed value */ -type Callback = Nullary | Unary | Binary | Ternary; +type Callback = Nullary | Unary | Binary | Ternary; /** * Computes the maximum value of an array via a callback function. @@ -89,7 +89,7 @@ type Callback = Nullary | Unary | Binary | Ternary; * var v = maxBy( x, accessor ); * // returns 8.0 */ -declare function maxBy( x: InputArray, clbk: Callback, thisArg?: ThisParameterType> ): number; +declare function maxBy( x: U, clbk: Callback, thisArg?: ThisParameterType> ): number; // EXPORTS // From dc5316ab78273637cfa8cb2020c2082710bc387a Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 1 Jun 2025 01:29:09 -0700 Subject: [PATCH 12/18] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/array/max-by/lib/main.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js b/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js index e71b332d0ec8..925ca2afcad7 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js +++ b/lib/node_modules/@stdlib/stats/array/max-by/lib/main.js @@ -46,6 +46,7 @@ var GENERIC_DTYPE = 'generic'; * @param {*} [thisArg] - execution context * @throws {TypeError} first argument must be an array-like object * @throws {TypeError} first argument must have a supported data type +* @throws {TypeError} second argument must be a function * @returns {number} maximum value * * @example @@ -68,7 +69,7 @@ function maxBy( x, clbk, thisArg ) { throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', join( IDTYPES, '", "' ), dt ) ); } if ( !isFunction( clbk ) ) { - throw new TypeError( format( 'invalid argument. Callback function must be a function. Value: `%s`.', clbk ) ); + throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', clbk ) ); } return strided( x.length, x, 1, 0, wrapper ); From b9df70b0e6d3fa48339cb56d023e87ebe686f799 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 1 Jun 2025 01:31:45 -0700 Subject: [PATCH 13/18] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/stats/array/max-by/test/test.js | 35 ++----------------- 1 file changed, 3 insertions(+), 32 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/array/max-by/test/test.js b/lib/node_modules/@stdlib/stats/array/max-by/test/test.js index e8f81252fcfe..7019101a1071 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/test/test.js +++ b/lib/node_modules/@stdlib/stats/array/max-by/test/test.js @@ -74,7 +74,7 @@ tape( 'the function throws an error if provided a first argument which is not an function badValue( value ) { return function badValue() { - maxBy( value ); + maxBy( value, accessor ); }; } }); @@ -94,12 +94,12 @@ tape( 'the function throws an error if provided a first argument which has an un function badValue( value ) { return function badValue() { - maxBy( value ); + maxBy( value, accessor ); }; } }); -tape( 'the function throws an error if provided a callback function argument which is not a function', function test( t ) { +tape( 'the function throws an error if provided a second argument which is not a function', function test( t ) { var values; var i; var x; @@ -128,35 +128,6 @@ tape( 'the function throws an error if provided a callback function argument whi } }); -tape( 'the function throws an error if provided a callback function argument which is not a function (options)', function test( t ) { - var values; - var i; - var x; - - x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; - values = [ - '5', - 5, - NaN, - null, - void 0, - true, - [], - {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - maxBy( x, value, {} ); - }; - } -}); - tape( 'the function calculates the maximum value of an array via a callback function', function test( t ) { var x; var v; From ed38422be147ee8bfc5f5541aa74dba827e793be Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 1 Jun 2025 01:34:36 -0700 Subject: [PATCH 14/18] test: update descriptions Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/array/max-by/test/test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/array/max-by/test/test.js b/lib/node_modules/@stdlib/stats/array/max-by/test/test.js index 7019101a1071..07897b3ddcee 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/test/test.js +++ b/lib/node_modules/@stdlib/stats/array/max-by/test/test.js @@ -232,13 +232,13 @@ tape( 'if provided an empty array, the function returns `NaN` (accessors)', func t.end(); }); -tape( 'if provided an array containing a single element, the function returns the first element applying callback function', function test( t ) { +tape( 'if provided an array containing a single element, the function returns the result of applying a provided callback function', function test( t ) { var v = maxBy( [ 1.0 ], accessor ); t.strictEqual( v, 2.0, 'returns expected value' ); t.end(); }); -tape( 'if provided an array containing a single element, the function returns the first element applying callback function (accessors)', function test( t ) { +tape( 'if provided an array containing a single element, the function returns the result of applying a provided callback function (accessors)', function test( t ) { var v = maxBy( toAccessorArray( [ 1.0 ] ), accessor ); t.strictEqual( v, 2.0, 'returns expected value' ); t.end(); From 69df5398e0cd3824f65927f62a1cf433823b295f Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 1 Jun 2025 01:35:14 -0700 Subject: [PATCH 15/18] test: update descriptions Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/array/max-by/test/test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/array/max-by/test/test.js b/lib/node_modules/@stdlib/stats/array/max-by/test/test.js index 07897b3ddcee..38510d13121a 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/test/test.js +++ b/lib/node_modules/@stdlib/stats/array/max-by/test/test.js @@ -232,13 +232,13 @@ tape( 'if provided an empty array, the function returns `NaN` (accessors)', func t.end(); }); -tape( 'if provided an array containing a single element, the function returns the result of applying a provided callback function', function test( t ) { +tape( 'if provided an array containing a single element, the function returns the result of applying a provided callback function to that element', function test( t ) { var v = maxBy( [ 1.0 ], accessor ); t.strictEqual( v, 2.0, 'returns expected value' ); t.end(); }); -tape( 'if provided an array containing a single element, the function returns the result of applying a provided callback function (accessors)', function test( t ) { +tape( 'if provided an array containing a single element, the function returns the result of applying a provided callback function to that element (accessors)', function test( t ) { var v = maxBy( toAccessorArray( [ 1.0 ] ), accessor ); t.strictEqual( v, 2.0, 'returns expected value' ); t.end(); From 7a0df50650e6702017fac191f5c9692a341a2cb0 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 1 Jun 2025 01:36:37 -0700 Subject: [PATCH 16/18] test: add test Signed-off-by: Athan --- .../@stdlib/stats/array/max-by/test/test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/node_modules/@stdlib/stats/array/max-by/test/test.js b/lib/node_modules/@stdlib/stats/array/max-by/test/test.js index 38510d13121a..4d38a57add21 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/test/test.js +++ b/lib/node_modules/@stdlib/stats/array/max-by/test/test.js @@ -220,6 +220,25 @@ tape( 'the function supports providing a callback execution context', function t } }); +tape( 'the function supports providing a callback execution context (accessors)', function test( t ) { + var ctx; + var x; + + x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + ctx = { + 'count': 0 + }; + maxBy( toAccessorArray( x ), accessor, ctx ); + + t.strictEqual( ctx.count, x.length, 'returns expected value' ); + t.end(); + + function accessor( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v * 2.0; + } +}); + tape( 'if provided an empty array, the function returns `NaN`', function test( t ) { var v = maxBy( [], accessor ); t.strictEqual( isnan( v ), true, 'returns expected value' ); From 6fda0bd38b2273fe7f64f3e9106ef04757d387f5 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 1 Jun 2025 01:40:34 -0700 Subject: [PATCH 17/18] test: update callback tests Signed-off-by: Athan --- .../@stdlib/stats/array/max-by/test/test.js | 48 +++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/array/max-by/test/test.js b/lib/node_modules/@stdlib/stats/array/max-by/test/test.js index 4d38a57add21..aebc90d316f3 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/test/test.js +++ b/lib/node_modules/@stdlib/stats/array/max-by/test/test.js @@ -202,6 +202,10 @@ tape( 'the function calculates the maximum value of an array (array-like object) }); tape( 'the function supports providing a callback execution context', function test( t ) { + var expected; + var indices; + var values; + var arrays; var ctx; var x; @@ -209,32 +213,70 @@ tape( 'the function supports providing a callback execution context', function t ctx = { 'count': 0 }; + indices = []; + values = []; + arrays = []; maxBy( x, accessor, ctx ); t.strictEqual( ctx.count, x.length, 'returns expected value' ); + + expected = [ 0, 1, 2, 3, 4 ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ x, x, x, x, x ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + t.end(); - function accessor( v ) { + function accessor( v, idx, arr ) { this.count += 1; // eslint-disable-line no-invalid-this + indices.push( idx ); + values.push( v ); + arrays.push( arr ); return v * 2.0; } }); tape( 'the function supports providing a callback execution context (accessors)', function test( t ) { + var expected; + var indices; + var values; + var arrays; var ctx; + var ax; var x; x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + ax = toAccessorArray( x ); ctx = { 'count': 0 }; - maxBy( toAccessorArray( x ), accessor, ctx ); + indices = []; + values = []; + arrays = []; + maxBy( ax, accessor, ctx ); t.strictEqual( ctx.count, x.length, 'returns expected value' ); + + expected = [ 0, 1, 2, 3, 4 ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ ax, ax, ax, ax, ax ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + t.end(); - function accessor( v ) { + function accessor( v, idx, arr ) { this.count += 1; // eslint-disable-line no-invalid-this + indices.push( idx ); + values.push( v ); + arrays.push( arr ); return v * 2.0; } }); From 210584da8217fcd8f39e05a6edd5e34636192ae0 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 1 Jun 2025 01:50:02 -0700 Subject: [PATCH 18/18] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/array/max-by/test/test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/array/max-by/test/test.js b/lib/node_modules/@stdlib/stats/array/max-by/test/test.js index aebc90d316f3..b24349e41e2f 100644 --- a/lib/node_modules/@stdlib/stats/array/max-by/test/test.js +++ b/lib/node_modules/@stdlib/stats/array/max-by/test/test.js @@ -228,7 +228,6 @@ tape( 'the function supports providing a callback execution context', function t expected = [ x, x, x, x, x ]; t.deepEqual( arrays, expected, 'returns expected value' ); - t.end(); function accessor( v, idx, arr ) { @@ -269,7 +268,6 @@ tape( 'the function supports providing a callback execution context (accessors)' expected = [ ax, ax, ax, ax, ax ]; t.deepEqual( arrays, expected, 'returns expected value' ); - t.end(); function accessor( v, idx, arr ) {