Skip to content

Commit d2eec12

Browse files
committed
Add Typescript definition
1 parent 1019dc4 commit d2eec12

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
* If `'values'`, values are returned; if `'indices'`, indices are returned; if `'*'`, both indices and values are returned.
31+
*/
32+
returns?: 'values' | 'indices' | '*';
33+
}
34+
35+
/**
36+
* Groups values as arrays associated with distinct keys.
37+
*
38+
* ## Notes
39+
*
40+
* - If provided an empty collection, the function returns an empty object.
41+
*
42+
* @param collection - collection to group
43+
* @param groups - collection defining which group an element in the input collection belongs to
44+
* @throws first and last arguments must be the same length
45+
* @returns group results
46+
*
47+
* @example
48+
* var arr = [ 'beep', 'boop', 'foo', 'bar' ];
49+
* var groups = [ 'b', 'b', 'f', 'b' ];
50+
*
51+
* var out = group( arr, groups );
52+
* // returns { 'b': [ 'beep', 'boop', 'bar' ], 'f': [ 'foo' ] }
53+
*/
54+
declare function group( collection: Collection, groups: Collection ): any; // tslint-disable-line max-line-length
55+
56+
/**
57+
* Groups values as arrays associated with distinct keys.
58+
*
59+
* ## Notes
60+
*
61+
* - If provided an empty collection, the function returns an empty object.
62+
*
63+
* @param collection - collection to group
64+
* @param options - function options
65+
* @param options.returns - if `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned (default: 'values')
66+
* @param groups - collection defining which group an element in the input collection belongs to
67+
* @throws first and last arguments must be the same length
68+
* @returns group results
69+
*
70+
* @example
71+
* var arr = [ 'beep', 'boop', 'foo', 'bar' ];
72+
* var groups = [ 'b', 'b', 'f', 'b' ];
73+
*
74+
* var opts = {
75+
* 'returns': 'indices'
76+
* };
77+
*
78+
* var out = group( arr, opts, groups );
79+
* // returns { 'b': [ 0, 1, 3 ], 'f': [ 2 ] }
80+
*
81+
* @example
82+
* var arr = [ 'beep', 'boop', 'foo', 'bar' ];
83+
* var groups = [ 'b', 'b', 'f', 'b' ];
84+
*
85+
* var opts = {
86+
* 'returns': '*'
87+
* };
88+
*
89+
* var out = group( arr, opts, groups );
90+
* // returns { 'b': [ [ 0, 'beep' ], [ 1, 'boop' ], [ 3, 'bar' ] ], 'f': [ [ 2, 'foo' ] ] }
91+
*/
92+
declare function group( collection: Collection, options: Options, groups: Collection ): any; // tslint-disable-line max-line-length
93+
94+
95+
// EXPORTS //
96+
97+
export = group;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 group = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an object...
25+
{
26+
group( [ 'beep', 'boop', 'foo', 'bar' ], [ 'b', 'b', 'f', 'b' ] ); // $ExpectType any
27+
const opts = {
28+
'returns': 'indices' as 'indices'
29+
};
30+
group( [ 'beep', 'boop', 'foo', 'bar' ], opts, [ 'b', 'b', 'f', 'b' ] ); // $ExpectType any
31+
}
32+
33+
// The compiler throws an error if the function is provided a first argument which is not a collection...
34+
{
35+
group( true, [ 'b', 'b', 'f', 'b' ] ); // $ExpectError
36+
group( false, [ 'b', 'b', 'f', 'b' ] ); // $ExpectError
37+
group( 5, [ 'b', 'b', 'f', 'b' ] ); // $ExpectError
38+
}
39+
40+
// The compiler throws an error if the function is provided a last argument which is not a collection...
41+
{
42+
const arr = [ 'beep', 'boop', 'foo', 'bar' ];
43+
group( arr, false ); // $ExpectError
44+
group( arr, true ); // $ExpectError
45+
group( arr, 32 ); // $ExpectError
46+
group( arr, {} ); // $ExpectError
47+
48+
group( arr, {}, false ); // $ExpectError
49+
group( arr, {}, true ); // $ExpectError
50+
group( arr, {}, 32 ); // $ExpectError
51+
group( arr, {}, {} ); // $ExpectError
52+
}
53+
54+
// The compiler throws an error if the function is provided an options argument which is not an object...
55+
{
56+
const arr = [ 'beep', 'boop', 'foo', 'bar' ];
57+
const groups = [ 'b', 'b', 'f', 'b' ];
58+
group( arr, null, groups ); // $ExpectError
59+
}
60+
61+
// The compiler throws an error if the function is provided a `returns` option which is not one of 'indices', 'values', or '*'...
62+
{
63+
const arr = [ 'beep', 'boop', 'foo', 'bar' ];
64+
const groups = [ 'b', 'b', 'f', 'b' ];
65+
group( arr, { 'returns': '5' }, groups ); // $ExpectError
66+
group( arr, { 'returns': 123 }, groups ); // $ExpectError
67+
group( arr, { 'returns': [] }, groups ); // $ExpectError
68+
group( arr, { 'returns': {} }, groups ); // $ExpectError
69+
group( arr, { 'returns': ( x: number ): number => x }, groups ); // $ExpectError
70+
}
71+
72+
// The compiler throws an error if the function is provided an invalid number of arguments...
73+
{
74+
const arr = [ 'beep', 'boop', 'foo', 'bar' ];
75+
const groups = [ 'b', 'b', 'f', 'b' ];
76+
group(); // $ExpectError
77+
group( arr ); // $ExpectError
78+
group( arr, {}, groups, 16 ); // $ExpectError
79+
}

lib/node_modules/@stdlib/utils/group/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)