mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 05:50:43 -07:00
feat: nil/null/empty/blank literals, resolves #102
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
import { isNil, isString } from 'src/util/underscore'
|
||||
import { isDrop } from 'src/drop/idrop'
|
||||
import { EmptyDrop } from 'src/drop/empty-drop'
|
||||
|
||||
export class BlankDrop extends EmptyDrop {
|
||||
equals (value: any) {
|
||||
if (value === false) return true
|
||||
if (isNil(isDrop(value) ? value.value() : value)) return true
|
||||
if (isString(value)) return /^\s*$/.test(value)
|
||||
return super.equals(value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export abstract class Drop {
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { Drop } from './drop'
|
||||
import { IComparable } from './icomparable'
|
||||
import { isObject, isString, isArray } from 'src/util/underscore'
|
||||
import { IDrop } from 'src/drop/idrop'
|
||||
|
||||
export class EmptyDrop extends Drop implements IDrop, IComparable {
|
||||
equals (value: any) {
|
||||
if (isString(value) || isArray(value)) return value.length === 0
|
||||
if (isObject(value)) return Object.keys(value).length === 0
|
||||
return false
|
||||
}
|
||||
gt () {
|
||||
return false
|
||||
}
|
||||
geq () {
|
||||
return false
|
||||
}
|
||||
lt () {
|
||||
return false
|
||||
}
|
||||
leq () {
|
||||
return false
|
||||
}
|
||||
value () {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { isFunction } from 'src/util/underscore'
|
||||
|
||||
export interface IComparable {
|
||||
equals: (rhs: any) => boolean
|
||||
gt: (rhs: any) => boolean
|
||||
geq: (rhs: any) => boolean
|
||||
lt: (rhs: any) => boolean
|
||||
leq: (rhs: any) => boolean
|
||||
}
|
||||
|
||||
export function isComparable (arg: any): arg is IComparable {
|
||||
return arg && isFunction(arg.equals)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Drop } from './drop'
|
||||
import { isFunction } from 'src/util/underscore'
|
||||
|
||||
export interface IDrop {
|
||||
value(): any
|
||||
}
|
||||
|
||||
export function isDrop (value: any): value is IDrop {
|
||||
return value instanceof Drop && isFunction((value as any).value)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { Drop } from './drop'
|
||||
import { IComparable } from './icomparable'
|
||||
import { isNil } from 'src/util/underscore'
|
||||
import { IDrop, isDrop } from 'src/drop/idrop'
|
||||
import { BlankDrop } from 'src/drop/blank-drop'
|
||||
|
||||
export class NullDrop extends Drop implements IDrop, IComparable {
|
||||
equals (value: any) {
|
||||
return isNil(isDrop(value) ? value.value() : value) || value instanceof BlankDrop
|
||||
}
|
||||
gt () {
|
||||
return false
|
||||
}
|
||||
geq () {
|
||||
return false
|
||||
}
|
||||
lt () {
|
||||
return false
|
||||
}
|
||||
leq () {
|
||||
return false
|
||||
}
|
||||
value () {
|
||||
return null
|
||||
}
|
||||
}
|
||||
+58
-16
@@ -2,14 +2,43 @@ import * as lexical from '../parser/lexical'
|
||||
import assert from '../util/assert'
|
||||
import Scope from 'src/scope/scope'
|
||||
import { range, last } from 'src/util/underscore'
|
||||
import { isComparable } from 'src/drop/icomparable'
|
||||
import { NullDrop } from 'src/drop/null-drop'
|
||||
import { EmptyDrop } from 'src/drop/empty-drop'
|
||||
import { BlankDrop } from 'src/drop/blank-drop'
|
||||
import { isDrop } from 'src/drop/idrop'
|
||||
|
||||
const operators = {
|
||||
'==': (l: any, r: any) => l === r,
|
||||
'!=': (l: any, r: any) => l !== r,
|
||||
'>': (l: any, r: any) => l !== null && r !== null && l > r,
|
||||
'<': (l: any, r: any) => l !== null && r !== null && l < r,
|
||||
'>=': (l: any, r: any) => l !== null && r !== null && l >= r,
|
||||
'<=': (l: any, r: any) => l !== null && r !== null && l <= r,
|
||||
const binaryOperators: {[key: string]: (lhs: any, rhs: any) => boolean} = {
|
||||
'==': (l: any, r: any) => {
|
||||
if (isComparable(l)) return l.equals(r)
|
||||
if (isComparable(r)) return r.equals(l)
|
||||
return l === r
|
||||
},
|
||||
'!=': (l: any, r: any) => {
|
||||
if (isComparable(l)) return !l.equals(r)
|
||||
if (isComparable(r)) return !r.equals(l)
|
||||
return l !== r
|
||||
},
|
||||
'>': (l: any, r: any) => {
|
||||
if (isComparable(l)) return l.gt(r)
|
||||
if (isComparable(r)) return r.lt(l)
|
||||
return l > r
|
||||
},
|
||||
'<': (l: any, r: any) => {
|
||||
if (isComparable(l)) return l.lt(r)
|
||||
if (isComparable(r)) return r.gt(l)
|
||||
return l < r
|
||||
},
|
||||
'>=': (l: any, r: any) => {
|
||||
if (isComparable(l)) return l.geq(r)
|
||||
if (isComparable(r)) return r.leq(l)
|
||||
return l >= r
|
||||
},
|
||||
'<=': (l: any, r: any) => {
|
||||
if (isComparable(l)) return l.leq(r)
|
||||
if (isComparable(r)) return r.geq(l)
|
||||
return l <= r
|
||||
},
|
||||
'contains': (l: any, r: any) => {
|
||||
if (!l) return false
|
||||
if (typeof l.indexOf !== 'function') return false
|
||||
@@ -19,41 +48,54 @@ const operators = {
|
||||
'or': (l: any, r: any) => isTruthy(l) || isTruthy(r)
|
||||
}
|
||||
|
||||
export function evalExp (exp: string, scope: Scope): any {
|
||||
assert(scope, 'unable to evalExp: scope undefined')
|
||||
export function parseExp (exp: string, scope: Scope): any {
|
||||
assert(scope, 'unable to parseExp: scope undefined')
|
||||
const operatorREs = lexical.operators
|
||||
let match
|
||||
for (let i = 0; i < operatorREs.length; i++) {
|
||||
const operatorRE = operatorREs[i]
|
||||
const expRE = new RegExp(`^(${lexical.quoteBalanced.source})(${operatorRE.source})(${lexical.quoteBalanced.source})$`)
|
||||
if ((match = exp.match(expRE))) {
|
||||
const l = evalExp(match[1], scope)
|
||||
const op = operators[match[2].trim()]
|
||||
const r = evalExp(match[3], scope)
|
||||
const l = parseExp(match[1], scope)
|
||||
const op = binaryOperators[match[2].trim()]
|
||||
const r = parseExp(match[3], scope)
|
||||
return op(l, r)
|
||||
}
|
||||
}
|
||||
|
||||
if ((match = exp.match(lexical.rangeLine))) {
|
||||
const low = evalValue(match[1], scope)
|
||||
const high = evalValue(match[2], scope)
|
||||
const low = parseValue(match[1], scope)
|
||||
const high = parseValue(match[2], scope)
|
||||
return range(low, high + 1)
|
||||
}
|
||||
|
||||
return evalValue(exp, scope)
|
||||
return parseValue(exp, scope)
|
||||
}
|
||||
|
||||
export function evalValue (str: string, scope: Scope) {
|
||||
export function evalExp (str: string, scope: Scope): any {
|
||||
const value = parseExp(str, scope)
|
||||
return isDrop(value) ? value.value() : value
|
||||
}
|
||||
|
||||
function parseValue (str: string, scope: Scope): any {
|
||||
if (!str) return null
|
||||
str = str.trim()
|
||||
|
||||
if (str === 'true') return true
|
||||
if (str === 'false') return false
|
||||
if (str === 'nil' || str === 'null') return new NullDrop()
|
||||
if (str === 'empty') return new EmptyDrop()
|
||||
if (str === 'blank') return new BlankDrop()
|
||||
if (!isNaN(Number(str))) return Number(str)
|
||||
if ((str[0] === '"' || str[0] === "'") && str[0] === last(str)) return str.slice(1, -1)
|
||||
return scope.get(str)
|
||||
}
|
||||
|
||||
export function evalValue (str: string, scope: Scope): any {
|
||||
const value = parseValue(str, scope)
|
||||
return isDrop(value) ? value.value() : value
|
||||
}
|
||||
|
||||
export function isTruthy (val: any): boolean {
|
||||
return !isFalsy(val)
|
||||
}
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
export default interface IContext {
|
||||
[key: string]: any;
|
||||
liquid_method_missing?: (key: string) => any; // eslint-disable-line
|
||||
}
|
||||
+12
-6
@@ -3,11 +3,17 @@ import { __assign } from 'tslib'
|
||||
import assert from '../util/assert'
|
||||
import { NormalizedFullOptions, applyDefault } from '../liquid-options'
|
||||
import BlockMode from './block-mode'
|
||||
import IContext from './icontext'
|
||||
|
||||
export type Context = {
|
||||
[key: string]: any
|
||||
liquid_method_missing?: (key: string) => any // eslint-disable-line
|
||||
to_liquid?: () => any // eslint-disable-line
|
||||
toLiquid?: () => any // eslint-disable-line
|
||||
}
|
||||
|
||||
export default class Scope {
|
||||
opts: NormalizedFullOptions
|
||||
contexts: Array<IContext>
|
||||
contexts: Array<Context>
|
||||
blocks: object = {}
|
||||
groups: {[key: string]: number} = {}
|
||||
blockMode: BlockMode = BlockMode.OUTPUT
|
||||
@@ -67,10 +73,10 @@ export default class Scope {
|
||||
}
|
||||
return null
|
||||
}
|
||||
private readProperty (obj: IContext, key: string) {
|
||||
private readProperty (obj: Context, key: string) {
|
||||
let val
|
||||
if (_.isNil(obj)) {
|
||||
val = undefined
|
||||
val = obj
|
||||
} else {
|
||||
obj = toLiquid(obj)
|
||||
val = key === 'size' ? readSize(obj) : obj[key]
|
||||
@@ -144,7 +150,7 @@ export default class Scope {
|
||||
}
|
||||
}
|
||||
|
||||
function toLiquid (obj: IContext) {
|
||||
function toLiquid (obj: Context) {
|
||||
if (_.isFunction(obj.to_liquid)) {
|
||||
return obj.to_liquid()
|
||||
}
|
||||
@@ -154,7 +160,7 @@ function toLiquid (obj: IContext) {
|
||||
return obj
|
||||
}
|
||||
|
||||
function readSize (obj: IContext) {
|
||||
function readSize (obj: Context) {
|
||||
if (!_.isNil(obj.size)) return obj.size
|
||||
if (_.isArray(obj) || _.isString(obj)) return obj.length
|
||||
return obj.size
|
||||
|
||||
@@ -6,11 +6,11 @@ const arrToStr = Array.prototype.toString
|
||||
* @param {any} value The value to check.
|
||||
* @return {Boolean} Returns true if value is a string, else false.
|
||||
*/
|
||||
export function isString (value: any) {
|
||||
export function isString (value: any): value is string {
|
||||
return toStr.call(value) === '[object String]'
|
||||
}
|
||||
|
||||
export function isFunction (value: any) {
|
||||
export function isFunction (value: any): value is Function {
|
||||
return typeof value === 'function'
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ export function stringify (value: any): string {
|
||||
}
|
||||
|
||||
function defaultToString (value: any): string {
|
||||
const cache: string[] = []
|
||||
const cache: any[] = []
|
||||
return JSON.stringify(value, (key, value) => {
|
||||
if (isObject(value)) {
|
||||
if (cache.indexOf(value) !== -1) {
|
||||
@@ -57,12 +57,12 @@ export function isNil (value: any): boolean {
|
||||
return value === null || value === undefined
|
||||
}
|
||||
|
||||
export function isArray (value: any): boolean {
|
||||
export function isArray (value: any): value is any[] {
|
||||
// be compatible with IE 8
|
||||
return toStr.call(value) === '[object Array]'
|
||||
}
|
||||
|
||||
export function isError (value: any): boolean {
|
||||
export function isError (value: any): value is Error {
|
||||
const signature = toStr.call(value)
|
||||
// [object XXXError]
|
||||
return signature.substr(-6, 5) === 'Error' ||
|
||||
@@ -102,7 +102,7 @@ export function last (arr: any[] | string): any | string {
|
||||
* @param {any} value The value to check.
|
||||
* @return {Boolean} Returns true if value is an object, else false.
|
||||
*/
|
||||
export function isObject (value: any): boolean {
|
||||
export function isObject (value: any): value is object {
|
||||
const type = typeof value
|
||||
return value !== null && (type === 'object' || type === 'function')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user