📄 generateDocs
Generates documentation for an API map. It analyzes each function in the provided map and extracts a description. This can be used to automatically build your docs site or README.
| Parameter | Type | Default | Description |
|---|---|---|---|
apiMap |
object |
— | An object where keys are function names and values are the corresponding function references. Each function may optionally include a description property or a JSDoc comment. |
string – A formatted string listing each function with its description.
-
Check
descriptionproperty: If a function has adescriptionproperty, that is used first. -
Check JSDoc-style comment: If no
descriptionproperty exists, it tries to extract a JSDoc comment from the function string. -
Fallback: If neither exists, it returns
"No description available."for that function.
1function add(a, b) {2 /** Adds two numbers together and returns the result */3 return a + b;4}6function multiply(a, b) {7 return a * b;8}9multiply.description = "Multiplies two numbers.";11const apiMap = { add, multiply };13console.log(generateDocs(apiMap));
Output:
- `add`: Adds two numbers together and returns the result
- `multiply`: Multiplies two numbers.
- Function without description or JSDoc:
1function noop() {}2const apiMap = { noop };3console.log(generateDocs(apiMap));
Output:
- `noop`: No description available.
- Function with multiline JSDoc:
1function complex(a, b) {2 /**3 * Performs a complex operation.4 * Returns a transformed value.5 */6 return a + b;7}8const apiMap = { complex };9console.log(generateDocs(apiMap));
Output:
- `complex`: Performs a complex operation. Returns a transformed value.
- Function with leading
*in JSDoc:
1function example() {2 /**3 * Example function4 * with multiple lines5 */6 return true;7}8const apiMap = { example };9console.log(generateDocs(apiMap));
Output:
- `example`: Example function with multiple lines
- Non-function values in map – will skip properly because
.toString()works on anything, but best practice is to provide only functions.
- The function works best when the
apiMaponly contains functions. Non-function values may produce unexpected results. - Leading/trailing whitespace in JSDoc is automatically trimmed.
- Multi-line JSDoc comments are flattened into a single line for readability.