diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2023-03-18 19:41:32 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2023-03-18 20:15:21 -0300 |
commit | 4f0d935f1dec0cece23ac661b02486f095b5ee94 (patch) | |
tree | 5ecb22838d56c747c65793b49bac51565b4ce17f /src/util | |
parent | 82f1d4de61b3660e07353f44e6125ced019b18ad (diff) |
data steps: annotate content function names
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/sugar.js | 76 |
1 files changed, 74 insertions, 2 deletions
diff --git a/src/util/sugar.js b/src/util/sugar.js index c60bddb6..ad36d16b 100644 --- a/src/util/sugar.js +++ b/src/util/sugar.js @@ -146,8 +146,9 @@ export function bindOpts(fn, bind) { ]); }; - Object.defineProperty(bound, 'name', { - value: fn.name ? `(options-bound) ${fn.name}` : `(options-bound)`, + annotateFunction(bound, { + name: fn, + trait: 'options-bound', }); for (const [key, descriptor] of Object.entries(Object.getOwnPropertyDescriptors(fn))) { @@ -487,3 +488,74 @@ export function decorateErrorWithIndex(fn) { } }; } + +// Delicious function annotations, such as: +// +// (*bound) soWeAreBackInTheMine +// (data *unfulfilled) generateShrekTwo +// +export function annotateFunction(fn, { + name: nameOrFunction = null, + description: newDescription, + trait: newTrait, +}) { + let name; + + if (typeof nameOrFunction === 'function') { + name = nameOrFunction.name; + } else if (typeof nameOrFunction === 'string') { + name = nameOrFunction; + } + + name ??= fn.name ?? 'anonymous'; + + const match = name.match(/^ *(?<prefix>.*?) *\((?<description>.*)( #(?<trait>.*))?\) *(?<suffix>.*) *$/); + + let prefix, suffix, description, trait; + if (match) { + ({prefix, suffix, description, trait} = match.groups); + } + + prefix ??= ''; + suffix ??= name; + description ??= ''; + trait ??= ''; + + if (newDescription) { + if (description) { + description += '; ' + newDescription; + } else { + description = newDescription; + } + } + + if (newTrait) { + if (trait) { + trait += ' #' + newTrait; + } else { + trait = '#' + newTrait; + } + } + + let parenthesesPart; + + if (description && trait) { + parenthesesPart = `${description} ${trait}`; + } else if (description || trait) { + parenthesesPart = description || trait; + } else { + parenthesesPart = ''; + } + + let finalName; + + if (prefix && parenthesesPart) { + finalName = `${prefix} (${parenthesesPart}) ${suffix}`; + } else if (parenthesesPart) { + finalName = `(${parenthesesPart}) ${suffix}`; + } else { + finalName = suffix; + } + + Object.defineProperty(fn, 'name', {value: finalName}); +} |