Skip to content

Commit e586e24

Browse files
committed
Add Typescript definitions
1 parent 2e7d42a commit e586e24

File tree

6 files changed

+274
-0
lines changed

6 files changed

+274
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
* Converts a collection to an object whose keys are determined by a provided function and whose values are the collection values, iterating from right to left.
27+
*
28+
* ## Notes
29+
*
30+
* - When invoked, the input function is provided two arguments:
31+
*
32+
* - `value`: collection value
33+
* - `index`: collection index
34+
*
35+
* - If more than one element in a collection resolves to the same key, the key value is the collection element which last resolved to the key.
36+
*
37+
* - Object values are shallow copies.
38+
*
39+
* @param collection - input collection
40+
* @param fcn - function to invoke
41+
* @param thisArg - execution context
42+
* @returns output object
43+
*
44+
* @example
45+
* function toKey( value, index ) {
46+
* console.log( '%d: %s', index, JSON.stringify( value ) );
47+
* return value.name;
48+
* }
49+
*
50+
* var collection = [
51+
* { 'name': 'beep', 'a': 1 },
52+
* { 'name': 'boop', 'b': 2 }
53+
* ];
54+
*
55+
* var obj = keyByRight( collection, toKey );
56+
* // returns { 'boop': { 'name': 'boop', 'b': 2 }, 'beep': { 'name': 'beep', 'a': 1 } }
57+
*/
58+
declare function keyByRight( collection: Collection, fcn: Function, thisArg?: any ): any; // tslint-disable-line max-line-length
59+
60+
61+
// EXPORTS //
62+
63+
export = keyByRight;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 keyByRight = require( './index' );
20+
21+
interface Foo {
22+
/**
23+
* String indicating the name.
24+
*/
25+
name: string;
26+
27+
/**
28+
* Number indicating a first value.
29+
*/
30+
a?: number;
31+
32+
/**
33+
* Number indicating a second value.
34+
*/
35+
b?: number;
36+
}
37+
38+
const toKey = ( value: Foo ) => {
39+
return value.name;
40+
};
41+
42+
// TESTS //
43+
44+
// The function returns an object...
45+
{
46+
keyByRight( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], toKey ); // $ExpectType any
47+
keyByRight( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], toKey, {} ); // $ExpectType any
48+
}
49+
50+
// The compiler throws an error if the function is provided a first argument which is not a collection...
51+
{
52+
keyByRight( 2, toKey ); // $ExpectError
53+
keyByRight( false, toKey ); // $ExpectError
54+
keyByRight( true, toKey ); // $ExpectError
55+
keyByRight( {}, toKey ); // $ExpectError
56+
}
57+
58+
// The compiler throws an error if the function is provided a second argument which is not a function...
59+
{
60+
keyByRight( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], 2 ); // $ExpectError
61+
keyByRight( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], false ); // $ExpectError
62+
keyByRight( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], true ); // $ExpectError
63+
keyByRight( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], 'abc' ); // $ExpectError
64+
keyByRight( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], {} ); // $ExpectError
65+
keyByRight( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], [] ); // $ExpectError
66+
}
67+
68+
// The compiler throws an error if the function is provided an invalid number of arguments...
69+
{
70+
keyByRight(); // $ExpectError
71+
keyByRight( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ] ); // $ExpectError
72+
keyByRight( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ]], toKey, {}, 3 ); // $ExpectError
73+
}

lib/node_modules/@stdlib/utils/key-by-right/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": {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
* Converts a collection to an object whose keys are determined by a provided function and whose values are the collection values.
27+
*
28+
* ## Notes
29+
*
30+
* - When invoked, the input function is provided two arguments:
31+
*
32+
* - `value`: collection value
33+
* - `index`: collection index
34+
*
35+
* - If more than one element in a collection resolves to the same key, the key value is the collection element which last resolved to the key.
36+
*
37+
* - Object values are shallow copies.
38+
*
39+
* @param collection - input collection
40+
* @param fcn - function to invoke
41+
* @param thisArg - execution context
42+
* @returns output object
43+
*
44+
* @example
45+
* function toKey( value, index ) {
46+
* console.log( '%d: %s', index, JSON.stringify( value ) );
47+
* return value.name;
48+
* }
49+
*
50+
* var collection = [
51+
* { 'name': 'beep', 'a': 1 },
52+
* { 'name': 'boop', 'b': 2 }
53+
* ];
54+
*
55+
* var obj = keyBy( collection, toKey );
56+
* // returns { 'beep': { 'name': 'beep', 'a': 1 }, 'boop': { 'name': 'boop', 'b': 2 } }
57+
*/
58+
declare function keyBy( collection: Collection, fcn: Function, thisArg?: any ): any; // tslint-disable-line max-line-length
59+
60+
61+
// EXPORTS //
62+
63+
export = keyBy;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 keyBy = require( './index' );
20+
21+
interface Foo {
22+
/**
23+
* String indicating the name.
24+
*/
25+
name: string;
26+
27+
/**
28+
* Number indicating a first value.
29+
*/
30+
a?: number;
31+
32+
/**
33+
* Number indicating a second value.
34+
*/
35+
b?: number;
36+
}
37+
38+
const toKey = ( value: Foo ) => {
39+
return value.name;
40+
};
41+
42+
// TESTS //
43+
44+
// The function returns an object...
45+
{
46+
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], toKey ); // $ExpectType any
47+
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], toKey, {} ); // $ExpectType any
48+
}
49+
50+
// The compiler throws an error if the function is provided a first argument which is not a collection...
51+
{
52+
keyBy( 2, toKey ); // $ExpectError
53+
keyBy( false, toKey ); // $ExpectError
54+
keyBy( true, toKey ); // $ExpectError
55+
keyBy( {}, toKey ); // $ExpectError
56+
}
57+
58+
// The compiler throws an error if the function is provided a second argument which is not a function...
59+
{
60+
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], 2 ); // $ExpectError
61+
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], false ); // $ExpectError
62+
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], true ); // $ExpectError
63+
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], 'abc' ); // $ExpectError
64+
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], {} ); // $ExpectError
65+
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], [] ); // $ExpectError
66+
}
67+
68+
// The compiler throws an error if the function is provided an invalid number of arguments...
69+
{
70+
keyBy(); // $ExpectError
71+
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ] ); // $ExpectError
72+
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ]], toKey, {}, 3 ); // $ExpectError
73+
}

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