Skip to content

Commit 7c0f342

Browse files
authored
Create accessors.js
Signed-off-by: Kaushikgtm <162317291+Kaushikgtm@users.noreply.github.com>
1 parent a644ffe commit 7c0f342

File tree

1 file changed

+103
-0
lines changed
  • lib/node_modules/@stdlib/stats/base/nanrange-by/lib

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
24+
25+
// MAIN //
26+
27+
/**
28+
* Calculates the range of a strided array via a callback function, ignoring `NaN` values.
29+
*
30+
* @param {PositiveInteger} N - number of indexed elements
31+
* @param {Collection} x - input array/collection
32+
* @param {integer} stride - index increment
33+
* @param {NonNegativeInteger} offset - starting index
34+
* @param {Callback} clbk - callback
35+
* @param {*} [thisArg] - execution context
36+
* @returns {number} range
37+
*
38+
* @example
39+
* var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0 ];
40+
*
41+
* function accessor( v ) {
42+
* return v * 2.0;
43+
* }
44+
*
45+
* var v = nanrangeBy( x.length, x, 1, 0, accessor );
46+
* // returns 18.0
47+
*/
48+
function nanrangeBy( N, x, strideX, offsetX, clbk, thisArg ) {
49+
var xbuf;
50+
var get;
51+
var max;
52+
var min;
53+
var ix;
54+
var v;
55+
var i;
56+
57+
// Cache reference to array data:
58+
xbuf = x.data;
59+
60+
// Cache a reference to the element accessor:
61+
get = x.accessors[0];
62+
if ( N <= 0 ) {
63+
return NaN;
64+
}
65+
if ( N === 1 || strideX === 0 ) {
66+
v = clbk.call( thisArg, get( xbuf, 0 ), 0, 0, x );
67+
if ( v === void 0 || isnan( v ) ) {
68+
return NaN;
69+
}
70+
return 0.0;
71+
}
72+
ix = offsetX;
73+
for ( i = 0; i < N; i++ ) {
74+
min = clbk.call( thisArg, get( xbuf, ix ), i, ix, x );
75+
if ( min === min && min !== void 0 ) {
76+
break;
77+
}
78+
ix += strideX;
79+
}
80+
if ( i === N ) {
81+
return NaN;
82+
}
83+
max = min;
84+
i += 1;
85+
for ( i; i < N; i++ ) {
86+
ix += strideX;
87+
v = clbk.call( thisArg, get( xbuf, ix ), i, ix, x );
88+
if ( v === void 0 || isnan( v ) ) {
89+
continue;
90+
}
91+
if ( v < min ) {
92+
min = v;
93+
} else if ( v > max ) {
94+
max = v;
95+
}
96+
}
97+
return max - min;
98+
}
99+
100+
101+
// EXPORTS //
102+
103+
module.exports = nanrangeBy;

0 commit comments

Comments
 (0)