Skip to content

Commit 490805f

Browse files
committed
Add Typescript definition
1 parent 12c5f89 commit 490805f

File tree

3 files changed

+283
-0
lines changed

3 files changed

+283
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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+
type Returns = 'values' | 'indices' | '*';
22+
23+
/**
24+
* Interface defining function options.
25+
*/
26+
interface Options {
27+
/**
28+
* Execution context.
29+
*/
30+
thisArg?: any;
31+
32+
/**
33+
* If `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned.
34+
*/
35+
returns?: Returns;
36+
}
37+
38+
/**
39+
* Returns a boolean indicating which group an object's own and inherited property values belongs to.
40+
*
41+
* @returns boolean indicating whether a property value should be placed in the first or second group
42+
*/
43+
type Nullary = () => boolean;
44+
45+
/**
46+
* Returns a boolean indicating which group an object's own and inherited property values belongs to.
47+
*
48+
* @param value - object value
49+
* @returns boolean indicating whether a property value should be placed in the first or second group
50+
*/
51+
type Unary = ( value: any ) => boolean;
52+
53+
/**
54+
* Returns a boolean indicating which group an object's own and inherited property values belongs to.
55+
*
56+
* @param value - object value
57+
* @param key - object key
58+
* @returns boolean indicating whether a property value should be placed in the first or second group
59+
*/
60+
type Binary = ( value: any, key: string | symbol ) => boolean;
61+
62+
/**
63+
* Returns a boolean indicating which group an object's own and inherited property values belongs to.
64+
*
65+
* @param value - object value
66+
* @param key - object key
67+
* @returns boolean indicating whether a property value should be placed in the first or second group
68+
*/
69+
type Predicate = Nullary | Unary | Binary;
70+
71+
/**
72+
* Splits an object's own property values into two groups according to a predicate function.
73+
*
74+
* @param obj - input object
75+
* @param predicate - predicate function indicating which group an element in the input object belongs to
76+
* @returns group results
77+
*
78+
* @example
79+
* function predicate( v ) {
80+
* return v[ 0 ] === 'b';
81+
* }
82+
* var obj = {
83+
* 'a': 'beep',
84+
* 'b': 'boop',
85+
* 'c': 'foo',
86+
* 'd': 'bar'
87+
* };
88+
* var out = bifurcateOwn( obj, predicate );
89+
* // e.g., returns [ [ 'beep', 'boop', 'bar' ], [ 'foo' ] ]
90+
*
91+
* @example
92+
* function predicate( v ) {
93+
* return v[ 0 ] === 'b';
94+
* }
95+
* var obj = {
96+
* 'a': 'beep',
97+
* 'b': 'boop',
98+
* 'c': 'foo',
99+
* 'd': 'bar'
100+
* };
101+
* var opts = {
102+
* 'returns': 'keys'
103+
* };
104+
* var out = bifurcateOwn( obj, opts, predicate );
105+
* // e.g., returns [ [ 'a', 'b', 'd' ], [ 'c' ] ]
106+
*
107+
* @example
108+
* function predicate( v ) {
109+
* return v[ 0 ] === 'b';
110+
* }
111+
* var obj = {
112+
* 'a': 'beep',
113+
* 'b': 'boop',
114+
* 'c': 'foo',
115+
* 'd': 'bar'
116+
* };
117+
* var opts = {
118+
* 'returns': '*'
119+
* };
120+
* var out = bifurcateOwn( obj, opts, predicate );
121+
* // e.g., returns [ [ [ 'a', 'beep' ], [ 'b', 'boop' ], [ 'd', 'bar' ] ], [ [ 'c', 'foo' ] ] ]
122+
*/
123+
declare function bifurcateOwn( obj: any, predicate: Predicate ): Array<Array<any>>; // tslint-disable-line max-line-length
124+
125+
/**
126+
* Splits an object's own property values into two groups according to a predicate function.
127+
*
128+
* @param obj - input object
129+
* @param options - function options
130+
* @param options.thisArg - execution context
131+
* @param options.returns - if `values`, values are returned; if `keys`, keys are returned; if `*`, both keys and values are returned
132+
* @param predicate - predicate function indicating which group an element in the input object belongs to
133+
* @returns group results
134+
*
135+
* @example
136+
* function predicate( v ) {
137+
* return v[ 0 ] === 'b';
138+
* }
139+
* var obj = {
140+
* 'a': 'beep',
141+
* 'b': 'boop',
142+
* 'c': 'foo',
143+
* 'd': 'bar'
144+
* };
145+
* var out = bifurcateOwn( obj, predicate );
146+
* // e.g., returns [ [ 'beep', 'boop', 'bar' ], [ 'foo' ] ]
147+
*
148+
* @example
149+
* function predicate( v ) {
150+
* return v[ 0 ] === 'b';
151+
* }
152+
* var obj = {
153+
* 'a': 'beep',
154+
* 'b': 'boop',
155+
* 'c': 'foo',
156+
* 'd': 'bar'
157+
* };
158+
* var opts = {
159+
* 'returns': 'keys'
160+
* };
161+
* var out = bifurcateOwn( obj, opts, predicate );
162+
* // e.g., returns [ [ 'a', 'b', 'd' ], [ 'c' ] ]
163+
*
164+
* @example
165+
* function predicate( v ) {
166+
* return v[ 0 ] === 'b';
167+
* }
168+
* var obj = {
169+
* 'a': 'beep',
170+
* 'b': 'boop',
171+
* 'c': 'foo',
172+
* 'd': 'bar'
173+
* };
174+
* var opts = {
175+
* 'returns': '*'
176+
* };
177+
* var out = bifurcateOwn( obj, opts, predicate );
178+
* // e.g., returns [ [ [ 'a', 'beep' ], [ 'b', 'boop' ], [ 'd', 'bar' ] ], [ [ 'c', 'foo' ] ] ]
179+
*/
180+
declare function bifurcateOwn( obj: any, options: Options, predicate: Predicate ): Array<Array<any>>; // tslint-disable-line max-line-length
181+
182+
183+
// EXPORTS //
184+
185+
export = bifurcateOwn;
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+
import bifurcateOwn = require( './index' );
20+
21+
const predicate = ( v: Array<any> ): boolean => v[ 0 ] === 'b';
22+
23+
/**
24+
* A class for instantiating objects with `a` and `b` dummy properties.
25+
*/
26+
class Foo {
27+
/**
28+
* String property.
29+
*/
30+
a: string;
31+
32+
/**
33+
* String property.
34+
*/
35+
b: string;
36+
37+
constructor() {
38+
this.a = 'beep';
39+
this.b = 'boop';
40+
}
41+
}
42+
43+
44+
// TESTS //
45+
46+
// The function returns an an array of arrays...
47+
{
48+
const obj = new Foo();
49+
bifurcateOwn( obj, predicate ); // $ExpectType any[][]
50+
bifurcateOwn( {}, predicate ); // $ExpectType any[][]
51+
const opts = {
52+
'returns': 'indices' as 'indices'
53+
};
54+
bifurcateOwn( obj, opts, predicate ); // $ExpectType any[][]
55+
}
56+
57+
// The compiler throws an error if the function is provided a last argument which is not a function...
58+
{
59+
const obj = new Foo();
60+
bifurcateOwn( obj, false ); // $ExpectError
61+
bifurcateOwn( obj, true ); // $ExpectError
62+
bifurcateOwn( obj, 32 ); // $ExpectError
63+
bifurcateOwn( obj, 'abc' ); // $ExpectError
64+
bifurcateOwn( obj, [] ); // $ExpectError
65+
bifurcateOwn( obj, {} ); // $ExpectError
66+
67+
bifurcateOwn( obj, {}, false ); // $ExpectError
68+
bifurcateOwn( obj, {}, true ); // $ExpectError
69+
bifurcateOwn( obj, {}, 32 ); // $ExpectError
70+
bifurcateOwn( obj, {}, 'abc' ); // $ExpectError
71+
bifurcateOwn( obj, {}, [] ); // $ExpectError
72+
bifurcateOwn( obj, {}, {} ); // $ExpectError
73+
}
74+
75+
// The compiler throws an error if the function is provided an options argument which is not an object...
76+
{
77+
const obj = new Foo();
78+
bifurcateOwn( obj, null, predicate ); // $ExpectError
79+
}
80+
81+
// The compiler throws an error if the function is provided a `returns` option which is not one of 'indices', 'values', or '*'...
82+
{
83+
const obj = new Foo();
84+
bifurcateOwn( obj, { 'returns': '5' }, predicate ); // $ExpectError
85+
bifurcateOwn( obj, { 'returns': 123 }, predicate ); // $ExpectError
86+
bifurcateOwn( obj, { 'returns': [] }, predicate ); // $ExpectError
87+
bifurcateOwn( obj, { 'returns': {} }, predicate ); // $ExpectError
88+
bifurcateOwn( obj, { 'returns': ( x: number ): number => x }, predicate ); // $ExpectError
89+
}
90+
91+
// The compiler throws an error if the function is provided an invalid number of arguments...
92+
{
93+
const obj = new Foo();
94+
bifurcateOwn(); // $ExpectError
95+
bifurcateOwn( obj ); // $ExpectError
96+
bifurcateOwn( obj, {}, predicate, 16 ); // $ExpectError
97+
}

lib/node_modules/@stdlib/utils/bifurcate-own/package.json

+1
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)