diff options
| author | Shipwreckt <me@shipwreckt.co.uk> | 2025-10-31 20:02:14 +0000 |
|---|---|---|
| committer | Shipwreckt <me@shipwreckt.co.uk> | 2025-10-31 20:02:14 +0000 |
| commit | 7a52ddeba2a68388b544f529d2d92104420f77b0 (patch) | |
| tree | 15ddd47457a2cb4a96060747437d36474e4f6b4e /node_modules/@11ty/eleventy/src/Engines/Util/ContextAugmenter.js | |
| parent | 53d6ae2b5568437afa5e4995580a3fb679b7b91b (diff) | |
Changed from static to 11ty!
Diffstat (limited to 'node_modules/@11ty/eleventy/src/Engines/Util/ContextAugmenter.js')
| -rw-r--r-- | node_modules/@11ty/eleventy/src/Engines/Util/ContextAugmenter.js | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/node_modules/@11ty/eleventy/src/Engines/Util/ContextAugmenter.js b/node_modules/@11ty/eleventy/src/Engines/Util/ContextAugmenter.js new file mode 100644 index 0000000..dd5fbc6 --- /dev/null +++ b/node_modules/@11ty/eleventy/src/Engines/Util/ContextAugmenter.js @@ -0,0 +1,67 @@ +const DATA_KEYS = ["page", "eleventy"]; + +function augmentFunction(fn, options = {}) { + let t = typeof fn; + if (t !== "function") { + throw new Error( + "Invalid type passed to `augmentFunction`. A function was expected and received: " + t, + ); + } + + /** @this {object} */ + return function (...args) { + let context = augmentObject(this || {}, options); + return fn.call(context, ...args); + }; +} + +function augmentObject(targetObject, options = {}) { + options = Object.assign( + { + source: undefined, // where to copy from + overwrite: true, + lazy: false, // lazily fetch the property + // getter: function() {}, + }, + options, + ); + + for (let key of DATA_KEYS) { + // Skip if overwrite: false and prop already exists on target + if (!options.overwrite && targetObject[key]) { + continue; + } + + if (options.lazy) { + let value; + if (typeof options.getter == "function") { + value = () => options.getter(key, options.source); + } else { + value = () => options.source?.[key]; + } + + // lazy getter important for Liquid strictVariables support + Object.defineProperty(targetObject, key, { + writable: true, + configurable: true, + enumerable: true, + value, + }); + } else { + let value; + if (typeof options.getter == "function") { + value = options.getter(key, options.source); + } else { + value = options.source?.[key]; + } + + if (value) { + targetObject[key] = value; + } + } + } + + return targetObject; +} + +export { DATA_KEYS as augmentKeys, augmentFunction, augmentObject }; |
