Skip to content

Commit 775b485

Browse files
committed
Add Typescript definition
1 parent e1bcdb7 commit 775b485

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
// TypeScript Version: 2.0
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection } from '@stdlib/types/object';
24+
25+
/**
26+
* Interface defining function options.
27+
*/
28+
interface Options {
29+
/**
30+
* Method name determining how ties are treated (`average`, `min`, `max`, `dense`, or `ordinal`; default: 'average').
31+
*/
32+
method?: 'average' | 'min' | 'max' | 'dense' | 'ordinal';
33+
34+
/**
35+
* Determines where missing values go (`first`,`last`, or `remove`; default: 'last').
36+
*/
37+
missing?: 'first' | 'last' | 'remove';
38+
39+
/**
40+
* Array of values encoding missing values (default: [null,NaN]).
41+
*/
42+
encoding?: Array<any>;
43+
}
44+
45+
46+
/**
47+
* Computes the sample ranks for the values of an array-like object.
48+
*
49+
* ## Notes
50+
*
51+
* - When all elements of the `array` are different, the ranks are uniquely determined. When there are equal elements (called *ties*), the `method` option determines how they are handled. The default, `'average'`, replaces the ranks of the ties by their mean. Other possible options are `'min'` and `'max'`, which replace the ranks of the ties by their minimum and maximum, respectively. `'dense'` works like `'min'`, with the difference that the next highest element after a tie is assigned the next smallest integer. Finally, `ordinal` gives each element in `arr` a distinct rank, according to the position they appear in.
52+
* - The `missing` option is used to specify how to handle missing data. By default, `NaN` or `null` are treated as missing values. `'last'`specifies that missing values are placed last, `'first'` that the are assigned the lowest ranks and `'remove'` means that they are removed from the array before the ranks are calculated.
53+
*
54+
* @param x - data array
55+
* @param options - options object
56+
* @param options.method - method name determining how ties are treated (`average`, `min`, `max`, `dense`, or `ordinal`; default: 'average')
57+
* @param options.missing - determines where missing values go (`first`,`last`, or `remove`; default: 'last')
58+
* @param options.encoding - array of values encoding missing values
59+
* @throws must provide valid options
60+
* @returns array containing the computed ranks for the elements of x
61+
*
62+
* @example
63+
* var arr = [ 1.1, 2.0, 3.5, 0.0, 2.4 ];
64+
* var out = ranks( arr );
65+
* // returns [ 2, 3, 5, 1, 4 ]
66+
*
67+
* @example
68+
* // Ties are averaged:
69+
* arr = [ 2, 2, 1, 4, 3 ];
70+
* out = ranks( arr );
71+
* // returns [ 2.5, 2.5, 1, 5, 4 ]
72+
*
73+
* @example
74+
* // Missing values are placed last:
75+
* arr = [ null, 2, 2, 1, 4, 3, NaN, NaN ];
76+
* out = ranks( arr );
77+
* // returns [ 6, 2.5, 2.5, 1, 5, 4, 7 ,8 ]
78+
*/
79+
declare function ranks( x: Collection, options?: Options ): Array<number>;
80+
81+
82+
// EXPORTS //
83+
84+
export = ranks;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
import ranks = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array of numbers...
25+
{
26+
ranks( [ 1.1, 2.0, 3.5, 0.0, 2.4 ] ); // $ExpectType number[]
27+
ranks( [ 1.1, 2.0, 3.5, 0.0, 2.4 ], { 'encoding': [ NaN ] } ); // $ExpectType number[]
28+
ranks( [ 1.1, 2.0, 3.5, 0.0, 2.4 ], { 'missing': 'first' } ); // $ExpectType number[]
29+
ranks( [ 1.1, 2.0, 3.5, 0.0, 2.4 ], { 'method': 'min' } ); // $ExpectType number[]
30+
}
31+
32+
// The function does not compile if provided a value other than a collection...
33+
{
34+
ranks( true ); // $ExpectError
35+
ranks( false ); // $ExpectError
36+
ranks( null ); // $ExpectError
37+
ranks( undefined ); // $ExpectError
38+
ranks( 5 ); // $ExpectError
39+
ranks( {} ); // $ExpectError
40+
}
41+
42+
// The compiler throws an error if the function is provided a second argument which is not an options object...
43+
{
44+
const arr = [ 1.1, 2.0, 3.5, 0.0, 2.4 ] ;
45+
ranks( arr, true ); // $ExpectError
46+
ranks( arr, false ); // $ExpectError
47+
ranks( arr, null ); // $ExpectError
48+
ranks( arr, 5 ); // $ExpectError
49+
ranks( arr, 'abc' ); // $ExpectError
50+
ranks( arr, ( x: number ): number => x ); // $ExpectError
51+
}
52+
53+
// The compiler throws an error if the function is provided a `method` option which is not a recognized method...
54+
{
55+
const arr = [ 1.1, 2.0, 3.5, 0.0, 2.4 ] ;
56+
ranks( arr, { 'method': 'abc' } ); // $ExpectError
57+
ranks( arr, { 'method': '123' } ); // $ExpectError
58+
ranks( arr, { 'method': true } ); // $ExpectError
59+
ranks( arr, { 'method': false } ); // $ExpectError
60+
ranks( arr, { 'method': null } ); // $ExpectError
61+
ranks( arr, { 'method': [] } ); // $ExpectError
62+
ranks( arr, { 'method': {} } ); // $ExpectError
63+
ranks( arr, { 'method': ( x: number ): number => x } ); // $ExpectError
64+
}
65+
66+
// The compiler throws an error if the function is provided a `missing` option which is not a recognized missing value directive...
67+
{
68+
const arr = [ 1.1, 2.0, 3.5, 0.0, 2.4 ] ;
69+
ranks( arr, { 'missing': 'abc' } ); // $ExpectError
70+
ranks( arr, { 'missing': '123' } ); // $ExpectError
71+
ranks( arr, { 'missing': true } ); // $ExpectError
72+
ranks( arr, { 'missing': false } ); // $ExpectError
73+
ranks( arr, { 'missing': null } ); // $ExpectError
74+
ranks( arr, { 'missing': [] } ); // $ExpectError
75+
ranks( arr, { 'missing': {} } ); // $ExpectError
76+
ranks( arr, { 'missing': ( x: number ): number => x } ); // $ExpectError
77+
}
78+
79+
// The compiler throws an error if the function is provided an `encoding` option which is not an array...
80+
{
81+
const arr = [ 1.1, 2.0, 3.5, 0.0, 2.4 ] ;
82+
ranks( arr, { 'encoding': 'abc' } ); // $ExpectError
83+
ranks( arr, { 'encoding': '123' } ); // $ExpectError
84+
ranks( arr, { 'encoding': true } ); // $ExpectError
85+
ranks( arr, { 'encoding': false } ); // $ExpectError
86+
ranks( arr, { 'encoding': null } ); // $ExpectError
87+
ranks( arr, { 'encoding': {} } ); // $ExpectError
88+
ranks( arr, { 'encoding': ( x: number ): number => x } ); // $ExpectError
89+
}
90+
91+
// The function does not compile if provided an invalid number of arguments...
92+
{
93+
const arr = [ 1.1, 2.0, 3.5, 0.0, 2.4 ] ;
94+
ranks(); // $ExpectError
95+
ranks( arr, {}, {} ); // $ExpectError
96+
}

lib/node_modules/@stdlib/stats/ranks/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"lib": "./lib",
2121
"test": "./test"
2222
},
23+
"types": "./docs/types",
2324
"scripts": {},
2425
"homepage": "https://github.com/stdlib-js/stdlib",
2526
"repository": {

0 commit comments

Comments
 (0)