mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-17 05:10:40 -07:00
refactor: strictly typed
This commit is contained in:
+16
-18
@@ -1,23 +1,25 @@
|
||||
import * as lexical from '../parser/lexical'
|
||||
import assert from '../util/assert'
|
||||
import Scope from 'src/scope/scope'
|
||||
import { range } from 'src/util/underscore'
|
||||
|
||||
const operators = {
|
||||
'==': (l, r) => l === r,
|
||||
'!=': (l, r) => l !== r,
|
||||
'>': (l, r) => l !== null && r !== null && l > r,
|
||||
'<': (l, r) => l !== null && r !== null && l < r,
|
||||
'>=': (l, r) => l !== null && r !== null && l >= r,
|
||||
'<=': (l, r) => l !== null && r !== null && l <= r,
|
||||
'contains': (l, r) => {
|
||||
'==': (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,
|
||||
'contains': (l: any, r: any) => {
|
||||
if (!l) return false
|
||||
if (typeof l.indexOf !== 'function') return false
|
||||
return l.indexOf(r) > -1
|
||||
},
|
||||
'and': (l, r) => isTruthy(l) && isTruthy(r),
|
||||
'or': (l, r) => isTruthy(l) || isTruthy(r)
|
||||
'and': (l: any, r: any) => isTruthy(l) && isTruthy(r),
|
||||
'or': (l: any, r: any) => isTruthy(l) || isTruthy(r)
|
||||
}
|
||||
|
||||
export function evalExp (exp, scope) {
|
||||
export function evalExp (exp: string, scope: Scope): any {
|
||||
assert(scope, 'unable to evalExp: scope undefined')
|
||||
const operatorREs = lexical.operators
|
||||
let match
|
||||
@@ -35,17 +37,13 @@ export function evalExp (exp, scope) {
|
||||
if ((match = exp.match(lexical.rangeLine))) {
|
||||
const low = evalValue(match[1], scope)
|
||||
const high = evalValue(match[2], scope)
|
||||
const range = []
|
||||
for (let j = low; j <= high; j++) {
|
||||
range.push(j)
|
||||
}
|
||||
return range
|
||||
return range(low, high + 1)
|
||||
}
|
||||
|
||||
return evalValue(exp, scope)
|
||||
}
|
||||
|
||||
export function evalValue (str, scope) {
|
||||
export function evalValue (str: string, scope: Scope) {
|
||||
str = str && str.trim()
|
||||
if (!str) return undefined
|
||||
|
||||
@@ -58,10 +56,10 @@ export function evalValue (str, scope) {
|
||||
throw new TypeError(`cannot eval '${str}' as value`)
|
||||
}
|
||||
|
||||
export function isTruthy (val) {
|
||||
export function isTruthy (val: any): boolean {
|
||||
return !isFalsy(val)
|
||||
}
|
||||
|
||||
export function isFalsy (val) {
|
||||
export function isFalsy (val: any): boolean {
|
||||
return val === false || undefined === val || val === null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user