diff options
Diffstat (limited to 'node_modules/@11ty/eleventy/src/Util/Objects')
6 files changed, 287 insertions, 0 deletions
diff --git a/node_modules/@11ty/eleventy/src/Util/Objects/DeepFreeze.js b/node_modules/@11ty/eleventy/src/Util/Objects/DeepFreeze.js new file mode 100644 index 0000000..88e2847 --- /dev/null +++ b/node_modules/@11ty/eleventy/src/Util/Objects/DeepFreeze.js @@ -0,0 +1,20 @@ +import { isPlainObject } from "@11ty/eleventy-utils"; + +// via https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze + +function DeepFreeze(obj, topLevelExceptions) { + for (let name of Reflect.ownKeys(obj)) { + if ((topLevelExceptions || []).find((key) => key === name)) { + continue; + } + + const value = obj[name]; + if (isPlainObject(value)) { + DeepFreeze(value); + } + } + + return Object.freeze(obj); +} + +export { DeepFreeze }; diff --git a/node_modules/@11ty/eleventy/src/Util/Objects/ObjectFilter.js b/node_modules/@11ty/eleventy/src/Util/Objects/ObjectFilter.js new file mode 100644 index 0000000..9ce8737 --- /dev/null +++ b/node_modules/@11ty/eleventy/src/Util/Objects/ObjectFilter.js @@ -0,0 +1,9 @@ +export default function objectFilter(obj, callback) { + let newObject = {}; + for (let [key, value] of Object.entries(obj || {})) { + if (callback(value, key)) { + newObject[key] = value; + } + } + return newObject; +} diff --git a/node_modules/@11ty/eleventy/src/Util/Objects/ProxyWrap.js b/node_modules/@11ty/eleventy/src/Util/Objects/ProxyWrap.js new file mode 100644 index 0000000..38730fd --- /dev/null +++ b/node_modules/@11ty/eleventy/src/Util/Objects/ProxyWrap.js @@ -0,0 +1,118 @@ +import types from "node:util/types"; +import debugUtil from "debug"; +import { isPlainObject } from "@11ty/eleventy-utils"; + +const debug = debugUtil("Dev:Eleventy:Proxy"); + +function wrapObject(target, fallback) { + if (Object.isFrozen(target)) { + return target; + } + + return new Proxy(target, { + getOwnPropertyDescriptor(target, prop) { + let ret; + + if (Reflect.has(target, prop)) { + ret = Reflect.getOwnPropertyDescriptor(target, prop); + } else if (Reflect.has(fallback, prop)) { + ret = Reflect.getOwnPropertyDescriptor(fallback, prop); + } + + return ret; + }, + has(target, prop) { + if (Reflect.has(target, prop)) { + return true; + } + + return Reflect.has(fallback, prop); + }, + ownKeys(target) { + let s = new Set(); + // The fallback keys need to come first to preserve proper key order + // https://github.com/11ty/eleventy/issues/3849 + if (isPlainObject(fallback)) { + for (let k of Reflect.ownKeys(fallback)) { + s.add(k); + } + } + for (let k of Reflect.ownKeys(target)) { + if (!s.has(k)) { + s.add(k); + } + } + return Array.from(s); + }, + get(target, prop) { + debug("handler:get", prop); + + let value = Reflect.get(target, prop); + + if (Reflect.has(target, prop)) { + // Already proxied + if (types.isProxy(value)) { + return value; + } + + if (isPlainObject(value) && Reflect.has(fallback, prop)) { + if (Object.isFrozen(value)) { + return value; + } + + let ret = wrapObject(value, Reflect.get(fallback, prop)); + debug("handler:get (primary, object)", prop); + return ret; + } + + debug("handler:get (primary)", prop); + return value; + } + + // Does not exist in primary + if ( + (typeof fallback === "object" || typeof fallback === "function") && + Reflect.has(fallback, prop) + ) { + // fallback has prop + let fallbackValue = Reflect.get(fallback, prop); + + if (isPlainObject(fallbackValue)) { + if (Object.isFrozen(fallbackValue)) { + return fallbackValue; + } + + debug("handler:get (fallback, object)", prop); + // set empty object on primary + let emptyObject = {}; + Reflect.set(target, prop, emptyObject); + + return wrapObject(emptyObject, fallbackValue); + } + + debug("handler:get (fallback)", prop); + return fallbackValue; + } + + // primary *and* fallback do _not_ have prop + debug("handler:get (not on primary or fallback)", prop); + + return value; + }, + set(target, prop, value) { + debug("handler:set", prop); + + return Reflect.set(target, prop, value); + }, + }); +} + +function ProxyWrap(target, fallback) { + if (!isPlainObject(target) || !isPlainObject(fallback)) { + throw new Error("ProxyWrap expects objects for both the target and fallback"); + } + + return wrapObject(target, fallback); +} + +export { ProxyWrap }; diff --git a/node_modules/@11ty/eleventy/src/Util/Objects/SampleModule.mjs b/node_modules/@11ty/eleventy/src/Util/Objects/SampleModule.mjs new file mode 100644 index 0000000..ff8b4c5 --- /dev/null +++ b/node_modules/@11ty/eleventy/src/Util/Objects/SampleModule.mjs @@ -0,0 +1 @@ +export default {}; diff --git a/node_modules/@11ty/eleventy/src/Util/Objects/Sortable.js b/node_modules/@11ty/eleventy/src/Util/Objects/Sortable.js new file mode 100644 index 0000000..a23d4c9 --- /dev/null +++ b/node_modules/@11ty/eleventy/src/Util/Objects/Sortable.js @@ -0,0 +1,136 @@ +class Sortable { + constructor() { + this.isSortAscending = true; + this.isSortNumeric = false; + this.items = []; + this._dirty = true; + + this.sortFunctionStringMap = { + "A-Z": "sortFunctionAscending", + "Z-A": "sortFunctionDescending", + "0-9": "sortFunctionNumericAscending", + "9-0": "sortFunctionNumericDescending", + }; + } + + get length() { + return this.items.length; + } + + add(item) { + this._dirty = true; + this.items.push(item); + } + + sort(sortFunction) { + if (!sortFunction) { + sortFunction = this.getSortFunction(); + } else if (typeof sortFunction === "string") { + let key = sortFunction; + let name; + if (key in this.sortFunctionStringMap) { + name = this.sortFunctionStringMap[key]; + } + if (Sortable[name]) { + sortFunction = Sortable[name]; + } else { + throw new Error( + `Invalid String argument for sort(). Received \`${key}\`. Valid values: ${Object.keys( + this.sortFunctionStringMap, + )}`, + ); + } + } + + return this.items.slice().sort(sortFunction); + } + + sortAscending() { + return this.sort(this.getSortFunctionAscending()); + } + + sortDescending() { + return this.sort(this.getSortFunctionDescending()); + } + + setSortDescending(isDescending = true) { + this.isSortAscending = !isDescending; + } + + setSortAscending(isAscending = true) { + this.isSortAscending = isAscending; + } + + setSortNumeric(isNumeric) { + this.isSortNumeric = isNumeric; + } + + /* Sort functions */ + static sortFunctionNumericAscending(a, b) { + return a - b; + } + + static sortFunctionNumericDescending(a, b) { + return b - a; + } + + static sortFunctionAscending(a, b) { + if (a > b) { + return 1; + } else if (a < b) { + return -1; + } + return 0; + } + + static sortFunctionDescending(a, b) { + return Sortable.sortFunctionAscending(b, a); + } + + static sortFunctionAlphabeticAscending(a, b) { + return Sortable.sortFunctionAscending(a, b); + } + + static sortFunctionAlphabeticDescending(a, b) { + return Sortable.sortFunctionAscending(b, a); + } + + static sortFunctionDate(mapA, mapB) { + return Sortable.sortFunctionNumericAscending(mapA.date.getTime(), mapB.date.getTime()); + } + + static sortFunctionDateInputPath(mapA, mapB) { + let sortDate = Sortable.sortFunctionNumericAscending(mapA.date.getTime(), mapB.date.getTime()); + if (sortDate === 0) { + return Sortable.sortFunctionAlphabeticAscending(mapA.inputPath, mapB.inputPath); + } + return sortDate; + } + /* End sort functions */ + + getSortFunction() { + if (this.isSortAscending) { + return this.getSortFunctionAscending(); + } else { + return this.getSortFunctionDescending(); + } + } + + getSortFunctionAscending() { + if (this.isSortNumeric) { + return Sortable.sortFunctionNumericAscending; + } else { + return Sortable.sortFunctionAlphabeticAscending; + } + } + + getSortFunctionDescending() { + if (this.isSortNumeric) { + return Sortable.sortFunctionNumericDescending; + } else { + return Sortable.sortFunctionAlphabeticDescending; + } + } +} + +export default Sortable; diff --git a/node_modules/@11ty/eleventy/src/Util/Objects/Unique.js b/node_modules/@11ty/eleventy/src/Util/Objects/Unique.js new file mode 100644 index 0000000..8570c0c --- /dev/null +++ b/node_modules/@11ty/eleventy/src/Util/Objects/Unique.js @@ -0,0 +1,3 @@ +export default function Unique(arr) { + return Array.from(new Set(arr)); +} |
