Skip to content

Commit

Permalink
Add Typescript definition
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeshifter committed Sep 14, 2019
1 parent 6bf114c commit 026ee8b
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
104 changes: 104 additions & 0 deletions lib/node_modules/@stdlib/utils/map-values/docs/types/index.d.ts
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.

Copy link
@kgryte

kgryte Sep 19, 2019

Member

Here and below:

Returns an object value.

This comment has been minimized.

Copy link
@Planeshifter

Planeshifter Sep 20, 2019

Author Member

Changed.

*
* @returns new value
*/
type Nullary = () => string;

This comment has been minimized.

Copy link
@kgryte

kgryte Sep 19, 2019

Member

I believe the return value should be any, not string.

This comment has been minimized.

Copy link
@Planeshifter

Planeshifter Sep 20, 2019

Author Member

Thanks for spotting this! Somehow forgot to update the type annotations; fixed.


/**
* 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 lib/node_modules/@stdlib/utils/map-values/docs/types/test.ts
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
}
1 change: 1 addition & 0 deletions lib/node_modules/@stdlib/utils/map-values/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"lib": "./lib",
"test": "./test"
},
"types": "./docs/types",
"scripts": {},
"homepage": "https://github.com/stdlib-js/stdlib",
"repository": {
Expand Down

0 comments on commit 026ee8b

Please sign in to comment.