Skip to content

Commit a2649af

Browse files
committed
Add Typescript definition
1 parent 1e852f6 commit a2649af

File tree

3 files changed

+206
-0
lines changed

3 files changed

+206
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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+
interface Summary {
24+
/**
25+
* Window size.
26+
*/
27+
window: number;
28+
29+
/**
30+
* Maximum value.
31+
*/
32+
max: number;
33+
34+
/**
35+
* Minimum value.
36+
*/
37+
min: number;
38+
39+
/**
40+
* Range.
41+
*/
42+
range: number;
43+
44+
/**
45+
* Mid-range.
46+
*/
47+
midrange: number;
48+
49+
/**
50+
* Sum.
51+
*/
52+
sum: number;
53+
54+
/**
55+
* Arithmetic mean.
56+
*/
57+
mean: number;
58+
59+
/**
60+
* Unbiased sample variance.
61+
*/
62+
variance: number;
63+
64+
/**
65+
* Corrected sample standard deviation.
66+
*/
67+
stdev: number;
68+
69+
/**
70+
* Corrected sample skewness.
71+
*/
72+
skewness: number;
73+
74+
/**
75+
* Corrected sample excess kurtosis.
76+
*/
77+
kurtosis: number;
78+
}
79+
80+
81+
/**
82+
* If provided a value, the accumulator function returns an updated summary; otherwise, the accumulator function returns the current summary.
83+
*
84+
* ## Notes
85+
*
86+
* - The returned summary is an object containing the following fields:
87+
*
88+
* - window: window size.
89+
* - max: maximum value.
90+
* - min: minimum value.
91+
* - range: range.
92+
* - midrange: mid-range.
93+
* - sum: sum.
94+
* - mean: arithmetic mean.
95+
* - variance: unbiased sample variance.
96+
* - stdev: corrected sample standard deviation.
97+
* - skewness: corrected sample skewness.
98+
* - kurtosis: corrected sample excess kurtosis.
99+
*
100+
*
101+
* @param x - value
102+
* @returns updated summary
103+
*/
104+
type accumulator = ( x?: number ) => Summary | null;
105+
106+
/**
107+
* Returns an accumulator function which incrementally computes a moving statistical summary.
108+
*
109+
* ## Notes
110+
*
111+
* - The `W` parameter defines the number of values over which to compute the moving statistical summary.
112+
* - If provided a value, the accumulator function returns an updated moving statistical summary. If not provided a value, the accumulator function returns the current moving statistical summary.
113+
* - As `W` values are needed to fill the window buffer, the first `W-1` returned summaries are calculated from smaller sample sizes. Until the window is full, each returned summary is calculated from all provided values.
114+
*
115+
* @param W - window size
116+
* @throws must provide a positive integer
117+
* @returns accumulator function
118+
*
119+
* @example
120+
* var accumulator = incrmsummary( 3 );
121+
*
122+
* var summary = accumulator();
123+
* // returns {}
124+
*
125+
* summary = accumulator( 2.0 );
126+
* // returns {...}
127+
*
128+
* summary = accumulator( -5.0 );
129+
* // returns {...}
130+
*
131+
* summary = accumulator();
132+
* // returns {...}
133+
*/
134+
declare function incrmsummary( W: number ): accumulator;
135+
136+
137+
// EXPORTS //
138+
139+
export = incrmsummary;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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 incrmsummary = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an accumulator function...
25+
{
26+
incrmsummary( 3 ); // $ExpectType accumulator
27+
}
28+
29+
// The compiler throws an error if the function is provided an argument which is not a number...
30+
{
31+
incrmsummary( '5' ); // $ExpectError
32+
incrmsummary( true ); // $ExpectError
33+
incrmsummary( false ); // $ExpectError
34+
incrmsummary( null ); // $ExpectError
35+
incrmsummary( undefined ); // $ExpectError
36+
incrmsummary( [] ); // $ExpectError
37+
incrmsummary( {} ); // $ExpectError
38+
incrmsummary( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided an invalid number of arguments...
42+
{
43+
incrmsummary(); // $ExpectError
44+
incrmsummary( 3, 2 ); // $ExpectError
45+
}
46+
47+
// The function returns an accumulator function which returns a summary object...
48+
{
49+
const acc = incrmsummary( 3 );
50+
51+
acc(); // $ExpectType Summary | null
52+
acc( 3.14 ); // $ExpectType Summary | null
53+
}
54+
55+
// The compiler throws an error if the returned accumulator function is provided invalid arguments...
56+
{
57+
const acc = incrmsummary( 3 );
58+
59+
acc( '5' ); // $ExpectError
60+
acc( true ); // $ExpectError
61+
acc( false ); // $ExpectError
62+
acc( null ); // $ExpectError
63+
acc( [] ); // $ExpectError
64+
acc( {} ); // $ExpectError
65+
acc( ( x: number ): number => x ); // $ExpectError
66+
}

lib/node_modules/@stdlib/stats/incr/msummary/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)