-
-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bf114c
commit 026ee8b
Showing
3 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
lib/node_modules/@stdlib/utils/map-values/docs/types/index.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2019 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// TypeScript Version: 2.0 | ||
|
||
/** | ||
* Returns a value for an object element in the new object. | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
* | ||
* @returns new value | ||
*/ | ||
type Nullary = () => string; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Planeshifter
Author
Member
|
||
|
||
/** | ||
* Returns a value for an object element in the new object. | ||
* | ||
* @param value - object value corresponding to `key` | ||
* @returns new value | ||
*/ | ||
type Unary = ( key: string ) => string; | ||
|
||
/** | ||
* Returns a value for an object element in the new object. | ||
* | ||
* @param value - object value corresponding to `key` | ||
* @param key - object key | ||
* @returns new value | ||
*/ | ||
type Binary = ( key: string, value: any ) => string; | ||
|
||
/** | ||
* Returns a value for an object element in the new object. | ||
* | ||
* @param value - object value corresponding to `key` | ||
* @param key - object key | ||
* @param obj - the input object | ||
* @returns new value | ||
*/ | ||
type Tertiary = ( key: string, value: any, obj: any ) => string; | ||
|
||
/** | ||
* Returns a value for an object element in the new object. | ||
* | ||
* @param value - object value corresponding to `key` | ||
* @param key - object key | ||
* @param obj - the input object | ||
* @returns new value | ||
*/ | ||
type Transform = Nullary | Unary | Binary | Tertiary; | ||
|
||
/** | ||
* Maps values from one object to a new object having the same keys. | ||
* | ||
* ## Notes | ||
* | ||
* - The transform function is provided three arguments: | ||
* | ||
* - `value`: object value corresponding to `key` | ||
* - `key`: object key | ||
* - `obj`: the input object | ||
* | ||
* - The function only maps values from own properties. Hence, the function does not map inherited properties. | ||
* | ||
* - The function shallow copies key values. | ||
* | ||
* - Iteration order is **not** guaranteed. | ||
* | ||
* @param obj - source object | ||
* @param transform - transform function | ||
* @returns new object | ||
* | ||
* @example | ||
* function transform( value, key ) { | ||
* return key + value; | ||
* } | ||
* | ||
* var obj1 = { | ||
* 'a': 1, | ||
* 'b': 2 | ||
* }; | ||
* | ||
* var obj2 = mapValues( obj1, transform ); | ||
* // returns { 'a': 'a1', 'b': 'b2' } | ||
*/ | ||
declare function mapValues( obj: any, transform: Transform ): any; | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = mapValues; |
54 changes: 54 additions & 0 deletions
54
lib/node_modules/@stdlib/utils/map-values/docs/types/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2019 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import mapValues = require( './index' ); | ||
|
||
const transform = ( value: string, key: string ): string => key + value; | ||
|
||
|
||
// TESTS // | ||
|
||
// The function returns an object... | ||
{ | ||
const obj = { | ||
'a': 'beep', | ||
'b': 'boop' | ||
}; | ||
mapValues( obj, ( key: string, value: string ) => key + value ); // $ExpectType any | ||
} | ||
|
||
// The compiler throws an error if the function is provided a last argument which is not a function... | ||
{ | ||
const obj = { | ||
'a': 'beep', | ||
'b': 'boop' | ||
}; | ||
mapValues( obj, false ); // $ExpectError | ||
mapValues( obj, true ); // $ExpectError | ||
mapValues( obj, 32 ); // $ExpectError | ||
mapValues( obj, 'abc' ); // $ExpectError | ||
mapValues( obj, [] ); // $ExpectError | ||
mapValues( obj, {} ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided an invalid number of arguments... | ||
{ | ||
mapValues(); // $ExpectError | ||
mapValues( {} ); // $ExpectError | ||
mapValues( {}, transform, 16 ); // $ExpectError | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Here and below: