UNPKG

@mdfriday/foundry

Version:

The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.

89 lines 2.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Scratch = void 0; /** * Scratch is a writable context used for stateful operations in Page/Node rendering. * TypeScript equivalent of Go's maps.Scratch */ class Scratch { constructor() { this.values = new Map(); } /** * Add will, for single values, add (using the + operator) the addend to the existing addend (if found). * Supports numeric values and strings. * If the first add for a key is an array or slice, then the next value(s) will be appended. */ add(key, newAddend) { const existingAddend = this.values.get(key); let newVal; if (existingAddend !== undefined) { if (Array.isArray(existingAddend)) { newVal = [...existingAddend, newAddend]; } else if (typeof existingAddend === 'number' && typeof newAddend === 'number') { newVal = existingAddend + newAddend; } else if (typeof existingAddend === 'string' && typeof newAddend === 'string') { newVal = existingAddend + newAddend; } else { newVal = newAddend; } } else { newVal = newAddend; } this.values.set(key, newVal); return ''; // Return empty string to match Go's behavior } /** * Set stores a value with the given key in the Node context. * This value can later be retrieved with Get. */ set(key, value) { this.values.set(key, value); return ''; // Return empty string to match Go's behavior } Set(key, value) { // Alias for set to match Go's method naming return this.set(key, value); } /** * Delete deletes the given key. */ delete(key) { this.values.delete(key); return ''; // Return empty string to match Go's behavior } /** * Get returns a value previously set by Add or Set. */ get(key) { return this.values.get(key); } Get(key) { // Alias for get to match Go's method naming return this.get(key); } /** * Values returns the raw backing map. */ getValues() { return this.values; } /** * SetInMap stores a value to a map with the given key in the Node context. */ setInMap(key, mapKey, value) { let mapValue = this.values.get(key); if (!mapValue || typeof mapValue !== 'object') { mapValue = {}; this.values.set(key, mapValue); } mapValue[mapKey] = value; return ''; // Return empty string to match Go's behavior } } exports.Scratch = Scratch; //# sourceMappingURL=scratch.js.map