-
Notifications
You must be signed in to change notification settings - Fork 1
/
AllowOneFunctionCall.ts
31 lines (25 loc) · 1019 Bytes
/
AllowOneFunctionCall.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Given a function fn, return a new function that is identical to the original function except that it ensures fn is called at most once.
// The first time the returned function is called, it should return the same result as fn.
// Every subsequent time it is called, it should return undefined.
type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
type OnceFn = (...args: JSONValue[]) => JSONValue | undefined
function once(fn: Function): OnceFn {
let called = false;
return function (...args) {
if (called){
return undefined;
};
called = true;
return fn(...args);
};
}
// Test cases
var fn = (a: number, b: number, c: number) => (a + b + c);
const onceFn = once(fn);
console.log(onceFn(1, 2, 3));
console.log(onceFn(2, 3, 6));
fn = (a: number, b: number, c: number) => (a * b * c);
const onceFn2 = once(fn);
console.log(onceFn2(5, 7, 4));
console.log(onceFn2(2, 3, 6));
console.log(onceFn2(4, 6, 8));