Skip to content

Commit 3facf3a

Browse files
committed
Add Typescript definition
1 parent 4538c0b commit 3facf3a

3 files changed

Lines changed: 136 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
/**
22+
* Returns a memoized function.
23+
*
24+
* ## Notes
25+
*
26+
* - The function does not set the `length` property of the returned function. Accordingly, the returned function `length` is always zero.
27+
* - The evaluation context is always `null`.
28+
* - The function serializes provided arguments as a string and stores results using the string as an identifier. To use a custom hash function, provide a hash function argument.
29+
*
30+
* @param fcn - function to memoize
31+
* @param hashFunction - function to map a set of arguments to a single value identifying that set
32+
* @returns memoized function
33+
*
34+
* @example
35+
* function factorial( n ) {
36+
* var prod;
37+
* var i;
38+
* prod = 1;
39+
* for ( i = n; i > 1; i-- ) {
40+
* prod *= i;
41+
* }
42+
* return prod;
43+
* }
44+
*
45+
* var memoized = memoize( factorial );
46+
*
47+
* var v = memoized( 5 );
48+
* // returns 120
49+
*
50+
* v = memoized( 5 );
51+
* // returns 120
52+
*/
53+
declare function memoize( fcn: Function, hashFunction?: Function ): Function;
54+
55+
56+
// EXPORTS //
57+
58+
export = memoize;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 memoize = require( './index' );
20+
21+
/**
22+
* Evaluates the factorial of `n`.
23+
*
24+
* @param n - input value
25+
* @returns factorial
26+
*/
27+
function factorial( n: number ) {
28+
let prod = 1;
29+
for ( let i = n; i > 1; i -= 1 ) {
30+
prod *= i;
31+
}
32+
return prod;
33+
}
34+
35+
/**
36+
* Hash function.
37+
*
38+
* @param arg - argument
39+
* @returns hashed value
40+
*/
41+
function hashFunction( arg: number ): string {
42+
return arg.toString();
43+
}
44+
45+
// TESTS //
46+
47+
// The function returns a function...
48+
{
49+
memoize( factorial ); // $ExpectType Function
50+
memoize( factorial, hashFunction ); // $ExpectType Function
51+
}
52+
53+
// The compiler throws an error if the function is provided a first argument other than a function...
54+
{
55+
memoize( 'abc' ); // $ExpectError
56+
memoize( 5 ); // $ExpectError
57+
memoize( [] ); // $ExpectError
58+
memoize( {} ); // $ExpectError
59+
memoize( true ); // $ExpectError
60+
memoize( false ); // $ExpectError
61+
}
62+
63+
// The compiler throws an error if the function is provided a second argument other than a function...
64+
{
65+
memoize( factorial, 'abc' ); // $ExpectError
66+
memoize( factorial, 5 ); // $ExpectError
67+
memoize( factorial, [] ); // $ExpectError
68+
memoize( factorial, {} ); // $ExpectError
69+
memoize( factorial, true ); // $ExpectError
70+
memoize( factorial, false ); // $ExpectError
71+
}
72+
73+
// The compiler throws an error if the function is provided an invalid number of arguments...
74+
{
75+
memoize(); // $ExpectError
76+
memoize( factorial, hashFunction, false ); // $ExpectError
77+
}

lib/node_modules/@stdlib/utils/memoize/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"lib": "./lib",
2121
"test": "./test"
2222
},
23+
"types": "./docs/types",
2324
"scripts": {},
2425
"homepage": "https://github.com/stdlib-js/stdlib",
2526
"repository": {

0 commit comments

Comments
 (0)