Skip to content

Commit 12c5f89

Browse files
committed
Add Typescript definition
1 parent da95b17 commit 12c5f89

File tree

3 files changed

+234
-0
lines changed

3 files changed

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

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