Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions solution/2600-2699/2666.Allow One Function Call/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ onceFn(4, 6, 8); // undefined, fn 没有被调用
#### TypeScript

```ts
function once<T extends (...args: any[]) => any>(
fn: T,
): (...args: Parameters<T>) => ReturnType<T> | 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) {
Expand All @@ -91,6 +92,32 @@ function once<T extends (...args: any[]) => 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
*/
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
33 changes: 30 additions & 3 deletions solution/2600-2699/2666.Allow One Function Call/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ onceFn(4, 6, 8); // undefined, fn was not called
#### TypeScript

```ts
function once<T extends (...args: any[]) => any>(
fn: T,
): (...args: Parameters<T>) => ReturnType<T> | 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) {
Expand All @@ -89,6 +90,32 @@ function once<T extends (...args: any[]) => 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
*/
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
21 changes: 21 additions & 0 deletions solution/2600-2699/2666.Allow One Function Call/Solution.js
Original file line number Diff line number Diff line change
@@ -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
*/
7 changes: 4 additions & 3 deletions solution/2600-2699/2666.Allow One Function Call/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function once<T extends (...args: any[]) => any>(
fn: T,
): (...args: Parameters<T>) => ReturnType<T> | 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) {
Expand Down