liquidjs
Version:
A simple, expressive and safe Shopify / Github Pages compatible template engine in pure JavaScript.
1 lines • 252 kB
Source Map (JSON)
{"version":3,"file":"liquid.browser.min.js","sources":["../src/tokens/token.ts","../src/drop/drop.ts","../src/util/underscore.ts","../src/util/error.ts","../src/util/character.ts","../src/util/assert.ts","../src/drop/null-drop.ts","../src/drop/empty-drop.ts","../src/drop/blank-drop.ts","../src/drop/forloop-drop.ts","../src/drop/block-drop.ts","../src/drop/comparable.ts","../src/util/literal.ts","../src/util/operator-trie.ts","../src/util/async.ts","../src/util/strftime.ts","../src/util/timezone-date.ts","../src/tokens/delimited-token.ts","../src/tokens/tag-token.ts","../src/tokens/output-token.ts","../src/tokens/html-token.ts","../src/tokens/number-token.ts","../src/tokens/identifier-token.ts","../src/tokens/literal-token.ts","../src/tokens/operator-token.ts","../src/tokens/property-access-token.ts","../src/tokens/filter-token.ts","../src/tokens/hash-token.ts","../src/render/string.ts","../src/tokens/quoted-token.ts","../src/tokens/range-token.ts","../src/tokens/liquid-tag-token.ts","../src/tokens/filtered-value-token.ts","../src/emitters/simple-emitter.ts","../src/build/streamed-emitter-browser.ts","../src/emitters/keeping-type-emitter.ts","../src/render/render.ts","../src/render/expression.ts","../src/render/boolean.ts","../src/render/operator.ts","../src/cache/lru.ts","../src/build/fs-impl-browser.ts","../src/filters/misc.ts","../src/filters/html.ts","../src/liquid-options.ts","../src/parser/whitespace-ctrl.ts","../src/parser/tokenizer.ts","../src/parser/parse-stream.ts","../src/template/template-impl.ts","../src/template/tag.ts","../src/template/hash.ts","../src/template/filter.ts","../src/parser/filter-arg.ts","../src/template/value.ts","../src/template/output.ts","../src/template/html.ts","../src/fs/loader.ts","../src/parser/parser.ts","../src/util/type-guards.ts","../src/parser/token-kind.ts","../src/context/block-mode.ts","../src/context/context.ts","../src/filters/math.ts","../src/filters/url.ts","../src/filters/array.ts","../src/filters/date.ts","../src/filters/string.ts","../src/filters/index.ts","../src/tags/assign.ts","../src/tags/for.ts","../src/tags/capture.ts","../src/tags/case.ts","../src/tags/comment.ts","../src/tags/render.ts","../src/tags/include.ts","../src/tags/decrement.ts","../src/tags/cycle.ts","../src/tags/if.ts","../src/tags/increment.ts","../src/tags/layout.ts","../src/tags/block.ts","../src/tags/raw.ts","../src/drop/tablerowloop-drop.ts","../src/tags/tablerow.ts","../src/tags/unless.ts","../src/tags/break.ts","../src/tags/continue.ts","../src/tags/echo.ts","../src/tags/liquid.ts","../src/tags/inline-comment.ts","../src/tags/index.ts","../src/liquid.ts","../src/template/tag-options-adapter.ts","../src/index.ts"],"sourcesContent":["import { TokenKind } from '../parser'\n\nexport abstract class Token {\n public constructor (\n public kind: TokenKind,\n public input: string,\n public begin: number,\n public end: number,\n public file?: string\n ) {}\n public getText () {\n return this.input.slice(this.begin, this.end)\n }\n public getPosition () {\n let [row, col] = [1, 1]\n for (let i = 0; i < this.begin; i++) {\n if (this.input[i] === '\\n') {\n row++\n col = 1\n } else col++\n }\n return [row, col]\n }\n public size () {\n return this.end - this.begin\n }\n}\n","export abstract class Drop {\n public liquidMethodMissing (key: string | number): Promise<any> | any {\n return undefined\n }\n}\n","import { Drop } from '../drop/drop'\n\nexport const toString = Object.prototype.toString\nconst toLowerCase = String.prototype.toLowerCase\n\nexport const hasOwnProperty = Object.hasOwnProperty\n\nexport function isString (value: any): value is string {\n return typeof value === 'string'\n}\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport function isFunction (value: any): value is Function {\n return typeof value === 'function'\n}\n\nexport function isPromise<T> (val: any): val is Promise<T> {\n return val && isFunction(val.then)\n}\n\nexport function isIterator (val: any): val is IterableIterator<any> {\n return val && isFunction(val.next) && isFunction(val.throw) && isFunction(val.return)\n}\n\nexport function escapeRegex (str: string) {\n return str.replace(/[-/\\\\^$*+?.()|[\\]{}]/g, '\\\\$&')\n}\n\nexport function promisify<T1, T2> (fn: (arg1: T1, cb: (err: Error | null, result: T2) => void) => void): (arg1: T1) => Promise<T2>;\nexport function promisify<T1, T2, T3> (fn: (arg1: T1, arg2: T2, cb: (err: Error | null, result: T3) => void) => void): (arg1: T1, arg2: T2) => Promise<T3>;\nexport function promisify (fn: any) {\n return function (...args: any[]) {\n return new Promise((resolve, reject) => {\n fn(...args, (err: Error, result: any) => {\n err ? reject(err) : resolve(result)\n })\n })\n }\n}\n\nexport function stringify (value: any): string {\n value = toValue(value)\n if (isString(value)) return value\n if (isNil(value)) return ''\n if (isArray(value)) return value.map(x => stringify(x)).join('')\n return String(value)\n}\n\nexport function toEnumerable<T = unknown> (val: any): T[] {\n val = toValue(val)\n if (isArray(val)) return val\n if (isString(val) && val.length > 0) return [val] as unknown as T[]\n if (isIterable(val)) return Array.from(val)\n if (isObject(val)) return Object.keys(val).map((key) => [key, val[key]]) as unknown as T[]\n return []\n}\n\nexport function toArray (val: any) {\n val = toValue(val)\n if (isNil(val)) return []\n if (isArray(val)) return val\n return [ val ]\n}\n\nexport function toValue (value: any): any {\n return (value instanceof Drop && isFunction(value.valueOf)) ? value.valueOf() : value\n}\n\nexport function isNumber (value: any): value is number {\n return typeof value === 'number'\n}\n\nexport function toLiquid (value: any): any {\n if (value && isFunction(value.toLiquid)) return toLiquid(value.toLiquid())\n return value\n}\n\nexport function isNil (value: any): boolean {\n return value == null\n}\n\nexport function isUndefined (value: any): boolean {\n return value === undefined\n}\n\nexport function isArray (value: any): value is any[] {\n // be compatible with IE 8\n return toString.call(value) === '[object Array]'\n}\n\nexport function isIterable (value: any): value is Iterable<any> {\n return isObject(value) && Symbol.iterator in value\n}\n\n/*\n * Iterates over own enumerable string keyed properties of an object and invokes iteratee for each property.\n * The iteratee is invoked with three arguments: (value, key, object).\n * Iteratee functions may exit iteration early by explicitly returning false.\n * @param {Object} object The object to iterate over.\n * @param {Function} iteratee The function invoked per iteration.\n * @return {Object} Returns object.\n */\nexport function forOwn <T> (\n obj: Record<string, T> | undefined,\n iteratee: ((val: T, key: string, obj: {[key: string]: T}) => boolean | void)\n) {\n obj = obj || {}\n for (const k in obj) {\n if (hasOwnProperty.call(obj, k)) {\n if (iteratee(obj[k], k, obj) === false) break\n }\n }\n return obj\n}\n\nexport function last <T>(arr: T[]): T;\nexport function last (arr: string): string;\nexport function last (arr: any[] | string): any | string {\n return arr[arr.length - 1]\n}\n\n/*\n * Checks if value is the language type of Object.\n * (e.g. arrays, functions, objects, regexes, new Number(0), and new String(''))\n * @param {any} value The value to check.\n * @return {Boolean} Returns true if value is an object, else false.\n */\nexport function isObject (value: any): value is object {\n const type = typeof value\n return value !== null && (type === 'object' || type === 'function')\n}\n\nexport function range (start: number, stop: number, step = 1) {\n const arr: number[] = []\n for (let i = start; i < stop; i += step) {\n arr.push(i)\n }\n return arr\n}\n\nexport function padStart (str: any, length: number, ch = ' ') {\n return pad(str, length, ch, (str, ch) => ch + str)\n}\n\nexport function padEnd (str: any, length: number, ch = ' ') {\n return pad(str, length, ch, (str, ch) => str + ch)\n}\n\nexport function pad (str: any, length: number, ch: string, add: (str: string, ch: string) => string) {\n str = String(str)\n let n = length - str.length\n while (n-- > 0) str = add(str, ch)\n return str\n}\n\nexport function identify<T> (val: T): T {\n return val\n}\n\nexport function changeCase (str: string): string {\n const hasLowerCase = [...str].some(ch => ch >= 'a' && ch <= 'z')\n return hasLowerCase ? str.toUpperCase() : str.toLowerCase()\n}\n\nexport function ellipsis (str: string, N: number): string {\n return str.length > N ? str.slice(0, N - 3) + '...' : str\n}\n\n// compare string in case-insensitive way, undefined values to the tail\nexport function caseInsensitiveCompare (a: any, b: any) {\n if (a == null && b == null) return 0\n if (a == null) return 1\n if (b == null) return -1\n a = toLowerCase.call(a)\n b = toLowerCase.call(b)\n if (a < b) return -1\n if (a > b) return 1\n return 0\n}\n\nexport function argumentsToValue<F extends (...args: any) => any> (fn: F) {\n return (...args: Parameters<F>) => fn(...args.map(toValue))\n}\n\nexport function escapeRegExp (text: string) {\n return text.replace(/[-[\\]{}()*+?.,\\\\^$|#\\s]/g, '\\\\$&')\n}\n","import * as _ from './underscore'\nimport { Token } from '../tokens/token'\nimport { Template } from '../template/template'\n\n/**\n * targeting ES5, extends Error won't create a proper prototype chain, need a trait to keep track of classes\n */\nconst TRAIT = '__liquidClass__'\n\nexport abstract class LiquidError extends Error {\n public token!: Token\n public context = ''\n private originalError?: Error\n public constructor (err: Error | string, token: Token) {\n /**\n * note: for ES5 targeting, `this` will be replaced by return value of Error(),\n * thus everything on `this` will be lost, avoid calling `LiquidError` methods here\n */\n super(typeof err === 'string' ? err : err.message)\n if (typeof err !== 'string') Object.defineProperty(this, 'originalError', { value: err, enumerable: false })\n Object.defineProperty(this, 'token', { value: token, enumerable: false })\n Object.defineProperty(this, TRAIT, { value: 'LiquidError', enumerable: false })\n }\n protected update () {\n Object.defineProperty(this, 'context', { value: mkContext(this.token), enumerable: false })\n this.message = mkMessage(this.message, this.token)\n this.stack = this.message + '\\n' + this.context +\n '\\n' + this.stack\n if (this.originalError) this.stack += '\\nFrom ' + this.originalError.stack\n }\n static is (obj: unknown): obj is LiquidError {\n return obj?.[TRAIT] === 'LiquidError'\n }\n}\n\nexport class TokenizationError extends LiquidError {\n public constructor (message: string, token: Token) {\n super(message, token)\n this.name = 'TokenizationError'\n super.update()\n }\n}\n\nexport class ParseError extends LiquidError {\n public constructor (err: Error, token: Token) {\n super(err, token)\n this.name = 'ParseError'\n this.message = err.message\n super.update()\n }\n}\n\nexport class RenderError extends LiquidError {\n public constructor (err: Error, tpl: Template) {\n super(err, tpl.token)\n this.name = 'RenderError'\n this.message = err.message\n super.update()\n }\n public static is (obj: any): obj is RenderError {\n return obj.name === 'RenderError'\n }\n}\n\nexport class LiquidErrors extends LiquidError {\n public constructor (public errors: RenderError[]) {\n super(errors[0], errors[0].token)\n this.name = 'LiquidErrors'\n const s = errors.length > 1 ? 's' : ''\n this.message = `${errors.length} error${s} found`\n super.update()\n }\n public static is (obj: any): obj is LiquidErrors {\n return obj.name === 'LiquidErrors'\n }\n}\n\nexport class UndefinedVariableError extends LiquidError {\n public constructor (err: Error, token: Token) {\n super(err, token)\n this.name = 'UndefinedVariableError'\n this.message = err.message\n super.update()\n }\n}\n\n// only used internally; raised where we don't have token information,\n// so it can't be an UndefinedVariableError.\nexport class InternalUndefinedVariableError extends Error {\n variableName: string\n\n public constructor (variableName: string) {\n super(`undefined variable: ${variableName}`)\n this.name = 'InternalUndefinedVariableError'\n this.variableName = variableName\n }\n}\n\nexport class AssertionError extends Error {\n public constructor (message: string) {\n super(message)\n this.name = 'AssertionError'\n this.message = message + ''\n }\n}\n\nfunction mkContext (token: Token) {\n const [line, col] = token.getPosition()\n const lines = token.input.split('\\n')\n const begin = Math.max(line - 2, 1)\n const end = Math.min(line + 3, lines.length)\n\n const context = _\n .range(begin, end + 1)\n .map(lineNumber => {\n const rowIndicator = (lineNumber === line) ? '>> ' : ' '\n const num = _.padStart(String(lineNumber), String(end).length)\n let text = `${rowIndicator}${num}| `\n\n const colIndicator = lineNumber === line\n ? '\\n' + _.padStart('^', col + text.length)\n : ''\n\n text += lines[lineNumber - 1]\n text += colIndicator\n return text\n })\n .join('\\n')\n\n return context\n}\n\nfunction mkMessage (msg: string, token: Token) {\n if (token.file) msg += `, file:${token.file}`\n const [line, col] = token.getPosition()\n msg += `, line:${line}, col:${col}`\n return msg\n}\n","// **DO NOT CHANGE THIS FILE**\n//\n// This file is generated by bin/character-gen.js\n// bitmask character types to boost performance\nexport const TYPES = [0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 4, 4, 4, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 2, 8, 0, 0, 0, 0, 8, 0, 0, 0, 64, 0, 65, 0, 0, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 0, 0, 2, 2, 2, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]\nexport const WORD = 1\nexport const OPERATOR = 2\nexport const BLANK = 4\nexport const QUOTE = 8\nexport const INLINE_BLANK = 16\nexport const NUMBER = 32\nexport const SIGN = 64\nexport const PUNCTUATION = 128\n\nexport function isWord (char: string): boolean {\n const code = char.charCodeAt(0)\n return code >= 128 ? !TYPES[code] : !!(TYPES[code] & WORD)\n}\nTYPES[160] = TYPES[5760] = TYPES[6158] = TYPES[8192] = TYPES[8193] = TYPES[8194] = TYPES[8195] = TYPES[8196] = TYPES[8197] = TYPES[8198] = TYPES[8199] = TYPES[8200] = TYPES[8201] = TYPES[8202] = TYPES[8232] = TYPES[8233] = TYPES[8239] = TYPES[8287] = TYPES[12288] = BLANK\nTYPES[8220] = TYPES[8221] = PUNCTUATION\n","import { AssertionError } from './error'\n\nexport function assert <T> (predicate: T | null | undefined, message?: string | (() => string)) {\n if (!predicate) {\n const msg = typeof message === 'function'\n ? message()\n : (message || `expect ${predicate} to be true`)\n throw new AssertionError(msg)\n }\n}\n","import { Drop } from './drop'\nimport { Comparable } from './comparable'\nimport { isNil, toValue } from '../util'\n\nexport class NullDrop extends Drop implements Comparable {\n public equals (value: any) {\n return isNil(toValue(value))\n }\n public gt () {\n return false\n }\n public geq () {\n return false\n }\n public lt () {\n return false\n }\n public leq () {\n return false\n }\n public valueOf () {\n return null\n }\n}\n","import { Drop } from './drop'\nimport { Comparable } from './comparable'\nimport { isObject, isString, isArray, toValue } from '../util'\n\nexport class EmptyDrop extends Drop implements Comparable {\n public equals (value: any) {\n if (value instanceof EmptyDrop) return false\n value = toValue(value)\n if (isString(value) || isArray(value)) return value.length === 0\n if (isObject(value)) return Object.keys(value).length === 0\n return false\n }\n public gt () {\n return false\n }\n public geq () {\n return false\n }\n public lt () {\n return false\n }\n public leq () {\n return false\n }\n public valueOf () {\n return ''\n }\n}\n","import { isNil, isString, toValue } from '../util'\nimport { EmptyDrop } from '../drop'\n\nexport class BlankDrop extends EmptyDrop {\n public equals (value: any) {\n if (value === false) return true\n if (isNil(toValue(value))) return true\n if (isString(value)) return /^\\s*$/.test(value)\n return super.equals(value)\n }\n}\n","import { Drop } from './drop'\n\nexport class ForloopDrop extends Drop {\n protected i = 0\n public name: string\n public length: number\n public constructor (length: number, collection: string, variable: string) {\n super()\n this.length = length\n this.name = `${variable}-${collection}`\n }\n public next () {\n this.i++\n }\n public index0 () {\n return this.i\n }\n public index () {\n return this.i + 1\n }\n public first () {\n return this.i === 0\n }\n public last () {\n return this.i === this.length - 1\n }\n public rindex () {\n return this.length - this.i\n }\n public rindex0 () {\n return this.length - this.i - 1\n }\n public valueOf () {\n return JSON.stringify(this)\n }\n}\n","import { Drop } from './drop'\n\nexport class BlockDrop extends Drop {\n constructor (\n // the block render from layout template\n private superBlockRender: () => Iterable<any> = () => ''\n ) {\n super()\n }\n /**\n * Provide parent access in child block by\n * {{ block.super }}\n */\n public super () {\n return this.superBlockRender()\n }\n}\n","import { isFunction } from '../util'\n\nexport interface Comparable {\n equals: (rhs: any) => boolean;\n gt: (rhs: any) => boolean;\n geq: (rhs: any) => boolean;\n lt: (rhs: any) => boolean;\n leq: (rhs: any) => boolean;\n}\n\nexport function isComparable (arg: any): arg is Comparable {\n return (\n arg &&\n isFunction(arg.equals) &&\n isFunction(arg.gt) &&\n isFunction(arg.geq) &&\n isFunction(arg.lt) &&\n isFunction(arg.leq)\n )\n}\n","import { BlankDrop, EmptyDrop, NullDrop } from '../drop'\n\nconst nil = new NullDrop()\nexport const literalValues = {\n 'true': true,\n 'false': false,\n 'nil': nil,\n 'null': nil,\n 'empty': new EmptyDrop(),\n 'blank': new BlankDrop()\n}\n\nexport type LiteralKey = keyof typeof literalValues\nexport type LiteralValue = typeof literalValues[LiteralKey]\n","import { isWord } from '../util/character'\n\ninterface TrieInput<T> {\n [key: string]: T\n}\n\ninterface TrieLeafNode<T> {\n data: T;\n end: true;\n needBoundary?: true;\n}\n\nexport interface Trie<T> {\n [key: string]: Trie<T> | TrieLeafNode<T>;\n}\n\nexport type TrieNode<T> = Trie<T> | TrieLeafNode<T>\n\nexport function createTrie<T = any> (input: TrieInput<T>): Trie<T> {\n const trie: Trie<T> = {}\n for (const [name, data] of Object.entries(input)) {\n let node: Trie<T> | TrieLeafNode<T> = trie\n\n for (let i = 0; i < name.length; i++) {\n const c = name[i]\n node[c] = node[c] || {}\n\n if (i === name.length - 1 && isWord(name[i])) {\n node[c].needBoundary = true\n }\n\n node = node[c]\n }\n\n node.data = data\n node.end = true\n }\n return trie\n}\n","import { isPromise, isIterator } from './underscore'\n\n// convert an async iterator to a Promise\nexport async function toPromise<T> (val: Generator<unknown, T, unknown> | Promise<T> | T): Promise<T> {\n if (!isIterator(val)) return val\n let value: unknown\n let done = false\n let next = 'next'\n do {\n const state = val[next](value)\n done = state.done\n value = state.value\n next = 'next'\n try {\n if (isIterator(value)) value = toPromise(value)\n if (isPromise(value)) value = await value\n } catch (err) {\n next = 'throw'\n value = err\n }\n } while (!done)\n return value as T\n}\n\n// convert an async iterator to a value in a synchronous manner\nexport function toValueSync<T> (val: Generator<unknown, T, unknown> | T): T {\n if (!isIterator(val)) return val\n let value: any\n let done = false\n let next = 'next'\n do {\n const state = val[next](value)\n done = state.done\n value = state.value\n next = 'next'\n if (isIterator(value)) {\n try {\n value = toValueSync(value)\n } catch (err) {\n next = 'throw'\n value = err\n }\n }\n } while (!done)\n return value\n}\n","import { changeCase, padStart, padEnd } from './underscore'\nimport { LiquidDate } from './liquid-date'\n\nconst rFormat = /%([-_0^#:]+)?(\\d+)?([EO])?(.)/\nconst monthNames = [\n 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',\n 'September', 'October', 'November', 'December'\n]\nconst dayNames = [\n 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'\n]\nconst monthNamesShort = monthNames.map(abbr)\nconst dayNamesShort = dayNames.map(abbr)\ninterface FormatOptions {\n flags: object;\n width?: string;\n modifier?: string;\n}\n\nfunction abbr (str: string) {\n return str.slice(0, 3)\n}\n\n// prototype extensions\nfunction daysInMonth (d: LiquidDate) {\n const feb = isLeapYear(d) ? 29 : 28\n return [31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n}\nfunction getDayOfYear (d: LiquidDate) {\n let num = 0\n for (let i = 0; i < d.getMonth(); ++i) {\n num += daysInMonth(d)[i]\n }\n return num + d.getDate()\n}\nfunction getWeekOfYear (d: LiquidDate, startDay: number) {\n // Skip to startDay of this week\n const now = getDayOfYear(d) + (startDay - d.getDay())\n // Find the first startDay of the year\n const jan1 = new Date(d.getFullYear(), 0, 1)\n const then = (7 - jan1.getDay() + startDay)\n return String(Math.floor((now - then) / 7) + 1)\n}\nfunction isLeapYear (d: LiquidDate) {\n const year = d.getFullYear()\n return !!((year & 3) === 0 && (year % 100 || (year % 400 === 0 && year)))\n}\nfunction ordinal (d: LiquidDate) {\n const date = d.getDate()\n if ([11, 12, 13].includes(date)) return 'th'\n\n switch (date % 10) {\n case 1: return 'st'\n case 2: return 'nd'\n case 3: return 'rd'\n default: return 'th'\n }\n}\nfunction century (d: LiquidDate) {\n return parseInt(d.getFullYear().toString().substring(0, 2), 10)\n}\n\n// default to 0\nconst padWidths = {\n d: 2,\n e: 2,\n H: 2,\n I: 2,\n j: 3,\n k: 2,\n l: 2,\n L: 3,\n m: 2,\n M: 2,\n S: 2,\n U: 2,\n W: 2\n}\n\n// default to '0'\nconst padChars = {\n a: ' ',\n A: ' ',\n b: ' ',\n B: ' ',\n c: ' ',\n e: ' ',\n k: ' ',\n l: ' ',\n p: ' ',\n P: ' '\n}\nfunction getTimezoneOffset (d: LiquidDate, opts: FormatOptions) {\n const nOffset = Math.abs(d.getTimezoneOffset())\n const h = Math.floor(nOffset / 60)\n const m = nOffset % 60\n return (d.getTimezoneOffset() > 0 ? '-' : '+') +\n padStart(h, 2, '0') +\n (opts.flags[':'] ? ':' : '') +\n padStart(m, 2, '0')\n}\nconst formatCodes = {\n a: (d: LiquidDate) => dayNamesShort[d.getDay()],\n A: (d: LiquidDate) => dayNames[d.getDay()],\n b: (d: LiquidDate) => monthNamesShort[d.getMonth()],\n B: (d: LiquidDate) => monthNames[d.getMonth()],\n c: (d: LiquidDate) => d.toLocaleString(),\n C: (d: LiquidDate) => century(d),\n d: (d: LiquidDate) => d.getDate(),\n e: (d: LiquidDate) => d.getDate(),\n H: (d: LiquidDate) => d.getHours(),\n I: (d: LiquidDate) => String(d.getHours() % 12 || 12),\n j: (d: LiquidDate) => getDayOfYear(d),\n k: (d: LiquidDate) => d.getHours(),\n l: (d: LiquidDate) => String(d.getHours() % 12 || 12),\n L: (d: LiquidDate) => d.getMilliseconds(),\n m: (d: LiquidDate) => d.getMonth() + 1,\n M: (d: LiquidDate) => d.getMinutes(),\n N: (d: LiquidDate, opts: FormatOptions) => {\n const width = Number(opts.width) || 9\n const str = String(d.getMilliseconds()).slice(0, width)\n return padEnd(str, width, '0')\n },\n p: (d: LiquidDate) => (d.getHours() < 12 ? 'AM' : 'PM'),\n P: (d: LiquidDate) => (d.getHours() < 12 ? 'am' : 'pm'),\n q: (d: LiquidDate) => ordinal(d),\n s: (d: LiquidDate) => Math.round(d.getTime() / 1000),\n S: (d: LiquidDate) => d.getSeconds(),\n u: (d: LiquidDate) => d.getDay() || 7,\n U: (d: LiquidDate) => getWeekOfYear(d, 0),\n w: (d: LiquidDate) => d.getDay(),\n W: (d: LiquidDate) => getWeekOfYear(d, 1),\n x: (d: LiquidDate) => d.toLocaleDateString(),\n X: (d: LiquidDate) => d.toLocaleTimeString(),\n y: (d: LiquidDate) => d.getFullYear().toString().slice(2, 4),\n Y: (d: LiquidDate) => d.getFullYear(),\n z: getTimezoneOffset,\n Z: (d: LiquidDate, opts: FormatOptions) => {\n if (d.getTimezoneName) {\n return d.getTimezoneName() || getTimezoneOffset(d, opts)\n }\n return (typeof Intl !== 'undefined' ? Intl.DateTimeFormat().resolvedOptions().timeZone : '')\n },\n 't': () => '\\t',\n 'n': () => '\\n',\n '%': () => '%'\n};\n(formatCodes as any).h = formatCodes.b\n\nexport function strftime (d: LiquidDate, formatStr: string) {\n let output = ''\n let remaining = formatStr\n let match\n while ((match = rFormat.exec(remaining))) {\n output += remaining.slice(0, match.index)\n remaining = remaining.slice(match.index + match[0].length)\n output += format(d, match)\n }\n return output + remaining\n}\n\nfunction format (d: LiquidDate, match: RegExpExecArray) {\n const [input, flagStr = '', width, modifier, conversion] = match\n const convert = formatCodes[conversion]\n if (!convert) return input\n const flags = {}\n for (const flag of flagStr) flags[flag] = true\n let ret = String(convert(d, { flags, width, modifier }))\n let padChar = padChars[conversion] || '0'\n let padWidth = width || padWidths[conversion] || 0\n if (flags['^']) ret = ret.toUpperCase()\n else if (flags['#']) ret = changeCase(ret)\n if (flags['_']) padChar = ' '\n else if (flags['0']) padChar = '0'\n if (flags['-']) padWidth = 0\n return padStart(ret, padWidth, padChar)\n}\n","import { LiquidDate } from './liquid-date'\nimport { isString } from './underscore'\n\n// one minute in milliseconds\nconst OneMinute = 60000\nconst ISO8601_TIMEZONE_PATTERN = /([zZ]|([+-])(\\d{2}):(\\d{2}))$/\n\n/**\n * A date implementation with timezone info, just like Ruby date\n *\n * Implementation:\n * - create a Date offset by it's timezone difference, avoiding overriding a bunch of methods\n * - rewrite getTimezoneOffset() to trick strftime\n */\nexport class TimezoneDate implements LiquidDate {\n private timezoneOffset: number\n private timezoneName: string\n private date: Date\n private displayDate: Date\n constructor (init: string | number | Date | TimezoneDate, timezone: number | string) {\n this.date = init instanceof TimezoneDate\n ? init.date\n : new Date(init)\n this.timezoneOffset = isString(timezone) ? TimezoneDate.getTimezoneOffset(timezone, this.date) : timezone\n this.timezoneName = isString(timezone) ? timezone : ''\n\n const diff = (this.date.getTimezoneOffset() - this.timezoneOffset) * OneMinute\n const time = this.date.getTime() + diff\n this.displayDate = new Date(time)\n }\n\n getTime () {\n return this.displayDate.getTime()\n }\n\n getMilliseconds () {\n return this.displayDate.getMilliseconds()\n }\n getSeconds () {\n return this.displayDate.getSeconds()\n }\n getMinutes () {\n return this.displayDate.getMinutes()\n }\n getHours () {\n return this.displayDate.getHours()\n }\n getDay () {\n return this.displayDate.getDay()\n }\n getDate () {\n return this.displayDate.getDate()\n }\n getMonth () {\n return this.displayDate.getMonth()\n }\n getFullYear () {\n return this.displayDate.getFullYear()\n }\n toLocaleString (locale?: string, init?: any) {\n if (init?.timeZone) {\n return this.date.toLocaleString(locale, init)\n }\n return this.displayDate.toLocaleString(locale, init)\n }\n toLocaleTimeString (locale?: string) {\n return this.displayDate.toLocaleTimeString(locale)\n }\n toLocaleDateString (locale?: string) {\n return this.displayDate.toLocaleDateString(locale)\n }\n getTimezoneOffset () {\n return this.timezoneOffset!\n }\n getTimezoneName () {\n return this.timezoneName\n }\n\n /**\n * Create a Date object fixed to it's declared Timezone. Both\n * - 2021-08-06T02:29:00.000Z and\n * - 2021-08-06T02:29:00.000+08:00\n * will always be displayed as\n * - 2021-08-06 02:29:00\n * regardless timezoneOffset in JavaScript realm\n *\n * The implementation hack:\n * Instead of calling `.getMonth()`/`.getUTCMonth()` respect to `preserveTimezones`,\n * we create a different Date to trick strftime, it's both simpler and more performant.\n * Given that a template is expected to be parsed fewer times than rendered.\n */\n static createDateFixedToTimezone (dateString: string): LiquidDate {\n const m = dateString.match(ISO8601_TIMEZONE_PATTERN)\n // representing a UTC timestamp\n if (m && m[1] === 'Z') {\n return new TimezoneDate(+new Date(dateString), 0)\n }\n // has a timezone specified\n if (m && m[2] && m[3] && m[4]) {\n const [, , sign, hours, minutes] = m\n const offset = (sign === '+' ? -1 : 1) * (parseInt(hours, 10) * 60 + parseInt(minutes, 10))\n return new TimezoneDate(+new Date(dateString), offset)\n }\n return new Date(dateString)\n }\n private static getTimezoneOffset (timezoneName: string, date = new Date()) {\n const localDateString = date.toLocaleString('en-US', { timeZone: timezoneName })\n const utcDateString = date.toLocaleString('en-US', { timeZone: 'UTC' })\n\n const localDate = new Date(localDateString)\n const utcDate = new Date(utcDateString)\n return (+utcDate - +localDate) / (60 * 1000)\n }\n}\n","import { Token } from './token'\nimport { TokenKind } from '../parser'\nimport { TYPES, BLANK } from '../util'\n\nexport abstract class DelimitedToken extends Token {\n public trimLeft = false\n public trimRight = false\n public contentRange: [number, number]\n public constructor (\n kind: TokenKind,\n [contentBegin, contentEnd]: [number, number],\n input: string,\n begin: number,\n end: number,\n trimLeft: boolean,\n trimRight: boolean,\n file?: string\n ) {\n super(kind, input, begin, end, file)\n const tl = input[contentBegin] === '-'\n const tr = input[contentEnd - 1] === '-'\n\n let l = tl ? contentBegin + 1 : contentBegin\n let r = tr ? contentEnd - 1 : contentEnd\n while (l < r && (TYPES[input.charCodeAt(l)] & BLANK)) l++\n while (r > l && (TYPES[input.charCodeAt(r - 1)] & BLANK)) r--\n\n this.contentRange = [l, r]\n this.trimLeft = tl || trimLeft\n this.trimRight = tr || trimRight\n }\n get content () {\n return this.input.slice(this.contentRange[0], this.contentRange[1])\n }\n}\n","import { DelimitedToken } from './delimited-token'\nimport { Tokenizer, TokenKind } from '../parser'\nimport type { NormalizedFullOptions } from '../liquid-options'\n\nexport class TagToken extends DelimitedToken {\n public name: string\n public tokenizer: Tokenizer\n public constructor (\n input: string,\n begin: number,\n end: number,\n options: NormalizedFullOptions,\n file?: string\n ) {\n const { trimTagLeft, trimTagRight, tagDelimiterLeft, tagDelimiterRight } = options\n const [valueBegin, valueEnd] = [begin + tagDelimiterLeft.length, end - tagDelimiterRight.length]\n super(TokenKind.Tag, [valueBegin, valueEnd], input, begin, end, trimTagLeft, trimTagRight, file)\n\n this.tokenizer = new Tokenizer(input, options.operators, file, this.contentRange)\n this.name = this.tokenizer.readTagName()\n this.tokenizer.assert(this.name, `illegal tag syntax, tag name expected`)\n this.tokenizer.skipBlank()\n }\n get args (): string {\n return this.tokenizer.input.slice(this.tokenizer.p, this.contentRange[1])\n }\n}\n","import { DelimitedToken } from './delimited-token'\nimport { NormalizedFullOptions } from '../liquid-options'\nimport { TokenKind } from '../parser'\n\nexport class OutputToken extends DelimitedToken {\n public constructor (\n input: string,\n begin: number,\n end: number,\n options: NormalizedFullOptions,\n file?: string\n ) {\n const { trimOutputLeft, trimOutputRight, outputDelimiterLeft, outputDelimiterRight } = options\n const valueRange: [number, number] = [begin + outputDelimiterLeft.length, end - outputDelimiterRight.length]\n super(TokenKind.Output, valueRange, input, begin, end, trimOutputLeft, trimOutputRight, file)\n }\n}\n","import { Token } from './token'\nimport { TokenKind } from '../parser'\n\nexport class HTMLToken extends Token {\n trimLeft = 0\n trimRight = 0\n constructor (\n public input: string,\n public begin: number,\n public end: number,\n public file?: string\n ) {\n super(TokenKind.HTML, input, begin, end, file)\n }\n public getContent () {\n return this.input.slice(this.begin + this.trimLeft, this.end - this.trimRight)\n }\n}\n","import { Token } from './token'\nimport { TokenKind } from '../parser'\n\nexport class NumberToken extends Token {\n public content: number\n constructor (\n public input: string,\n public begin: number,\n public end: number,\n public file?: string\n ) {\n super(TokenKind.Number, input, begin, end, file)\n this.content = Number(this.getText())\n }\n}\n","import { Token } from './token'\nimport { NUMBER, TYPES, SIGN } from '../util'\nimport { TokenKind } from '../parser'\n\nexport class IdentifierToken extends Token {\n public content: string\n constructor (\n public input: string,\n public begin: number,\n public end: number,\n public file?: string\n ) {\n super(TokenKind.Word, input, begin, end, file)\n this.content = this.getText()\n }\n isNumber (allowSign = false) {\n const begin = allowSign && TYPES[this.input.charCodeAt(this.begin)] & SIGN\n ? this.begin + 1\n : this.begin\n for (let i = begin; i < this.end; i++) {\n if (!(TYPES[this.input.charCodeAt(i)] & NUMBER)) return false\n }\n return true\n }\n}\n","import { Token } from './token'\nimport { TokenKind } from '../parser'\nimport { literalValues, LiteralValue } from '../util'\n\nexport class LiteralToken extends Token {\n public content: LiteralValue\n public literal: string\n public constructor (\n public input: string,\n public begin: number,\n public end: number,\n public file?: string\n ) {\n super(TokenKind.Literal, input, begin, end, file)\n this.literal = this.getText()\n this.content = literalValues[this.literal]\n }\n}\n","import { Token } from './token'\nimport { TokenKind } from '../parser'\n\nexport const enum OperatorType {\n Binary,\n Unary\n}\n\nexport const operatorPrecedences = {\n '==': 2,\n '!=': 2,\n '>': 2,\n '<': 2,\n '>=': 2,\n '<=': 2,\n 'contains': 2,\n 'not': 1,\n 'and': 0,\n 'or': 0\n}\n\nexport const operatorTypes = {\n '==': OperatorType.Binary,\n '!=': OperatorType.Binary,\n '>': OperatorType.Binary,\n '<': OperatorType.Binary,\n '>=': OperatorType.Binary,\n '<=': OperatorType.Binary,\n 'contains': OperatorType.Binary,\n 'not': OperatorType.Unary,\n 'and': OperatorType.Binary,\n 'or': OperatorType.Binary\n}\n\nexport class OperatorToken extends Token {\n public operator: string\n public constructor (\n public input: string,\n public begin: number,\n public end: number,\n public file?: string\n ) {\n super(TokenKind.Operator, input, begin, end, file)\n this.operator = this.getText()\n }\n getPrecedence () {\n const key = this.getText()\n return key in operatorPrecedences ? operatorPrecedences[key] : 1\n }\n}\n","import { Token } from './token'\nimport { LiteralToken } from './literal-token'\nimport { ValueToken } from './value-token'\nimport { IdentifierToken } from './identifier-token'\nimport { NumberToken } from './number-token'\nimport { RangeToken } from './range-token'\nimport { QuotedToken } from './quoted-token'\nimport { TokenKind } from '../parser'\n\nexport class PropertyAccessToken extends Token {\n constructor (\n public variable: QuotedToken | RangeToken | LiteralToken | NumberToken | undefined,\n public props: (ValueToken | IdentifierToken)[],\n input: string,\n begin: number,\n end: number,\n file?: string\n ) {\n super(TokenKind.PropertyAccess, input, begin, end, file)\n }\n}\n","import { Token } from './token'\nimport { FilterArg } from '../parser/filter-arg'\nimport { TokenKind } from '../parser'\n\nexport class FilterToken extends Token {\n public constructor (\n public name: string,\n public args: FilterArg[],\n input: string,\n begin: number,\n end: number,\n file?: string\n ) {\n super(TokenKind.Filter, input, begin, end, file)\n }\n}\n","import { Token } from './token'\nimport { ValueToken } from './value-token'\nimport { IdentifierToken } from './identifier-token'\nimport { TokenKind } from '../parser'\n\nexport class HashToken extends Token {\n constructor (\n public input: string,\n public begin: number,\n public end: number,\n public name: IdentifierToken,\n public value?: ValueToken,\n public file?: string\n ) {\n super(TokenKind.Hash, input, begin, end, file)\n }\n}\n","const rHex = /[\\da-fA-F]/\nconst rOct = /[0-7]/\nconst escapeChar = {\n b: '\\b',\n f: '\\f',\n n: '\\n',\n r: '\\r',\n t: '\\t',\n v: '\\x0B'\n}\n\nfunction hexVal (c: string) {\n const code = c.charCodeAt(0)\n if (code >= 97) return code - 87\n if (code >= 65) return code - 55\n return code - 48\n}\n\nexport function parseStringLiteral (str: string): string {\n let ret = ''\n for (let i = 1; i < str.length - 1; i++) {\n if (str[i] !== '\\\\') {\n ret += str[i]\n continue\n }\n if (escapeChar[str[i + 1]] !== undefined) {\n ret += escapeChar[str[++i]]\n } else if (str[i + 1] === 'u') {\n let val = 0\n let j = i + 2\n while (j <= i + 5 && rHex.test(str[j])) {\n val = val * 16 + hexVal(str[j++])\n }\n i = j - 1\n ret += String.fromCharCode(val)\n } else if (!rOct.test(str[i + 1])) {\n ret += str[++i]\n } else {\n let j = i + 1\n let val = 0\n while (j <= i + 3 && rOct.test(str[j])) {\n val = val * 8 + hexVal(str[j++])\n }\n i = j - 1\n ret += String.fromCharCode(val)\n }\n }\n return ret\n}\n","import { Token } from './token'\nimport { TokenKind } from '../parser'\nimport { parseStringLiteral } from '../render/string'\n\nexport class QuotedToken extends Token {\n public readonly content: string\n constructor (\n public input: string,\n public begin: number,\n public end: number,\n public file?: string\n ) {\n super(TokenKind.Quoted, input, begin, end, file)\n this.content = parseStringLiteral(this.getText())\n }\n}\n","import { Token } from './token'\nimport { ValueToken } from './value-token'\nimport { TokenKind } from '../parser'\n\nexport class RangeToken extends Token {\n constructor (\n public input: string,\n public begin: number,\n public end: number,\n public lhs: ValueToken,\n public rhs: ValueToken,\n public file?: string\n ) {\n super(TokenKind.Range, input, begin, end, file)\n }\n}\n","import { DelimitedToken } from './delimited-token'\nimport { NormalizedFullOptions } from '../liquid-options'\nimport { Tokenizer, TokenKind } from '../parser'\n\n/**\n * LiquidTagToken is different from TagToken by not having delimiters `{%` or `%}`\n */\nexport class LiquidTagToken extends DelimitedToken {\n public name: string\n public args: string\n public tokenizer: Tokenizer\n public constructor (\n input: string,\n begin: number,\n end: number,\n options: NormalizedFullOptions,\n file?: string\n ) {\n super(TokenKind.Tag, [begin, end], input, begin, end, false, false, file)\n\n this.tokenizer = new Tokenizer(input, options.operators, file, this.contentRange)\n this.name = this.tokenizer.readTagName()\n this.tokenizer.assert(this.name, 'illegal liquid tag syntax')\n\n this.tokenizer.skipBlank()\n this.args = this.tokenizer.remaining()\n }\n}\n","import { Token } from './token'\nimport { FilterToken } from './filter-token'\nimport { TokenKind } from '../parser'\nimport { Expression } from '../render'\n\n/**\n * value expression with optional filters\n * e.g.\n * {% assign foo=\"bar\" | append: \"coo\" %}\n */\nexport class FilteredValueToken extends Token {\n constructor (\n public initial: Expression,\n public filters: FilterToken[],\n public input: string,\n public begin: number,\n public end: number,\n public file?: string\n ) {\n super(TokenKind.FilteredValue, input, begin, end, file)\n }\n}\n","import { stringify } from '../util'\nimport { Emitter } from './emitter'\n\nexport class SimpleEmitter implements Emitter {\n public buffer = '';\n\n public write (html: any) {\n this.buffer += stringify(html)\n }\n}\n","import { Emitter } from '../emitters'\n\nexport class StreamedEmitter implements Emitter {\n public buffer = '';\n public stream: NodeJS.ReadableStream = null as any\n constructor () {\n throw new Error('streaming not supported in browser')\n }\n public write: (html: any) => void\n public error: (err: Error) => void\n public end: () => void\n}\n","import { stringify, toValue } from '../util'\nimport { Emitter } from './emitter'\n\nexport class KeepingTypeEmitter implements Emitter {\n public buffer: any = '';\n\n public write (html: any) {\n html = toValue(html)\n // This will only preserve the type if the value is isolated.\n // I.E:\n // {{ my-port }} -> 42\n // {{ my-host }}:{{ my-port }} -> 'host:42'\n if (typeof html !== 'string' && this.buffer === '') {\n this.buffer = html\n } else {\n this.buffer = stringify(this.buffer) + stringify(html)\n }\n }\n}\n","import { toPromise, RenderError, LiquidErrors, LiquidError } from '../util'\nimport { Context } from '../context'\nimport { Template } from '../template'\nimport { Emitter, KeepingTypeEmitter, StreamedEmitter, SimpleEmitter } from '../emitters'\n\nexport class Render {\n public renderTemplatesToNodeStream (templates: Template[], ctx: Context): NodeJS.ReadableStream {\n const emitter = new StreamedEmitter()\n Promise.resolve().then(() => toPromise(this.renderTemplates(templates, ctx, emitter)))\n .then(() => emitter.end(), err => emitter.error(err))\n return emitter.stream\n }\n public * renderTemplates (templates: Template[], ctx: Context, emitter?: Emitter): IterableIterator<any> {\n if (!emitter) {\n emitter = ctx.opts.keepOutputType ? new KeepingTypeEmitter() : new SimpleEmitter()\n }\n const errors = []\n for (const tpl of templates) {\n try {\n // if tpl.render supports emitter, it'll return empty `html`\n const html = yield tpl.render(ctx, emitter)\n // if not, it'll return an `html`, write to the emitter for it\n html && emitter.write(html)\n if (emitter['break'] || emitter['continue']) break\n } catch (e) {\n const err = LiquidError.is(e) ? e : new RenderError(e as Error, tpl)\n if (ctx.opts.catchAllErrors) errors.push(err)\n else throw err\n }\n }\n if (errors.length) {\n throw new LiquidErrors(errors)\n }\n return emitter.buffer\n }\n}\n","import { QuotedToken, RangeToken, OperatorToken, Token, PropertyAccessToken, OperatorType, operatorTypes } from '../tokens'\nimport { isRangeToken, isPropertyAccessToken, UndefinedVariableError, range, isOperatorToken, assert } from '../util'\nimport type { Context } from '../context'\nimport type { UnaryOperatorHandler } from '../render'\n\nexport class Expression {\n private postfix: Token[]\n\n public constructor (tokens: IterableIterator<Token>) {\n this.postfix = [...toPostfix(tokens)]\n }\n public * evaluate (ctx: Context, lenient?: boolean): Generator<unknown, unknown, unknown> {\n assert(ctx, 'unable to evaluate: context not defined')\n const operands: any[] = []\n for (const token of this.postfix) {\n if (isOperatorToken(token)) {\n const r = operands.pop()\n let result\n if (operatorTypes[token.operator] === OperatorType.Unary) {\n result = yield (ctx.opts.operators[token.operator] as UnaryOperatorHandler)(r, ctx)\n } else {\n const l = operands.pop()\n result = yield ctx.opts.operators[token.operator](l, r, ctx)\n }\n operands.push(result)\n } else {\n operands.push(yield evalToken(token, ctx, lenient))\n }\n }\n return operands[0]\n }\n public valid () {\n return !!this.postfix.length\n }\n}\n\nexport function * evalToken (token: Token | undefined, ctx: Context, lenient = false): IterableIterator<unknown> {\n if (!token) return\n if ('content' in token) return token.content\n if (isPropertyAccessToken(token)) return yield evalPropertyAccessToken(token, ctx, lenient)\n if (isRangeToken(token)) return yield evalRangeToken(token, ctx)\n}\n\nfunction * evalPropertyAccessToken (token: PropertyAccessToken, ctx: Context, lenient: boolean): IterableIterator<unknown> {\n const props: string[] = []\n for (const prop of token.props) {\n props.push((yield evalToken(prop, ctx, false)) as unknown as string)\n }\n try {\n if (token.variable) {\n const variable = yield evalToken(token.variable, ctx, lenient)\n return yield ctx._getFromScope(variable, props)\n } else {\n return yield ctx._get(props)\n }\n } catch (e) {\n if (lenient && (e as Error).name === 'InternalUndefinedVariableError') return null\n throw (new UndefinedVariableError(e as Error, token))\n }\n}\n\nexport function evalQuotedToken (token: QuotedToken) {\n return token.content\n}\n\nfunction * evalRangeToken (token: RangeToken, ctx: Context) {\n const low: number = yield evalToken(token.lhs, ctx)\n const high: number = yield evalToken(token.rhs, ctx)\n return range(+low, +high + 1)\n}\n\nfunction * toPostfix (tokens: IterableIterator<Token>): IterableIterator<Token> {\n const ops: OperatorToken[] = []\n for (const token of tokens) {\n if (isOperatorToken(token)) {\n while (ops.length && ops[ops.length - 1].getPrecedence() > token.getPrecedence()) {\n yield ops.pop()!\n }\n ops.push(token)\n } else yield token\n }\n while (ops.length) {\n yield ops.pop()!\n }\n}\n","import { Context } from '../context/context'\nimport { toValue } from '../util'\n\nexport function isTruthy (val: any, ctx: Context): boolean {\n return !isFalsy(val, ctx)\n}\n\nexport function isFalsy (val: any, ctx: Context): boolean {\n val = toValue(val)\n\n if (ctx.opts.jsTruthy) {\n return !val\n } else {\n return val === false || undefined === val || val === null\n }\n}\n","import { isComparable } from '../drop/comparable'\nimport { Context } from '../context'\nimport { toValue } from '../util'\nimport { isFalsy, isTruthy } from '../render/boolean'\nimport { isArray, isFunction } from '../util/underscore'\n\nexport type UnaryOperatorHandler = (operand: any, ctx: Context) => boolean;\nexport type BinaryOperatorHandler = (lhs: any, rhs: any, ctx: Context) => boolean;\nexport type OperatorHandler = UnaryOperatorHandler | BinaryOperatorHandler;\nexport type Operators = Record<string, OperatorHandler>\n\nexport const defaultOperators: Operators = {\n '==': equals,\n '!=': (l: any, r: any) => !equals(l, r),\n '>': (l: any, r: any) => {\n if (isComparable(l)) return l.gt(r)\n if (isComparable(r)) return r.lt(l)\n return toValue(l) > toValue(r)\n },\n '<': (l: any, r: any) => {\n if (isComparable(l)) return l.lt(r)\n if (isComparable(r)) return r.gt(l)\n return toValue(l) < toValue(r)\n },\n '>=': (l: any, r: any) => {\n if (isComparable(l)) return l.geq(r)\n if (isComparable(r)) return r.leq(l)\n return toValue(l) >= toValue(r)\n },\n '<=': (l: any, r: any) => {\n if (isComparable(l)) return l.leq(r)\n if (isComparable(r)) return r.geq(l)\n return toValue(l) <= toValue(r)\n },\n 'contains': (l: any, r: any) => {\n l = toValue(l)\n if (isArray(l)) return l.some((i) => equals(i, r))\n if (isFunction(l?.indexOf)) return l.indexOf(toValue(r)) > -1\n return false\n },\n 'not': (v: any, ctx: Context) => isFalsy(toValue(v), ctx),\n 'and': (l: any, r: any, ctx: Context) => isTruthy(toValue(l), ctx) && isTruthy(toValue(r), ctx),\n 'or': (l: any, r: any, ctx: Context) => isTruthy(toValue(l), ctx) || isTruthy(toValue(r), ctx)\n}\n\nexport function equals (lhs: any, rhs: any): boolean {\n if (isComparable(lhs)) return lhs.equals(rhs)\n if (isComparable(rhs)) return rhs.equals(lhs)\n lhs = toValue(lhs)\n rhs = toValue(rhs)\n if (isArray(lhs)) {\n return isArray(rhs) && arrayEquals(lhs, rhs)\n }\n return lhs === rhs\n}\n\nfunction arrayEquals (lhs: any[], rhs: any[]): boolean {\n if (lhs.length !== rhs.length) return false\n return !lhs.some((value, i) => !equals(value, rhs[i]))\n}\n","import { Cache } from './cache'\n\nclass Node<T> {\n constructor (\n public key: string,\n public value: T,\n public next: Node<T>,\n public prev: Node<T>\n ) {}\n}\n\nexport class LRU<T> implements Cache<T> {\n private cache: Record<string, Node<T>> = {}\n private head: Node<T>\n private tail: Node<T>\n\n constructor (\n public limit: number,\n public size = 0\n ) {\n this.head = new Node<T>('HEAD', null as any, null as any, null as any)\n this.tail = new Node<T>('TAIL', null as any, null as any, null as any)\n this.head.next = this.tail\n this.tail.prev = this.head\n }\n\n write (key