diff --git a/src/www/src/configs/nav-links.json b/src/www/src/configs/nav-links.json index 7f7636d..c2dfb31 100644 --- a/src/www/src/configs/nav-links.json +++ b/src/www/src/configs/nav-links.json @@ -81,10 +81,6 @@ "label": "Functional", "url": "/docs/functions/functional", "methods": [ - { - "label": "retry", - "url": "/docs/functions/functional/retry" - }, { "label": "callAfter", "url": "/docs/functions/functional/callAfter" @@ -97,6 +93,14 @@ "label": "nTimes", "url": "/docs/functions/functional/nTimes" }, + { + "label": "once", + "url": "/docs/functions/functional/once" + }, + { + "label": "retry", + "url": "/docs/functions/functional/retry" + }, { "label": "sleep", "url": "/docs/functions/functional/sleep" diff --git a/src/www/src/configs/prev-next-button-links.json b/src/www/src/configs/prev-next-button-links.json index 553ce80..82e0463 100644 --- a/src/www/src/configs/prev-next-button-links.json +++ b/src/www/src/configs/prev-next-button-links.json @@ -67,10 +67,6 @@ "label": "zip", "url": "/docs/functions/arrays/zip" }, - { - "label": "retry", - "url": "/docs/functions/functional/retry" - }, { "label": "callAfter", "url": "/docs/functions/functional/callAfter" @@ -83,6 +79,14 @@ "label": "nTimes", "url": "/docs/functions/functional/nTimes" }, + { + "label": "once", + "url": "/docs/functions/functional/once" + }, + { + "label": "retry", + "url": "/docs/functions/functional/retry" + }, { "label": "sleep", "url": "/docs/functions/functional/sleep" diff --git a/src/www/src/configs/registry.json b/src/www/src/configs/registry.json index af6a37b..e088287 100644 --- a/src/www/src/configs/registry.json +++ b/src/www/src/configs/registry.json @@ -143,50 +143,82 @@ } }, { - "name": "retry", + "name": "callAfter", "code": { - "ts": "/**\r\n * Retries the given function a specified number of times with a delay between each retry.\r\n * @param fn The function to retry.\r\n * @param retries The number of times to retry the function. Default is 3.\r\n * @param delay The delay in milliseconds between each retry. Default is 1000ms (1 second).\r\n * @returns A Promise that resolves to the result of the function if it succeeds, or rejects with the last error if all retries fail.\r\n */\r\nconst retry = async (\r\n fn: Function,\r\n retries: number = 3,\r\n delay: number = 1000\r\n): Promise => {\r\n try {\r\n return await fn();\r\n } catch (error) {\r\n if (retries > 0) {\r\n await new Promise((resolve) => setTimeout(resolve, delay));\r\n return retry(fn, retries - 1, delay);\r\n }\r\n throw error;\r\n }\r\n};\r\n\r\nexport default retry;\r\n", - "js": "/**\n * Retries the given function a specified number of times with a delay between each retry.\n * @param fn The function to retry.\n * @param retries The number of times to retry the function. Default is 3.\n * @param delay The delay in milliseconds between each retry. Default is 1000ms (1 second).\n * @returns A Promise that resolves to the result of the function if it succeeds, or rejects with the last error if all retries fail.\n */\nconst retry = async (fn, retries = 3, delay = 1000) => {\n try {\n return await fn();\n }\n catch (error) {\n if (retries > 0) {\n await new Promise((resolve) => setTimeout(resolve, delay));\n return retry(fn, retries - 1, delay);\n }\n throw error;\n }\n};\nexport default retry;\n" + "ts": "/**\r\n * Creates a function that delays the execution of the provided function until it has been called a specified number of times.\r\n *\r\n * @param fn - The function to be called after a certain number of invocations.\r\n * @param count - The number of times the function needs to be called before it is executed.\r\n * @returns A new function that delays the execution of the provided function until it has been called a specified number of times.\r\n */\r\nconst callAfter = (\r\n fn: (...args: S) => T,\r\n count: number\r\n): ((...args: S) => T | undefined) => {\r\n let counter = 0;\r\n return (...args: S): T | undefined => {\r\n if (counter < count) {\r\n counter++;\r\n return undefined;\r\n }\r\n return fn(...args);\r\n };\r\n};\r\n\r\nexport default callAfter;\r\n", + "js": "/**\n * Creates a function that delays the execution of the provided function until it has been called a specified number of times.\n *\n * @param fn - The function to be called after a certain number of invocations.\n * @param count - The number of times the function needs to be called before it is executed.\n * @returns A new function that delays the execution of the provided function until it has been called a specified number of times.\n */\nconst callAfter = (fn, count) => {\n let counter = 0;\n return (...args) => {\n if (counter < count) {\n counter++;\n return undefined;\n }\n return fn(...args);\n };\n};\nexport default callAfter;\n" }, "category": "functional", "type": "functions", "examples": [ - "import retry from \".\";\r\n\r\nconst fn = async () => {\r\n throw new Error(\"failed\");\r\n};\r\n\r\nretry(fn, 2, 1000).catch((error) => {\r\n console.log(error.message);\r\n});\r\n// Expected output: \"failed\" after retrying twice but It will call the function 3 times.\r\n" + "import callAfter from \".\";\r\n\r\nconst fn = (x: number) => x + 1;\r\n\r\nconst callAfterFn = callAfter(fn, 2);\r\n\r\nconst result1 = callAfterFn(1);\r\n// Expected Output: undefined\r\n\r\nconst result2 = callAfterFn(2);\r\n// Expected Output: undefined\r\n\r\nconst result3 = callAfterFn(3);\r\n// Expected Output: 4 : as the function `fn` has been called twice already.\r\n" ], "docs": { "metaData": { - "desc": "Retries the given function a specified number of times with a delay between each retry." + "desc": "Returns a new function that can be called only after calling a specific number of times." }, - "md": "\r\nThe retry function retries the given function a specified number of\r\ntimes with a delay between each retry. If the function succeeds within\r\nthe specified number of retries, the Promise will resolve with the\r\nresult of the function. If the function fails on all retries, the\r\nPromise will be rejected with the last error. The retry function is\r\ndesigned to handle both synchronous and asynchronous functions, as it\r\nwraps the call to fn with Promise.resolve. This ensures that fn is\r\nalways treated as a Promise, allowing the use of .then and .catch to\r\nhandle the result or any errors.\r\n\r\nThe function retries the given function a specified number of times. The\r\ntotal number of times a function is called is retries + 1.\r\n" + "md": "\r\nThe `callAfter` function is used to create a new function that can be called only after calling a specific number of times. Before the specified number of calls, the function will always return `undefined` without executing the original function.\r\n" }, "props": [ { "title": "function", "required": true, - "propDesc": "The function to retry.", + "propDesc": "The function to be called", "type": "Function" }, { - "title": "retries", - "required": false, - "defaultValue": "3", - "propDesc": "The number of times to retry the function.", + "title": "count", + "required": true, + "propDesc": "The number of times the function needs to be called before it is executed", "type": "number" + } + ], + "createdAt": { + "date": "2024-04-28T01:32:03.898Z", + "packageVersion": "0.1.0" + }, + "lastUpdated": { + "date": "2024-04-28T01:41:39.599Z", + "packageVersion": "0.1.0" + } + }, + { + "name": "callBefore", + "code": { + "ts": "/**\r\n * Calls the provided function `fn` only for the first `count` invocations,\r\n * and returns `undefined` for subsequent invocations.\r\n *\r\n * @template T - The return type of the function `fn`.\r\n * @template S - The argument types of the function `fn`.\r\n * @param {(...args: S) => T} fn - The function to be called.\r\n * @param {number} count - The number of times the function `fn` should be called.\r\n * @returns {(...args: S) => T | undefined} - A function that calls `fn` only for the first `count` invocations.\r\n */\r\nconst callBefore = (\r\n fn: (...args: S) => T,\r\n count: number\r\n): ((...args: S) => T | undefined) => {\r\n let counter = 0;\r\n return (...args: S): T | undefined => {\r\n if (counter < count) {\r\n counter++;\r\n return fn(...args);\r\n }\r\n return undefined;\r\n };\r\n};\r\n\r\nexport default callBefore;\r\n", + "js": "/**\n * Calls the provided function `fn` only for the first `count` invocations,\n * and returns `undefined` for subsequent invocations.\n *\n * @template T - The return type of the function `fn`.\n * @template S - The argument types of the function `fn`.\n * @param {(...args: S) => T} fn - The function to be called.\n * @param {number} count - The number of times the function `fn` should be called.\n * @returns {(...args: S) => T | undefined} - A function that calls `fn` only for the first `count` invocations.\n */\nconst callBefore = (fn, count) => {\n let counter = 0;\n return (...args) => {\n if (counter < count) {\n counter++;\n return fn(...args);\n }\n return undefined;\n };\n};\nexport default callBefore;\n" + }, + "category": "functional", + "type": "functions", + "examples": [ + "import callBefore from \".\";\r\n\r\nconst fn = (x: number) => x + 1;\r\n\r\nconst callBeforeFn = callBefore(fn, 2);\r\n\r\nconst result1 = callBeforeFn(1);\r\n// Expected Output: 2\r\n\r\nconst result2 = callBeforeFn(2);\r\n// Expected Output: 3\r\n\r\nconst result3 = callBeforeFn(3);\r\n// Expected Output: undefined : as the function `fn` has been called twice already.\r\n" + ], + "docs": { + "metaData": { + "desc": "Returns a new function that can be called only for specific number of times." }, + "md": "\r\nThe `callBefore` function is used to create a new function that can be called only for a specific number of times. After the specified number of calls, the function will always return `undefined` without executing the original function.\r\n\r\nThis is useful in some scenarios like `rate limiting` or `trial period` where you want to allow a function to be called only for a specific number of times.\r\n" + }, + "props": [ { - "title": "delay", - "required": false, - "defaultValue": "1000", - "propDesc": "The delay in milliseconds between each retry.", + "title": "function", + "required": true, + "propDesc": "The function to be called", + "type": "Function" + }, + { + "title": "count", + "required": true, + "propDesc": "The number of times the function can be called.", "type": "number" } ], "createdAt": { - "date": "2024-04-17T13:28:44.122Z", + "date": "2024-04-28T01:06:22.893Z", "packageVersion": "0.1.0" }, "lastUpdated": { - "date": "2024-04-26T06:19:26.171Z", + "date": "2024-04-28T01:06:22.894Z", "packageVersion": "0.1.0" } }, @@ -511,61 +543,62 @@ } }, { - "name": "callAfter", + "name": "nTimes", "code": { - "ts": "const callAfter = (\r\n fn: (...args: S) => T,\r\n count: number\r\n): ((...args: S) => T | undefined) => {\r\n let counter = 0;\r\n return (...args: S): T | undefined => {\r\n if (counter < count) {\r\n counter++;\r\n return undefined;\r\n }\r\n return fn(...args);\r\n };\r\n};\r\n\r\nexport default callAfter;\r\n", - "js": "const callAfter = (fn, count) => {\n let counter = 0;\n return (...args) => {\n if (counter < count) {\n counter++;\n return undefined;\n }\n return fn(...args);\n };\n};\nexport default callAfter;\n" + "ts": "/**\r\n * Executes a given function `n` times and returns an array of the results.\r\n *\r\n * @template T - The type of the result returned by the function.\r\n * @param {Function} fn - The function to be executed `n` times.\r\n * @param {number} [n=1] - The number of times the function should be executed. Defaults to 1.\r\n * @returns {T[]} - An array of the results returned by the function.\r\n * @throws {Error} - If `n` is less than 0.\r\n */\r\nconst nTimes = (fn: (i: number) => T, n: number = 1): T[] => {\r\n if (n < 0) {\r\n throw new Error(\"n must be greater than 0\");\r\n }\r\n let result: T[] = [];\r\n for (let i = 0; i < n; i++) {\r\n result.push(fn(i));\r\n }\r\n return result;\r\n};\r\n\r\nexport default nTimes;\r\n", + "js": "/**\n * Executes a given function `n` times and returns an array of the results.\n *\n * @template T - The type of the result returned by the function.\n * @param {Function} fn - The function to be executed `n` times.\n * @param {number} [n=1] - The number of times the function should be executed. Defaults to 1.\n * @returns {T[]} - An array of the results returned by the function.\n * @throws {Error} - If `n` is less than 0.\n */\nconst nTimes = (fn, n = 1) => {\n if (n < 0) {\n throw new Error(\"n must be greater than 0\");\n }\n let result = [];\n for (let i = 0; i < n; i++) {\n result.push(fn(i));\n }\n return result;\n};\nexport default nTimes;\n" }, "category": "functional", "type": "functions", "examples": [ - "import callAfter from \".\";\r\n\r\nconst fn = (x: number) => x + 1;\r\n\r\nconst callAfterFn = callAfter(fn, 2);\r\n\r\nconst result1 = callAfterFn(1);\r\n// Expected Output: undefined\r\n\r\nconst result2 = callAfterFn(2);\r\n// Expected Output: undefined\r\n\r\nconst result3 = callAfterFn(3);\r\n// Expected Output: 4 : as the function `fn` has been called twice already.\r\n" + "import nTimes from \".\";\r\n\r\nconst result = nTimes(() => \"result\", 3);\r\n// Expected: [\"result\", \"result\", \"result\"]\r\n\r\n// You can also use the index of the iteration.\r\nconst result2 = nTimes((i) => i, 3);\r\n// Expected: [0, 1, 2]\r\n" ], "docs": { "metaData": { - "desc": "Returns a new function that can be called only after calling a specific number of times." + "desc": "Calls a function n times and returns an array of the results." }, - "md": "\r\nThe `callAfter` function is used to create a new function that can be called only after calling a specific number of times. Before the specified number of calls, the function will always return `undefined` without executing the original function.\r\n" + "md": "\r\nThe `nTimes` function calls a function `n` times and returns an array of the results. The function is called with the index of the iteration as the first argument.\r\n" }, "props": [ { "title": "function", "required": true, - "propDesc": "The function to be called", + "propDesc": "The function called `n` times. The function receives the current iteration index as an argument.", "type": "Function" }, { - "title": "count", - "required": true, - "propDesc": "The number of times the function can be called.", + "title": "n", + "required": false, + "defaultValue": "1", + "propDesc": "The number of times to call the function.", "type": "number" } ], "createdAt": { - "date": "2024-04-28T01:32:03.898Z", + "date": "2024-04-27T16:24:24.979Z", "packageVersion": "0.1.0" }, "lastUpdated": { - "date": "2024-04-28T01:32:03.900Z", + "date": "2024-04-27T16:24:24.980Z", "packageVersion": "0.1.0" } }, { - "name": "callBefore", + "name": "once", "code": { - "ts": "/**\r\n * Calls the provided function `fn` only for the first `count` invocations,\r\n * and returns `undefined` for subsequent invocations.\r\n *\r\n * @template T - The return type of the function `fn`.\r\n * @template S - The argument types of the function `fn`.\r\n * @param {(...args: S) => T} fn - The function to be called.\r\n * @param {number} count - The number of times the function `fn` should be called.\r\n * @returns {(...args: S) => T | undefined} - A function that calls `fn` only for the first `count` invocations.\r\n */\r\nconst callBefore = (\r\n fn: (...args: S) => T,\r\n count: number\r\n): ((...args: S) => T | undefined) => {\r\n let counter = 0;\r\n return (...args: S): T | undefined => {\r\n if (counter < count) {\r\n counter++;\r\n return fn(...args);\r\n }\r\n return undefined;\r\n };\r\n};\r\n\r\nexport default callBefore;\r\n", - "js": "/**\n * Calls the provided function `fn` only for the first `count` invocations,\n * and returns `undefined` for subsequent invocations.\n *\n * @template T - The return type of the function `fn`.\n * @template S - The argument types of the function `fn`.\n * @param {(...args: S) => T} fn - The function to be called.\n * @param {number} count - The number of times the function `fn` should be called.\n * @returns {(...args: S) => T | undefined} - A function that calls `fn` only for the first `count` invocations.\n */\nconst callBefore = (fn, count) => {\n let counter = 0;\n return (...args) => {\n if (counter < count) {\n counter++;\n return fn(...args);\n }\n return undefined;\n };\n};\nexport default callBefore;\n" + "ts": "/**\r\n * Creates a function that can only be called once. Subsequent calls to the function will return undefined.\r\n *\r\n * @param fn - The function to be called once.\r\n * @returns A new function that can only be called once.\r\n */\r\nconst once = (\r\n fn: (...args: S) => T\r\n): ((...args: S) => T | undefined) => {\r\n let isCalled = false;\r\n return (...args: S): T | undefined => {\r\n if (!isCalled) {\r\n isCalled = true;\r\n return fn(...args);\r\n }\r\n return undefined;\r\n };\r\n};\r\n\r\nexport default once;\r\n", + "js": "/**\n * Creates a function that can only be called once. Subsequent calls to the function will return undefined.\n *\n * @param fn - The function to be called once.\n * @returns A new function that can only be called once.\n */\nconst once = (fn) => {\n let isCalled = false;\n return (...args) => {\n if (!isCalled) {\n isCalled = true;\n return fn(...args);\n }\n return undefined;\n };\n};\nexport default once;\n" }, "category": "functional", "type": "functions", "examples": [ - "import callBefore from \".\";\r\n\r\nconst fn = (x: number) => x + 1;\r\n\r\nconst callBeforeFn = callBefore(fn, 2);\r\n\r\nconst result1 = callBeforeFn(1);\r\n// Expected Output: 2\r\n\r\nconst result2 = callBeforeFn(2);\r\n// Expected Output: 3\r\n\r\nconst result3 = callBeforeFn(3);\r\n// Expected Output: undefined : as the function `fn` has been called twice already.\r\n" + "import once from \".\";\r\n\r\nconst subscribe = once(() => {\r\n console.log(\"Subscribed\");\r\n});\r\n\r\nconst result = subscribe();\r\n// Expected Output: Subscribed\r\n\r\nconst result2 = subscribe();\r\n// Expected Output: undefined : as the function has been called once already.\r\n" ], "docs": { "metaData": { - "desc": "Returns a new function that can be called only for specific number of times." + "desc": "Returns a new function that can be called only once." }, - "md": "\r\nThe `callBefore` function is used to create a new function that can be called only for a specific number of times. After the specified number of calls, the function will always return `undefined` without executing the original function.\r\n\r\nThis is useful in some scenarios like `rate limiting` or `trial period` where you want to allow a function to be called only for a specific number of times.\r\n" + "md": "\r\nThe `once` function is used to create a new function that can be called only once. After the first call, the function will always return undefined without executing the original function.\r\n\r\nThis is useful when you want to ensure that a function is called only once, regardless of how many times it is called. For example, a subscribe button on a website should only be clicked once, and the function should not be executed again if the button is clicked multiple times.\r\n" }, "props": [ { @@ -573,61 +606,62 @@ "required": true, "propDesc": "The function to be called", "type": "Function" - }, - { - "title": "count", - "required": true, - "propDesc": "The number of times the function can be called.", - "type": "number" } ], "createdAt": { - "date": "2024-04-28T01:06:22.893Z", + "date": "2024-04-28T01:41:39.718Z", "packageVersion": "0.1.0" }, "lastUpdated": { - "date": "2024-04-28T01:06:22.894Z", + "date": "2024-04-28T01:41:39.718Z", "packageVersion": "0.1.0" } }, { - "name": "nTimes", + "name": "retry", "code": { - "ts": "/**\r\n * Executes a given function `n` times and returns an array of the results.\r\n *\r\n * @template T - The type of the result returned by the function.\r\n * @param {Function} fn - The function to be executed `n` times.\r\n * @param {number} [n=1] - The number of times the function should be executed. Defaults to 1.\r\n * @returns {T[]} - An array of the results returned by the function.\r\n * @throws {Error} - If `n` is less than 0.\r\n */\r\nconst nTimes = (fn: (i: number) => T, n: number = 1): T[] => {\r\n if (n < 0) {\r\n throw new Error(\"n must be greater than 0\");\r\n }\r\n let result: T[] = [];\r\n for (let i = 0; i < n; i++) {\r\n result.push(fn(i));\r\n }\r\n return result;\r\n};\r\n\r\nexport default nTimes;\r\n", - "js": "/**\n * Executes a given function `n` times and returns an array of the results.\n *\n * @template T - The type of the result returned by the function.\n * @param {Function} fn - The function to be executed `n` times.\n * @param {number} [n=1] - The number of times the function should be executed. Defaults to 1.\n * @returns {T[]} - An array of the results returned by the function.\n * @throws {Error} - If `n` is less than 0.\n */\nconst nTimes = (fn, n = 1) => {\n if (n < 0) {\n throw new Error(\"n must be greater than 0\");\n }\n let result = [];\n for (let i = 0; i < n; i++) {\n result.push(fn(i));\n }\n return result;\n};\nexport default nTimes;\n" + "ts": "/**\r\n * Retries the given function a specified number of times with a delay between each retry.\r\n * @param fn The function to retry.\r\n * @param retries The number of times to retry the function. Default is 3.\r\n * @param delay The delay in milliseconds between each retry. Default is 1000ms (1 second).\r\n * @returns A Promise that resolves to the result of the function if it succeeds, or rejects with the last error if all retries fail.\r\n */\r\nconst retry = async (\r\n fn: Function,\r\n retries: number = 3,\r\n delay: number = 1000\r\n): Promise => {\r\n try {\r\n return await fn();\r\n } catch (error) {\r\n if (retries > 0) {\r\n await new Promise((resolve) => setTimeout(resolve, delay));\r\n return retry(fn, retries - 1, delay);\r\n }\r\n throw error;\r\n }\r\n};\r\n\r\nexport default retry;\r\n", + "js": "/**\n * Retries the given function a specified number of times with a delay between each retry.\n * @param fn The function to retry.\n * @param retries The number of times to retry the function. Default is 3.\n * @param delay The delay in milliseconds between each retry. Default is 1000ms (1 second).\n * @returns A Promise that resolves to the result of the function if it succeeds, or rejects with the last error if all retries fail.\n */\nconst retry = async (fn, retries = 3, delay = 1000) => {\n try {\n return await fn();\n }\n catch (error) {\n if (retries > 0) {\n await new Promise((resolve) => setTimeout(resolve, delay));\n return retry(fn, retries - 1, delay);\n }\n throw error;\n }\n};\nexport default retry;\n" }, "category": "functional", "type": "functions", "examples": [ - "import nTimes from \".\";\r\n\r\nconst result = nTimes(() => \"result\", 3);\r\n// Expected: [\"result\", \"result\", \"result\"]\r\n\r\n// You can also use the index of the iteration.\r\nconst result2 = nTimes((i) => i, 3);\r\n// Expected: [0, 1, 2]\r\n" + "import retry from \".\";\r\n\r\nconst fn = async () => {\r\n throw new Error(\"failed\");\r\n};\r\n\r\nretry(fn, 2, 1000).catch((error) => {\r\n console.log(error.message);\r\n});\r\n// Expected output: \"failed\" after retrying twice but It will call the function 3 times.\r\n" ], "docs": { "metaData": { - "desc": "Calls a function n times and returns an array of the results." + "desc": "Retries the given function a specified number of times with a delay between each retry." }, - "md": "\r\nThe `nTimes` function calls a function `n` times and returns an array of the results. The function is called with the index of the iteration as the first argument.\r\n" + "md": "\r\nThe retry function retries the given function a specified number of\r\ntimes with a delay between each retry. If the function succeeds within\r\nthe specified number of retries, the Promise will resolve with the\r\nresult of the function. If the function fails on all retries, the\r\nPromise will be rejected with the last error. The retry function is\r\ndesigned to handle both synchronous and asynchronous functions, as it\r\nwraps the call to fn with Promise.resolve. This ensures that fn is\r\nalways treated as a Promise, allowing the use of .then and .catch to\r\nhandle the result or any errors.\r\n\r\nThe function retries the given function a specified number of times. The\r\ntotal number of times a function is called is retries + 1.\r\n" }, "props": [ { "title": "function", "required": true, - "propDesc": "The function called `n` times. The function receives the current iteration index as an argument.", + "propDesc": "The function to retry.", "type": "Function" }, { - "title": "n", + "title": "retries", "required": false, - "defaultValue": "1", - "propDesc": "The number of times to call the function.", + "defaultValue": "3", + "propDesc": "The number of times to retry the function.", + "type": "number" + }, + { + "title": "delay", + "required": false, + "defaultValue": "1000", + "propDesc": "The delay in milliseconds between each retry.", "type": "number" } ], "createdAt": { - "date": "2024-04-27T16:24:24.979Z", + "date": "2024-04-17T13:28:44.122Z", "packageVersion": "0.1.0" }, "lastUpdated": { - "date": "2024-04-27T16:24:24.980Z", + "date": "2024-04-26T06:19:26.171Z", "packageVersion": "0.1.0" } }, diff --git a/src/www/src/registry/functions/functional/callAfter/index.ts b/src/www/src/registry/functions/functional/callAfter/index.ts index 369a5c9..1c1e56a 100644 --- a/src/www/src/registry/functions/functional/callAfter/index.ts +++ b/src/www/src/registry/functions/functional/callAfter/index.ts @@ -1,3 +1,10 @@ +/** + * Creates a function that delays the execution of the provided function until it has been called a specified number of times. + * + * @param fn - The function to be called after a certain number of invocations. + * @param count - The number of times the function needs to be called before it is executed. + * @returns A new function that delays the execution of the provided function until it has been called a specified number of times. + */ const callAfter = ( fn: (...args: S) => T, count: number diff --git a/src/www/src/registry/functions/functional/callAfter/props.ts b/src/www/src/registry/functions/functional/callAfter/props.ts index b991c16..4f64b74 100644 --- a/src/www/src/registry/functions/functional/callAfter/props.ts +++ b/src/www/src/registry/functions/functional/callAfter/props.ts @@ -12,7 +12,7 @@ const Props: IRegistryFunctionPropTable[] = [ title: "count", required: true, defaultValue: undefined, - propDesc: "The number of times the function can be called.", + propDesc: "The number of times the function needs to be called before it is executed", type: "number", }, ]; diff --git a/src/www/src/registry/functions/functional/once/docs.md b/src/www/src/registry/functions/functional/once/docs.md new file mode 100644 index 0000000..25ad107 --- /dev/null +++ b/src/www/src/registry/functions/functional/once/docs.md @@ -0,0 +1,7 @@ +--- +desc: Returns a new function that can be called only once. +--- + +The `once` function is used to create a new function that can be called only once. After the first call, the function will always return undefined without executing the original function. + +This is useful when you want to ensure that a function is called only once, regardless of how many times it is called. For example, a subscribe button on a website should only be clicked once, and the function should not be executed again if the button is clicked multiple times. diff --git a/src/www/src/registry/functions/functional/once/index.test.ts b/src/www/src/registry/functions/functional/once/index.test.ts new file mode 100644 index 0000000..b9ebf13 --- /dev/null +++ b/src/www/src/registry/functions/functional/once/index.test.ts @@ -0,0 +1,26 @@ +import { describe, expect, it, vi } from "vitest"; +import once from "."; + +describe("once", () => { + it("calls the provided function only once", () => { + const fn = vi.fn((x: number) => x + 1); + const onceFn = once(() => fn(1)); + const result1 = onceFn(); + const result2 = onceFn(); + const result3 = onceFn(); + expect(result1).toBe(2); + expect(result2).toBe(undefined); + expect(result3).toBe(undefined); + }); + + it("should also accept functions with arguments", () => { + const fn = vi.fn((x: number) => x + 1); + const onceFn = once(fn); + const result1 = onceFn(1); + const result2 = onceFn(2); + const result3 = onceFn(3); + expect(result1).toBe(2); + expect(result2).toBe(undefined); + expect(result3).toBe(undefined); + }); +}); diff --git a/src/www/src/registry/functions/functional/once/index.ts b/src/www/src/registry/functions/functional/once/index.ts new file mode 100644 index 0000000..49cfdc3 --- /dev/null +++ b/src/www/src/registry/functions/functional/once/index.ts @@ -0,0 +1,20 @@ +/** + * Creates a function that can only be called once. Subsequent calls to the function will return undefined. + * + * @param fn - The function to be called once. + * @returns A new function that can only be called once. + */ +const once = ( + fn: (...args: S) => T +): ((...args: S) => T | undefined) => { + let isCalled = false; + return (...args: S): T | undefined => { + if (!isCalled) { + isCalled = true; + return fn(...args); + } + return undefined; + }; +}; + +export default once; diff --git a/src/www/src/registry/functions/functional/once/once.example.ts b/src/www/src/registry/functions/functional/once/once.example.ts new file mode 100644 index 0000000..d87d37c --- /dev/null +++ b/src/www/src/registry/functions/functional/once/once.example.ts @@ -0,0 +1,11 @@ +import once from "."; + +const subscribe = once(() => { + console.log("Subscribed"); +}); + +const result = subscribe(); +// Expected Output: Subscribed + +const result2 = subscribe(); +// Expected Output: undefined : as the function has been called once already. diff --git a/src/www/src/registry/functions/functional/once/props.ts b/src/www/src/registry/functions/functional/once/props.ts new file mode 100644 index 0000000..f18f679 --- /dev/null +++ b/src/www/src/registry/functions/functional/once/props.ts @@ -0,0 +1,13 @@ +import { IRegistryFunctionPropTable } from "@/types/registry.types"; + +const Props: IRegistryFunctionPropTable[] = [ + { + title: "function", + required: true, + defaultValue: undefined, + propDesc: "The function to be called", + type: "Function", + }, +]; + +export default Props;