Skip to content

Commit 628896c

Browse files
committed
refactor: update docs and tests
--- 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: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - 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 ---
1 parent 7bf215b commit 628896c

File tree

5 files changed

+86
-55
lines changed

5 files changed

+86
-55
lines changed

lib/node_modules/@stdlib/stats/array/min-by/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ The function has the following parameters:
5757
- **clbk**: callback function.
5858
- **thisArg**: execution context (_optional_).
5959

60-
The invoked callback is provided four arguments:
60+
The invoked callback is provided three arguments:
6161

62-
- **value**: array element.
63-
- **aidx**: array index.
64-
- **array**: input array/collection.
62+
- **value**: current array element.
63+
- **index**: current array index.
64+
- **array**: input array.
6565

6666
To set the callback execution context, provide a `thisArg`.
6767

lib/node_modules/@stdlib/stats/array/min-by/docs/repl.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
The callback function is provided three arguments:
88

9-
- value: array element.
10-
- aidx: array index.
9+
- value: current array element.
10+
- index: current array index.
1111
- array: the input array.
1212

1313
The callback function should return a numeric value.

lib/node_modules/@stdlib/stats/array/min-by/docs/types/index.d.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,44 +32,44 @@ type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
3232
*
3333
* @returns accessed value
3434
*/
35-
type Nullary<U> = ( this: U ) => number | void;
35+
type Nullary<ThisArg> = ( this: ThisArg ) => number | void;
3636

3737
/**
3838
* Returns an accessed value.
3939
*
40-
* @param value - array element
40+
* @param value - current array element
4141
* @returns accessed value
4242
*/
43-
type Unary<T, U> = ( this: U, value: T ) => number | void;
43+
type Unary<T, ThisArg> = ( this: ThisArg, value: T ) => number | void;
4444

4545
/**
4646
* Returns an accessed value.
4747
*
48-
* @param value - array element
49-
* @param aidx - array index
48+
* @param value - current array element
49+
* @param index - current array index
5050
* @returns accessed value
5151
*/
52-
type Binary<T, U> = ( this: U, value: T, aidx: number ) => number | void;
52+
type Binary<T, ThisArg> = ( this: ThisArg, value: T, index: number ) => number | void;
5353

5454
/**
5555
* Returns an accessed value.
5656
*
57-
* @param value - array element
58-
* @param aidx - array index
57+
* @param value - current array element
58+
* @param index - current array index
5959
* @param array - input array
6060
* @returns accessed value
6161
*/
62-
type Ternary<T, U> = ( this: U, value: T, aidx: number, array: Collection<T> ) => number | void;
62+
type Ternary<T, U, ThisArg> = ( this: ThisArg, value: T, index: number, array: U ) => number | void;
6363

6464
/**
6565
* Returns an accessed value.
6666
*
67-
* @param value - array element
68-
* @param aidx - array index
67+
* @param value - current array element
68+
* @param index - current array index
6969
* @param array - input array
7070
* @returns accessed value
7171
*/
72-
type Callback<T, U> = Nullary<U> | Unary<T, U> | Binary<T, U> | Ternary<T, U>;
72+
type Callback<T, U, ThisArg> = Nullary<ThisArg> | Unary<T, ThisArg> | Binary<T, ThisArg> | Ternary<T, U, ThisArg>;
7373

7474
/**
7575
* Computes the minimum value of an array via a callback function.
@@ -89,7 +89,7 @@ type Callback<T, U> = Nullary<U> | Unary<T, U> | Binary<T, U> | Ternary<T, U>;
8989
* var v = minBy( x, accessor );
9090
* // returns -10.0
9191
*/
92-
declare function minBy<T = number, U = unknown>( x: InputArray, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
92+
declare function minBy<T = number, U extends InputArray = InputArray, ThisArg = unknown>( x: U, clbk: Callback<T, U, ThisArg>, thisArg?: ThisParameterType<Callback<T, U, ThisArg>> ): number;
9393

9494

9595
// EXPORTS //

lib/node_modules/@stdlib/stats/array/min-by/lib/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var GENERIC_DTYPE = 'generic';
4646
* @param {*} [thisArg] - execution context
4747
* @throws {TypeError} first argument must be an array-like object
4848
* @throws {TypeError} first argument must have a supported data type
49+
* @throws {TypeError} second argument must be a function
4950
* @returns {number} minimum value
5051
*
5152
* @example
@@ -68,7 +69,7 @@ function minBy( x, clbk, thisArg ) {
6869
throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', join( IDTYPES, '", "' ), dt ) );
6970
}
7071
if ( !isFunction( clbk ) ) {
71-
throw new TypeError( format( 'invalid argument. Callback function must be a function. Value: `%s`.', clbk ) );
72+
throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', clbk ) );
7273
}
7374
return strided( x.length, x, 1, 0, wrapper );
7475

lib/node_modules/@stdlib/stats/array/min-by/test/test.js

Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ tape( 'the function throws an error if provided a first argument which is not an
7474

7575
function badValue( value ) {
7676
return function badValue() {
77-
minBy( value );
77+
minBy( value, accessor );
7878
};
7979
}
8080
});
@@ -94,12 +94,12 @@ tape( 'the function throws an error if provided a first argument which has an un
9494

9595
function badValue( value ) {
9696
return function badValue() {
97-
minBy( value );
97+
minBy( value, accessor );
9898
};
9999
}
100100
});
101101

