From eac4ec0a36abfa2b49f8933c40d04a589b7ca166 Mon Sep 17 00:00:00 2001 From: Abhinandan <93651229+AE-Hertz@users.noreply.github.com> Date: Wed, 31 Jul 2024 12:47:09 +0530 Subject: [PATCH 1/6] Update README_EN.md added Javascript code --- .../2666.Allow One Function Call/README_EN.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/solution/2600-2699/2666.Allow One Function Call/README_EN.md b/solution/2600-2699/2666.Allow One Function Call/README_EN.md index 540b24580e47f..e089516036729 100644 --- a/solution/2600-2699/2666.Allow One Function Call/README_EN.md +++ b/solution/2600-2699/2666.Allow One Function Call/README_EN.md @@ -91,6 +91,31 @@ function once any>( +```js +/** + * @param {Function} fn + * @return {Function} + */ +var once = function(fn) { + let called = false; + return function(...args){ + if(!called){ + called=true; + return fn(...args); + } + return undefined; + } +}; + +/** + * let fn = (a,b,c) => (a + b + c) + * let onceFn = once(fn) + * + * onceFn(1,2,3); // 6 + * onceFn(2,3,6); // returns undefined without calling fn + */ + + From 4b2b5d75d99b623c1caf97543be7285d757952a3 Mon Sep 17 00:00:00 2001 From: AE-Hertz Date: Wed, 31 Jul 2024 07:41:53 +0000 Subject: [PATCH 2/6] style: format code and docs with prettier --- solution/2600-2699/2666.Allow One Function Call/README_EN.md | 1 + 1 file changed, 1 insertion(+) diff --git a/solution/2600-2699/2666.Allow One Function Call/README_EN.md b/solution/2600-2699/2666.Allow One Function Call/README_EN.md index e089516036729..11ea7a55d2194 100644 --- a/solution/2600-2699/2666.Allow One Function Call/README_EN.md +++ b/solution/2600-2699/2666.Allow One Function Call/README_EN.md @@ -119,3 +119,4 @@ var once = function(fn) { +``` From 738cbe843b9cc4f1b92dae19297817157995e0e8 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 31 Jul 2024 19:46:09 +0800 Subject: [PATCH 3/6] Update Solution.ts --- .../2600-2699/2666.Allow One Function Call/Solution.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/solution/2600-2699/2666.Allow One Function Call/Solution.ts b/solution/2600-2699/2666.Allow One Function Call/Solution.ts index 958ae80a932dc..af5db873190d9 100644 --- a/solution/2600-2699/2666.Allow One Function Call/Solution.ts +++ b/solution/2600-2699/2666.Allow One Function Call/Solution.ts @@ -1,6 +1,7 @@ -function once any>( - fn: T, -): (...args: Parameters) => ReturnType | 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) { From 9b4f00b4c41d62f6f252e65a1e99cb8348c50653 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 31 Jul 2024 19:46:53 +0800 Subject: [PATCH 4/6] Update README.md --- .../2666.Allow One Function Call/README.md | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/solution/2600-2699/2666.Allow One Function Call/README.md b/solution/2600-2699/2666.Allow One Function Call/README.md index 4f4a3e6c685e6..8a77a386ed1dd 100644 --- a/solution/2600-2699/2666.Allow One Function Call/README.md +++ b/solution/2600-2699/2666.Allow One Function Call/README.md @@ -70,9 +70,10 @@ onceFn(4, 6, 8); // undefined, fn 没有被调用 #### TypeScript ```ts -function once any>( - fn: T, -): (...args: Parameters) => ReturnType | 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) { @@ -91,6 +92,32 @@ function once any>( */ ``` +#### JavaScript + +```js +/** + * @param {Function} fn + * @return {Function} + */ +var once = function (fn) { + let called = false; + return function (...args) { + if (!called) { + called = true; + return fn(...args); + } + }; +}; + +/** + * let fn = (a,b,c) => (a + b + c) + * let onceFn = once(fn) + * + * onceFn(1,2,3); // 6 + * onceFn(2,3,6); // returns undefined without calling fn + */ +``` + From 84d9acd9b9f0e622e6fc319744c956f9ee7af273 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 31 Jul 2024 19:47:34 +0800 Subject: [PATCH 5/6] Update README_EN.md --- .../2666.Allow One Function Call/README_EN.md | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/solution/2600-2699/2666.Allow One Function Call/README_EN.md b/solution/2600-2699/2666.Allow One Function Call/README_EN.md index 11ea7a55d2194..3e6f209ca4f94 100644 --- a/solution/2600-2699/2666.Allow One Function Call/README_EN.md +++ b/solution/2600-2699/2666.Allow One Function Call/README_EN.md @@ -68,9 +68,10 @@ onceFn(4, 6, 8); // undefined, fn was not called #### TypeScript ```ts -function once any>( - fn: T, -): (...args: Parameters) => ReturnType | 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) { @@ -89,22 +90,21 @@ function once any>( */ ``` - +#### JavaScript ```js /** * @param {Function} fn * @return {Function} */ -var once = function(fn) { +var once = function (fn) { let called = false; - return function(...args){ - if(!called){ - called=true; + return function (...args) { + if (!called) { + called = true; return fn(...args); } - return undefined; - } + }; }; /** @@ -114,9 +114,10 @@ var once = function(fn) { * onceFn(1,2,3); // 6 * onceFn(2,3,6); // returns undefined without calling fn */ +``` + -``` From 803eca61fd2c8747c494761554d3dc8dd1d95e97 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 31 Jul 2024 19:47:59 +0800 Subject: [PATCH 6/6] Create Solution.js --- .../2666.Allow One Function Call/Solution.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 solution/2600-2699/2666.Allow One Function Call/Solution.js diff --git a/solution/2600-2699/2666.Allow One Function Call/Solution.js b/solution/2600-2699/2666.Allow One Function Call/Solution.js new file mode 100644 index 0000000000000..a0f9bec40a3f6 --- /dev/null +++ b/solution/2600-2699/2666.Allow One Function Call/Solution.js @@ -0,0 +1,21 @@ +/** + * @param {Function} fn + * @return {Function} + */ +var once = function (fn) { + let called = false; + return function (...args) { + if (!called) { + called = true; + return fn(...args); + } + }; +}; + +/** + * let fn = (a,b,c) => (a + b + c) + * let onceFn = once(fn) + * + * onceFn(1,2,3); // 6 + * onceFn(2,3,6); // returns undefined without calling fn + */