Skip to content

Commit 692be02

Browse files
committed
Add Typescript definition
1 parent e5b5407 commit 692be02

File tree

3 files changed

+236
-0
lines changed

3 files changed

+236
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
import { Collection } from '@stdlib/types/object';
24+
25+
/**
26+
* Interface defining function options.
27+
*/
28+
interface Options {
29+
/**
30+
* Execution context.
31+
*/
32+
thisArg?: any;
33+
34+
/**
35+
* If `'values'`, values are returned; if `'indices'`, indices are returned; if `'*'`, both indices and values are returned.
36+
*/
37+
returns?: 'values' | 'indices' | '*';
38+
}
39+
40+
/**
41+
* Specifies which group an element in the input collection belongs to.
42+
*
43+
* @returns object key
44+
*/
45+
type Nullary = () => string | symbol;
46+
47+
/**
48+
* Specifies which group an element in the input collection belongs to.
49+
*
50+
* @param value - collection value
51+
* @returns object key
52+
*/
53+
type Unary = ( value: any ) => string | symbol;
54+
55+
/**
56+
* Specifies which group an element in the input collection belongs to.
57+
*
58+
* @param value - collection value
59+
* @param index - collection index
60+
* @returns object key
61+
*/
62+
type Binary = ( value: any, index: number ) => string | symbol;
63+
64+
/**
65+
* Specifies which group an element in the input collection belongs to.
66+
*
67+
* @param value - collection value
68+
* @param index - collection index
69+
* @returns object key
70+
*/
71+
type Indicator = Nullary | Unary | Binary;
72+
73+
/**
74+
* Groups values according to an indicator function.
75+
*
76+
* ## Notes
77+
*
78+
* - When invoked, the indicator function is provided two arguments:
79+
*
80+
* - `value`: collection value
81+
* - `index`: collection index
82+
*
83+
* - The value returned by an indicator function should be a value which can be serialized as an object key.
84+
*
85+
* - If provided an empty collection, the function returns an empty object.
86+
*
87+
* @param collection - collection to group
88+
* @param indicator - indicator function specifying which group an element in the input collection belongs to
89+
* @returns group results
90+
*
91+
* @example
92+
* function indicator( v ) {
93+
* return v[ 0 ];
94+
* }
95+
* var arr = [ 'beep', 'boop', 'foo', 'bar' ];
96+
*
97+
* var out = groupBy( arr, indicator );
98+
* // returns { 'b': [ 'beep', 'boop', 'bar' ], 'f': [ 'foo' ] }
99+
*/
100+
declare function groupBy( collection: Collection, indicator: Indicator ): any; // tslint-disable-line max-line-length
101+
102+
/**
103+
* Groups values according to an indicator function.
104+
*
105+
* ## Notes
106+
*
107+
* - When invoked, the indicator function is provided two arguments:
108+
*
109+
* - `value`: collection value
110+
* - `index`: collection index
111+
*
112+
* - The value returned by an indicator function should be a value which can be serialized as an object key.
113+
*
114+
* - If provided an empty collection, the function returns an empty object.
115+
*
116+
* @param collection - collection to group
117+
* @param options - function options
118+
* @param options.thisArg - execution context
119+
* @param options.returns - if `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned (default: 'values')
120+
* @param indicator - indicator function specifying which group an element in the input collection belongs to
121+
* @returns group results
122+
*
123+
* @example
124+
* function indicator( v ) {
125+
* return v[ 0 ];
126+
* }
127+
* var arr = [ 'beep', 'boop', 'foo', 'bar' ];
128+
*
129+
* var opts = {
130+
* 'returns': 'indices'
131+
* };
132+
* var out = groupBy( arr, opts, indicator );
133+
* // returns { 'b': [ 0, 1, 3 ], 'f': [ 2 ] }
134+
*
135+
* @example
136+
* function indicator( v ) {
137+
* return v[ 0 ];
138+
* }
139+
* var arr = [ 'beep', 'boop', 'foo', 'bar' ];
140+
*
141+
* var opts = {
142+
* 'returns': '*'
143+
* };
144+
* var out = groupBy( arr, opts, indicator );
145+
* // returns { 'b': [ [ 0, 'beep' ], [ 1, 'boop' ], [ 3, 'bar' ] ], 'f': [ [ 2, 'foo' ] ] }
146+
*/
147+
declare function groupBy( collection: Collection, options: Options, indicator: Indicator ): any; // tslint-disable-line max-line-length
148+
149+
150+
// EXPORTS //
151+
152+
export = groupBy;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 groupBy = require( './index' );
20+
21+
const indicator = ( v: string ): string => v[ 0 ];
22+
23+
24+
// TESTS //
25+
26+
// The function returns an object...
27+
{
28+
groupBy( [ 'beep', 'boop', 'foo', 'bar' ], indicator ); // $ExpectType any
29+
groupBy( [], indicator ); // $ExpectType any
30+
const opts = {
31+
'returns': 'indices' as 'indices'
32+
};
33+
groupBy( [ 'beep', 'boop', 'foo', 'bar' ], opts, indicator ); // $ExpectType any
34+
}
35+
36+
// The compiler throws an error if the function is provided a first argument which is not a collection...
37+
{
38+
groupBy( true, indicator ); // $ExpectError
39+
groupBy( false, indicator ); // $ExpectError
40+
groupBy( 5, indicator ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if the function is provided a last argument which is not a function...
44+
{
45+
const arr = [ 'beep', 'boop', 'foo', 'bar' ];
46+
groupBy( arr, false ); // $ExpectError
47+
groupBy( arr, true ); // $ExpectError
48+
groupBy( arr, 32 ); // $ExpectError
49+
groupBy( arr, 'abc' ); // $ExpectError
50+
groupBy( arr, [] ); // $ExpectError
51+
groupBy( arr, {} ); // $ExpectError
52+
53+
groupBy( arr, {}, false ); // $ExpectError
54+
groupBy( arr, {}, true ); // $ExpectError
55+
groupBy( arr, {}, 32 ); // $ExpectError
56+
groupBy( arr, {}, 'abc' ); // $ExpectError
57+
groupBy( arr, {}, [] ); // $ExpectError
58+
groupBy( arr, {}, {} ); // $ExpectError
59+
}
60+
61+
// The compiler throws an error if the function is provided an options argument which is not an object...
62+
{
63+
const arr = [ 'beep', 'boop', 'foo', 'bar' ];
64+
groupBy( arr, null, indicator ); // $ExpectError
65+
}
66+
67+
// The compiler throws an error if the function is provided a `returns` option which is not one of 'indices', 'values', or '*'...
68+
{
69+
const arr = [ 'beep', 'boop', 'foo', 'bar' ];
70+
groupBy( arr, { 'returns': '5' }, indicator ); // $ExpectError
71+
groupBy( arr, { 'returns': 123 }, indicator ); // $ExpectError
72+
groupBy( arr, { 'returns': [] }, indicator ); // $ExpectError
73+
groupBy( arr, { 'returns': {} }, indicator ); // $ExpectError
74+
groupBy( arr, { 'returns': ( x: number ): number => x }, indicator ); // $ExpectError
75+
}
76+
77+
// The compiler throws an error if the function is provided an invalid number of arguments...
78+
{
79+
const arr = [ 'beep', 'boop', 'foo', 'bar' ];
80+
groupBy(); // $ExpectError
81+
groupBy( arr ); // $ExpectError
82+
groupBy( arr, {}, indicator, 16 ); // $ExpectError
83+
}

lib/node_modules/@stdlib/utils/group-by/package.json

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

0 commit comments

Comments
 (0)