102-
tape( 'the function throws an error if provided a callback function argument which is not a function', function test( t ) {
102+
tape( 'the function throws an error if provided a second argument which is not a function', function test( t ) {
103103
var values;
104104
var i;
105105
var x;
@@ -128,35 +128,6 @@ tape( 'the function throws an error if provided a callback function argument whi
128128
}
129129
});
130130

131-
tape( 'the function throws an error if provided a callback function argument which is not a function (options)', function test( t ) {
132-
var values;
133-
var i;
134-
var x;
135-
136-
x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ];
137-
values = [
138-
'5',
139-
5,
140-
NaN,
141-
null,
142-
void 0,
143-
true,
144-
[],
145-
{}
146-
];
147-
148-
for ( i = 0; i < values.length; i++ ) {
149-
t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] );
150-
}
151-
t.end();
152-
153-
function badValue( value ) {
154-
return function badValue() {
155-
minBy( x, value, {} );
156-
};
157-
}
158-
});
159-
160131
tape( 'the function calculates the minimum value of an array via a callback function', function test( t ) {
161132
var x;
162133
var v;
@@ -231,20 +202,79 @@ tape( 'the function calculates the minimum value of an array (array-like object)
231202
});
232203

233204
tape( 'the function supports providing a callback execution context', function test( t ) {
205+
var expected;
206+
var indices;
207+
var values;
208+
var arrays;
234209
var ctx;
235210
var x;
236211

237212
x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
238213
ctx = {
239214
'count': 0
240215
};
216+
indices = [];
217+
values = [];
218+
arrays = [];
241219
minBy( x, accessor, ctx );
242220

243221
t.strictEqual( ctx.count, x.length, 'returns expected value' );
222+
223+
expected = [ 0, 1, 2, 3, 4 ];
224+
t.deepEqual( indices, expected, 'returns expected value' );
225+
226+
expected = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
227+
t.deepEqual( values, expected, 'returns expected value' );
228+
229+
expected = [ x, x, x, x, x ];
230+
t.deepEqual( arrays, expected, 'returns expected value' );
231+
t.end();
232+
233+
function accessor( v, idx, arr ) {
234+
this.count += 1; // eslint-disable-line no-invalid-this
235+
indices.push( idx );
236+
values.push( v );
237+
arrays.push( arr );
238+
return v * 2.0;
239+
}
240+
});
241+
242+
tape( 'the function supports providing a callback execution context (accessors)', function test( t ) {
243+
var expected;
244+
var indices;
245+
var values;
246+
var arrays;
247+
var ctx;
248+
var ax;
249+
var x;
250+
251+
x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
252+
ax = toAccessorArray( x );
253+
ctx = {
254+
'count': 0
255+
};
256+
indices = [];
257+
values = [];
258+
arrays = [];
259+
minBy( ax, accessor, ctx );
260+
261+
t.strictEqual( ctx.count, x.length, 'returns expected value' );
262+
263+
expected = [ 0, 1, 2, 3, 4 ];
264+
t.deepEqual( indices, expected, 'returns expected value' );
265+
266+
expected = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
267+
t.deepEqual( values, expected, 'returns expected value' );
268+
269+
expected = [ ax, ax, ax, ax, ax ];
270+
t.deepEqual( arrays, expected, 'returns expected value' );
244271
t.end();
245272

246-
function accessor( v ) {
273+
function accessor( v, idx, arr ) {
247274
this.count += 1; // eslint-disable-line no-invalid-this
275+
indices.push( idx );
276+
values.push( v );
277+
arrays.push( arr );
248278
return v * 2.0;
249279
}
250280
});
@@ -261,13 +291,13 @@ tape( 'if provided an empty array, the function returns `NaN` (accessors)', func
261291
t.end();
262292
});
263293

264-
tape( 'if provided an array containing a single element, the function returns the first element applying callback function', function test( t ) {
294+
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 ) {
265295
var v = minBy( [ 1.0 ], accessor );
266296
t.strictEqual( v, 2.0, 'returns expected value' );
267297
t.end();
268298
});
269299

270-
tape( 'if provided an array containing a single element, the function returns the first element applying callback function (accessors)', function test( t ) {
300+
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 ) {
271301
var v = minBy( toAccessorArray( [ 1.0 ] ), accessor );
272302
t.strictEqual( v, 2.0, 'returns expected value' );
273303
t.end();

0 commit comments

Comments
 (0)