diff --git a/lib/node_modules/@stdlib/stats/base/range-by/README.md b/lib/node_modules/@stdlib/stats/base/range-by/README.md index 1cb62cb91f51..3ce2e4defcf2 100644 --- a/lib/node_modules/@stdlib/stats/base/range-by/README.md +++ b/lib/node_modules/@stdlib/stats/base/range-by/README.md @@ -2,7 +2,7 @@ @license Apache-2.0 -Copyright (c) 2020 The Stdlib Authors. +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. @@ -38,7 +38,7 @@ The [**range**][range] is defined as the difference between the maximum and mini var rangeBy = require( '@stdlib/stats/base/range-by' ); ``` -#### rangeBy( N, x, stride, clbk\[, thisArg] ) +#### rangeBy( N, x, strideX, clbk\[, thisArg] ) Calculates the [range][range] of strided array `x` via a callback function. @@ -57,7 +57,7 @@ The function has the following parameters: - **N**: number of indexed elements. - **x**: input [`Array`][mdn-array], [`typed array`][mdn-typed-array], or an array-like object (excluding strings and functions). -- **stride**: index increment. +- **strideX**: stride length. - **clbk**: callback function. - **thisArg**: execution context (_optional_). @@ -89,7 +89,7 @@ var cnt = context.count; // returns 8 ``` -The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to access every other element +The `N` and `strideX` parameters determine which elements in `x` are accessed at runtime. For example, to access every other element ```javascript var floor = require( '@stdlib/math/base/special/floor' ); @@ -127,7 +127,7 @@ var v = rangeBy( N, x1, 2, accessor ); // returns 8.0 ``` -#### rangeBy.ndarray( N, x, stride, offset, clbk\[, thisArg] ) +#### rangeBy.ndarray( N, x, strideX, offset, clbk\[, thisArg] ) Calculates the [range][range] of strided array `x` via a callback function and using alternative indexing semantics. diff --git a/lib/node_modules/@stdlib/stats/base/range-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/range-by/benchmark/benchmark.js index 17e8ef1c2c19..5e2a32839092 100644 --- a/lib/node_modules/@stdlib/stats/base/range-by/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/range-by/benchmark/benchmark.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. @@ -21,11 +21,18 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +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 rangeBy = require( './../lib/range_by.js' ); +var rangeBy = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; // FUNCTIONS // @@ -49,13 +56,7 @@ function accessor( value ) { * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - x.push( ( randu()*20.0 ) - 10.0 ); - } + var x = uniform( len, -10, 10, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/range-by/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/range-by/benchmark/benchmark.ndarray.js index 427ab185fd51..53ddf8c546cf 100644 --- a/lib/node_modules/@stdlib/stats/base/range-by/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/range-by/benchmark/benchmark.ndarray.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/base/range-by/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/range-by/docs/repl.txt index a5427e4d614e..fc3445e704d6 100644 --- a/lib/node_modules/@stdlib/stats/base/range-by/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/range-by/docs/repl.txt @@ -1,5 +1,4 @@ - -{{alias}}( N, x, stride, clbk[, thisArg] ) +{{alias}}( N, x, strideX, clbk[, thisArg] ) Calculates the range of a strided array via a callback function. The `N` and `stride` parameters determine which elements in `x` are accessed @@ -14,7 +13,7 @@ - value: array element. - aidx: array index. - - sidx: strided index (offset + aidx*stride). + - sidx: strided index (offsetX + aidx*strideX). - array: the input array. The callback function should return a numeric value. @@ -31,7 +30,7 @@ Input array/collection. If provided an object, the object must be array- like (excluding strings and functions). - stride: integer + strideX: integer Index increment for `x`. clbk: Function @@ -53,7 +52,7 @@ > {{alias}}( x.length, x, 1, accessor ) 18.0 - // Using `N` and `stride` parameters: + // Using `N` and `strideX` parameters: > x = [ -2.0, 1.0, 3.0, -5.0, 4.0, -1.0, -3.0 ]; > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); > {{alias}}( N, x, 2, accessor ) @@ -66,12 +65,13 @@ > {{alias}}( N, x1, 2, accessor ) 8.0 -{{alias}}.ndarray( N, x, stride, offset, clbk[, thisArg] ) + +{{alias}}.ndarray( N, x, strideX, offsetX, clbk[, thisArg] ) Calculates the range of a strided array via a callback function and using alternative indexing semantics. While typed array views mandate a view offset based on the underlying - buffer, the `offset` parameter supports indexing semantics based on a + buffer, the `offsetX` parameter supports indexing semantics based on a starting index. Parameters @@ -83,10 +83,10 @@ Input array/collection. If provided an object, the object must be array- like (excluding strings and functions). - stride: integer + strideX: integer Index increment for `x`. - offset: integer + offsetX: integer Starting index of `x`. clbk: Function diff --git a/lib/node_modules/@stdlib/stats/base/range-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/range-by/docs/types/index.d.ts index ee65d0f01b7b..3de76051c8b1 100644 --- a/lib/node_modules/@stdlib/stats/base/range-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/range-by/docs/types/index.d.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. @@ -20,7 +20,14 @@ /// -import { Collection } from '@stdlib/types/array'; +// import { Collection } from '@stdlib/types/array'; +import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + + +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; /** * Returns an accessed value. @@ -65,7 +72,7 @@ type Ternary = ( this: U, value: T, aidx: number, sidx: number ) => number * @param array - input array * @returns accessed value */ -type Quaternary = ( this: U, value: T, aidx: number, sidx: number, array: Collection ) => number | void; +type Quaternary = ( this: U, value: T, aidx: number, sidx: number, array: InputArray ) => number | void; /** * Returns an accessed value. @@ -113,7 +120,7 @@ interface Routine { * var v = rangeBy( x.length, x, 1, accessor ); * // returns 18.0 */ - ( N: number, x: Collection, stride: number, clbk: Callback, thisArg?: ThisParameterType> ): number; + ( N: number, x: InputArray, stride: number, clbk: Callback, thisArg?: ThisParameterType> ): number; /** * Calculates the range of a strided array via a callback function and using alternative indexing semantics. @@ -147,7 +154,7 @@ interface Routine { * var v = rangeBy.ndarray( x.length, x, 1, 0, accessor ); * // returns 18.0 */ - ndarray( N: number, x: Collection, stride: number, offset: number, clbk: Callback, thisArg?: ThisParameterType> ): number; + ndarray( N: number, x: InputArray, stride: number, offset: number, clbk: Callback, thisArg?: ThisParameterType> ): number; } /** diff --git a/lib/node_modules/@stdlib/stats/base/range-by/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/range-by/docs/types/test.ts index f1f5674896d9..dff4896a2488 100644 --- a/lib/node_modules/@stdlib/stats/base/range-by/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/range-by/docs/types/test.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. @@ -16,7 +16,8 @@ * limitations under the License. */ -import rangeBy = require( './index' ); +import rangeBy = require( '@stdlib/stats/base/range-by' ); +import AccessorArray = require( '@stdlib/array/base/accessor' ); const accessor = (): number => { return 5.0; @@ -31,6 +32,8 @@ const accessor = (): number => { rangeBy( x.length, x, 1, accessor ); // $ExpectType number rangeBy( x.length, x, 1, accessor, {} ); // $ExpectType number + rangeBy( x.length, new AccessorArray ( x ), 1, accessor ); // $ExpectType number + rangeBy( x.length, new AccessorArray ( x ), 1, accessor, {} ); // $ExpectType number } // The compiler throws an error if the function is provided a first argument which is not a number... @@ -70,7 +73,7 @@ const accessor = (): number => { rangeBy( x.length, x, undefined, accessor ); // $ExpectError rangeBy( x.length, x, [], accessor ); // $ExpectError rangeBy( x.length, x, {}, accessor ); // $ExpectError - rangeBy( x.length, x, ( x: number, accessor ): number => x, accessor ); // $ExpectError + rangeBy( x.length, x, ( x: number ): number => x, accessor ); // $ExpectError } // The compiler throws an error if the function is provided a fourth argument which is not a function... @@ -103,6 +106,8 @@ const accessor = (): number => { rangeBy.ndarray( x.length, x, 1, 0, accessor ); // $ExpectType number rangeBy.ndarray( x.length, x, 1, 0, accessor, {} ); // $ExpectType number + rangeBy.ndarray( x.length, new AccessorArray ( x ), 1, 0, accessor ); // $ExpectType number + rangeBy.ndarray( x.length, new AccessorArray ( x ), 1, 0, accessor, {} ); // $ExpectType number } // The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... diff --git a/lib/node_modules/@stdlib/stats/base/range-by/examples/index.js b/lib/node_modules/@stdlib/stats/base/range-by/examples/index.js index 2c25e656bb8b..c2cae6048cd5 100644 --- a/lib/node_modules/@stdlib/stats/base/range-by/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/range-by/examples/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/base/range-by/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/range-by/lib/accessors.js new file mode 100644 index 000000000000..ca5c6b182781 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/range-by/lib/accessors.js @@ -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. +*/ + +'use strict'; + +// MODULES // + +var isnan = require( '@stdlib/math/base/assert/is-nan' ); + + +// MAIN // + +/** +* Calculates the range of a strided array via a callback function. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {NumericArray|Collection|AccessorArrayLike} x - input array/collection +* @param {integer} strideX - stride length for `x` +* @param {NonNegativeInteger} offsetX - starting index +* @param {Callback} clbk - callback +* @param {*} [thisArg] - execution context +* @returns {number} range +* +* @example +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; +* +* function accessor( v ) { +* if ( v === void 0 ) { +* return; +* } +* return v * 2.0; +* } +* +* var v = rangeBy( x.length, arraylike2object(x), 1, 0, accessor ); +* // returns 18.0 +*/ +function rangeBy( N, x, strideX, offsetX, clbk, thisArg) { + var xbuf; + var get; + var max; + var min; + var ix; + var v; + var i; + + xbuf = x.data; + + get = x.accessors[ 0 ]; + + if ( N === 1 || strideX === 0 ) { + v = clbk.call( thisArg, get(xbuf, offsetX), 0, 0, x ); + if ( v === void 0 || isnan( v ) ) { + return NaN; + } + return 0.0; + } + + ix = offsetX; + for ( i = 0; i < N; i++ ) { + min = clbk.call( thisArg, get(xbuf, ix), i, ix, x ); + if ( min !== void 0 ) { + break; + } + ix += strideX; + } + if ( i === N ) { + return NaN; + } + max = min; + i += 1; + for ( i; i < N; i++ ) { + ix += strideX; + v = clbk.call( thisArg, get(xbuf, ix), i, ix, x ); + if ( v === void 0 ) { + continue; + } + if ( isnan( v ) ) { + return v; + } + if ( v < min ) { + min = v; + } else if ( v > max ) { + max = v; + } + } + return max - min; +} + + +// EXPORTS // + +module.exports = rangeBy; diff --git a/lib/node_modules/@stdlib/stats/base/range-by/lib/index.js b/lib/node_modules/@stdlib/stats/base/range-by/lib/index.js index dfc0f442b883..e10d23e20836 100644 --- a/lib/node_modules/@stdlib/stats/base/range-by/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/range-by/lib/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/base/range-by/lib/main.js b/lib/node_modules/@stdlib/stats/base/range-by/lib/main.js index 237cd1ff444b..14a623095f81 100644 --- a/lib/node_modules/@stdlib/stats/base/range-by/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/range-by/lib/main.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/base/range-by/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/range-by/lib/ndarray.js index dbad088a497d..d4b926156de1 100644 --- a/lib/node_modules/@stdlib/stats/base/range-by/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/range-by/lib/ndarray.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. @@ -21,6 +21,8 @@ // MODULES // var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); // MAIN // @@ -29,9 +31,9 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); * Calculates the range of a strided array via a callback function. * * @param {PositiveInteger} N - number of indexed elements -* @param {Collection} x - input array/collection -* @param {integer} stride - index increment -* @param {NonNegativeInteger} offset - starting index +* @param {NumericArray|Collection|AccessorArrayLike} x - input array/collection +* @param {integer} strideX - stride length for `x` +* @param {NonNegativeInteger} offsetX - starting index * @param {Callback} clbk - callback * @param {*} [thisArg] - execution context * @returns {number} range @@ -40,36 +42,44 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); * 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; +* if ( v === void 0 ) { +* return; +* } +* return v * 2.0; * } * * var v = rangeBy( x.length, x, 1, 0, accessor ); * // returns 18.0 */ -function rangeBy( N, x, stride, offset, clbk, thisArg ) { +function rangeBy( N, x, strideX, offsetX, clbk, thisArg ) { var max; var min; var ix; var v; var i; + var o; if ( N <= 0 ) { return NaN; } - if ( N === 1 || stride === 0 ) { + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, o, strideX, offsetX, clbk, thisArg ); + } + if ( N === 1 || strideX === 0 ) { v = clbk.call( thisArg, x[ 0 ], 0, 0, x ); if ( v === void 0 || isnan( v ) ) { return NaN; } return 0.0; } - ix = offset; + ix = offsetX; for ( i = 0; i < N; i++ ) { min = clbk.call( thisArg, x[ ix ], i, ix, x ); if ( min !== void 0 ) { break; } - ix += stride; + ix += strideX; } if ( i === N ) { return NaN; @@ -77,7 +87,7 @@ function rangeBy( N, x, stride, offset, clbk, thisArg ) { max = min; i += 1; for ( i; i < N; i++ ) { - ix += stride; + ix += strideX; v = clbk.call( thisArg, x[ ix ], i, ix, x ); if ( v === void 0 ) { continue; diff --git a/lib/node_modules/@stdlib/stats/base/range-by/lib/range_by.js b/lib/node_modules/@stdlib/stats/base/range-by/lib/range_by.js index b4c87ca8271c..cb374ab0c995 100644 --- a/lib/node_modules/@stdlib/stats/base/range-by/lib/range_by.js +++ b/lib/node_modules/@stdlib/stats/base/range-by/lib/range_by.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. @@ -20,7 +20,8 @@ // MODULES // -var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -29,8 +30,8 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); * Calculates the range of a strided array via a callback function. * * @param {PositiveInteger} N - number of indexed elements -* @param {Collection} x - input array/collection -* @param {integer} stride - index increment +* @param {NumericArray|Collection|AccessorArrayLike} x - input array/collection +* @param {integer} strideX - index increment * @param {Callback} clbk - callback * @param {*} [thisArg] - execution context * @returns {number} range @@ -39,62 +40,17 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); * 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; +* if ( v === void 0 ) { +* return; +* } +* return v * 2.0; * } * * var v = rangeBy( x.length, x, 1, accessor ); * // returns 18.0 */ -function rangeBy( N, x, stride, clbk, thisArg ) { - var max; - var min; - var ix; - var v; - var i; - - if ( N <= 0 ) { - return NaN; - } - if ( N === 1 || stride === 0 ) { - v = clbk.call( thisArg, x[ 0 ], 0, 0, x ); - if ( v === void 0 || isnan( v ) ) { - return NaN; - } - return 0.0; - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - for ( i = 0; i < N; i++ ) { - min = clbk.call( thisArg, x[ ix ], i, ix, x ); - if ( min !== void 0 ) { - break; - } - ix += stride; - } - if ( i === N ) { - return NaN; - } - max = min; - i += 1; - for ( i; i < N; i++ ) { - ix += stride; - v = clbk.call( thisArg, x[ ix ], i, ix, x ); - if ( v === void 0 ) { - continue; - } - if ( isnan( v ) ) { - return v; - } - if ( v < min ) { - min = v; - } else if ( v > max ) { - max = v; - } - } - return max - min; +function rangeBy( N, x, strideX, clbk, thisArg ) { + return ndarray(N, x, strideX, stride2offset(N, strideX), clbk, thisArg); } diff --git a/lib/node_modules/@stdlib/stats/base/range-by/test/test.js b/lib/node_modules/@stdlib/stats/base/range-by/test/test.js index a3de23413280..4bcd38aee8a9 100644 --- a/lib/node_modules/@stdlib/stats/base/range-by/test/test.js +++ b/lib/node_modules/@stdlib/stats/base/range-by/test/test.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/base/range-by/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/range-by/test/test.ndarray.js index 4dfd226b7998..73355c48008d 100644 --- a/lib/node_modules/@stdlib/stats/base/range-by/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/range-by/test/test.ndarray.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. @@ -24,6 +24,7 @@ var tape = require( 'tape' ); var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var rangeBy = require( './../lib/ndarray.js' ); @@ -53,6 +54,7 @@ tape( 'the function has an arity of 6', function test( t ) { tape( 'the function calculates the range of a strided array via a callback function', function test( t ) { var x; var v; + var i; x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; v = rangeBy( x.length, x, 1, 0, accessor ); @@ -74,11 +76,17 @@ tape( 'the function calculates the range of a strided array via a callback funct v = rangeBy( x.length, x, 1, 0, accessor ); t.strictEqual( isnan( v ), true, 'returns expected value' ); - x = new Array( 5 ); // sparse array + x = []; + for (i = 0; i < 5; i++) { + x.push(undefined); + } v = rangeBy( x.length, x, 1, 0, accessor ); t.strictEqual( isnan( v ), true, 'returns expected value' ); - x = new Array( 5 ); // sparse array + x = []; + for (i = 0; i < 5; i++) { + x.push(undefined); + } x[ 2 ] = 1.0; v = rangeBy( x.length, x, 1, 0, accessor ); t.strictEqual( v, 0.0, 'returns expected value' ); @@ -86,6 +94,49 @@ tape( 'the function calculates the range of a strided array via a callback funct t.end(); }); +tape( 'the function calculates the range of a strided array via a callback function (accessors)', function test( t ) { + var x; + var v; + var i; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = rangeBy( x.length, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( v, 18.0, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = rangeBy( x.length, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( v, 2.0, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = rangeBy( x.length, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = rangeBy( x.length, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = rangeBy( x.length, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = []; + for (i = 0; i < 5; i++) { + x.push(undefined); + } + v = rangeBy( x.length, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = []; + for (i = 0; i < 5; i++) { + x.push(undefined); + } + x[ 2 ] = 1.0; + v = rangeBy( x.length, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN`', function test( t ) { var x; var v; @@ -104,13 +155,17 @@ tape( 'if provided an `N` parameter less than or equal to `0`, the function retu tape( 'if provided an `N` parameter equal to `1`, the function returns `0`', function test( t ) { var x; var v; + var i; x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; v = rangeBy( 1, x, 1, 0, accessor ); t.strictEqual( v, 0.0, 'returns expected value' ); - x = new Array( 1 ); // sparse array + x = []; + for (i = 0; i < 1; i++) { + x.push(undefined); + } v = rangeBy( 1, x, 1, 0, accessor ); t.strictEqual( isnan( v ), true, 'returns expected value' ); @@ -167,13 +222,16 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', function test( t ) { var x; var v; - + var i; x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; v = rangeBy( x.length, x, 0, 0, accessor ); t.strictEqual( v, 0.0, 'returns expected value' ); - x = new Array( 1 ); // sparse array + x = []; + for (i = 0; i < 1; i++) { + x.push(undefined); + } v = rangeBy( 1, x, 0, 0, accessor ); t.strictEqual( isnan( v ), true, 'returns expected value' ); diff --git a/lib/node_modules/@stdlib/stats/base/range-by/test/test.range_by.js b/lib/node_modules/@stdlib/stats/base/range-by/test/test.range_by.js index aa3a295b2547..f030c3d0abec 100644 --- a/lib/node_modules/@stdlib/stats/base/range-by/test/test.range_by.js +++ b/lib/node_modules/@stdlib/stats/base/range-by/test/test.range_by.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. @@ -25,7 +25,8 @@ var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); var Float64Array = require( '@stdlib/array/float64' ); -var rangeBy = require( './../lib/range_by.js' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var rangeBy = require( './../lib/main.js' ); // FUNCTIONS // @@ -54,6 +55,7 @@ tape( 'the function has an arity of 5', function test( t ) { tape( 'the function calculates the range of a strided array via a callback function', function test( t ) { var x; var v; + var i; x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; v = rangeBy( x.length, x, 1, accessor ); @@ -75,11 +77,17 @@ tape( 'the function calculates the range of a strided array via a callback funct v = rangeBy( x.length, x, 1, accessor ); t.strictEqual( isnan( v ), true, 'returns expected value' ); - x = new Array( 5 ); // sparse array + x = []; + for (i = 0; i < 5; i++) { + x.push(undefined); + } v = rangeBy( x.length, x, 1, accessor ); t.strictEqual( isnan( v ), true, 'returns expected value' ); - x = new Array( 5 ); // sparse array + x = []; + for (i = 0; i < 5; i++) { + x.push(undefined); + } x[ 2 ] = 1.0; v = rangeBy( x.length, x, 1, accessor ); t.strictEqual( v, 0.0, 'returns expected value' ); @@ -87,6 +95,49 @@ tape( 'the function calculates the range of a strided array via a callback funct t.end(); }); +tape( 'the function calculates the range of a strided array via a callback function (accessors)', function test( t ) { + var x; + var v; + var i; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = rangeBy( x.length, toAccessorArray(x), 1, accessor ); + t.strictEqual( v, 18.0, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = rangeBy( x.length, toAccessorArray(x), 1, accessor ); + t.strictEqual( v, 2.0, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = rangeBy( x.length, toAccessorArray(x), 1, accessor ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = rangeBy( x.length, toAccessorArray(x), 1, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = rangeBy( x.length, toAccessorArray(x), 1, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = []; + for (i = 0; i < 5; i++) { + x.push(undefined); // Explicitly adding `undefined` + } + v = rangeBy( x.length, toAccessorArray(x), 1, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = []; + for (i = 0; i < 5; i++) { + x.push(undefined); // Explicitly adding `undefined` + } + x[ 2 ] = 1.0; + v = rangeBy( x.length, toAccessorArray(x), 1, accessor ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN`', function test( t ) { var x; var v; @@ -105,13 +156,17 @@ tape( 'if provided an `N` parameter less than or equal to `0`, the function retu tape( 'if provided an `N` parameter equal to `1`, the function returns `0`', function test( t ) { var x; var v; + var i; x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; v = rangeBy( 1, x, 1, accessor ); t.strictEqual( v, 0.0, 'returns expected value' ); - x = new Array( 1 ); // sparse array + x = []; + for (i = 0; i < 1; i++) { + x.push(undefined); // Explicitly adding `undefined` + } v = rangeBy( 1, x, 1, accessor ); t.strictEqual( isnan( v ), true, 'returns expected value' ); @@ -168,13 +223,17 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', function test( t ) { var x; var v; + var i; x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; v = rangeBy( x.length, x, 0, accessor ); t.strictEqual( v, 0.0, 'returns expected value' ); - x = new Array( 1 ); // sparse array + x = []; + for (i = 0; i < 1; i++) { + x.push(undefined); + } v = rangeBy( 1, x, 0, accessor ); t.strictEqual( isnan( v ), true, 'returns expected value' );