Created
June 17, 2024 23:39
-
-
Save webbower/9a2842d150e761e8f5dde1784c4c6770 to your computer and use it in GitHub Desktop.
defn() Function wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Define a function with an optional name | |
* | |
* Primarily useful for defining named functions with additional configuration based on method calls on the defined function since | |
* `const fn = (...) => {...}.fnMethod()` is invalid syntax, so you need to wrap the function definition with parens anyway. | |
* | |
* ```js | |
* const concat = defn('concat', (a, b) => a + b).withType([String, String], String); | |
* ``` | |
* | |
* @param {string|Function} nameOrFnDef The function to define with no name or a value that will be stringified to the function name | |
* @param {Function} [fnDef] The function definition if {@link nameOrFnDef} is provided | |
* @returns {Function} A function with a name if both args are provided, or an anonymous function if only 1 arg is provided | |
* @throws {TypeError} Thrown if a function was not provided to define | |
*/ | |
const defn = (nameOrFnDef, fnDef) => { | |
// Process the multiple possible signatures | |
const [name, fn] = fnDef && typeof nameOrFnDef !== 'function' | |
?[String(nameOrFnDef), fnDef] | |
: ['', nameOrFnDef]; | |
// We need a function to define and optionally name | |
if (typeof fn !== 'function') { | |
throw new TypeError('A function was not provided to `defn()` to define'); | |
} | |
// The function already has a `.name` which means it's probably already been defined | |
if (fn.name) { | |
return fn; | |
} | |
return name ? Object.defineProperty(fn, 'name', { value: name, configurable: true }) : fn; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment