mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
refactor: IDrop interface => abstract method
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import { isNil, isString } from '../util/underscore'
|
||||
import { isDrop } from '../drop/idrop'
|
||||
import { Drop } from '../drop/drop'
|
||||
import { EmptyDrop } from '../drop/empty-drop'
|
||||
|
||||
export class BlankDrop extends EmptyDrop {
|
||||
equals (value: any) {
|
||||
if (value === false) return true
|
||||
if (isNil(isDrop(value) ? value.valueOf() : value)) return true
|
||||
if (isNil(value instanceof Drop ? value.valueOf() : value)) return true
|
||||
if (isString(value)) return /^\s*$/.test(value)
|
||||
return super.equals(value)
|
||||
}
|
||||
|
||||
@@ -1,2 +1,6 @@
|
||||
export abstract class Drop {
|
||||
abstract valueOf(): any;
|
||||
|
||||
liquid_method_missing (name: string) { // eslint-disable-line
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { Drop } from './drop'
|
||||
import { IComparable } from './icomparable'
|
||||
import { isObject, isString, isArray } from '../util/underscore'
|
||||
import { IDrop } from '../drop/idrop'
|
||||
|
||||
export class EmptyDrop extends Drop implements IDrop, IComparable {
|
||||
export class EmptyDrop extends Drop implements IComparable {
|
||||
equals (value: any) {
|
||||
if (isString(value) || isArray(value)) return value.length === 0
|
||||
if (isObject(value)) return Object.keys(value).length === 0
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
import { Drop } from './drop'
|
||||
import { isFunction } from '../util/underscore'
|
||||
|
||||
export interface IDrop {
|
||||
valueOf(): any
|
||||
}
|
||||
|
||||
export function isDrop (value: any): value is IDrop {
|
||||
return value instanceof Drop && isFunction((value as any).valueOf)
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
import { Drop } from './drop'
|
||||
import { IComparable } from './icomparable'
|
||||
import { isNil } from '../util/underscore'
|
||||
import { IDrop, isDrop } from '../drop/idrop'
|
||||
import { BlankDrop } from '../drop/blank-drop'
|
||||
|
||||
export class NullDrop extends Drop implements IDrop, IComparable {
|
||||
export class NullDrop extends Drop implements IComparable {
|
||||
equals (value: any) {
|
||||
return isNil(isDrop(value) ? value.valueOf() : value) || value instanceof BlankDrop
|
||||
return isNil(value instanceof Drop ? value.valueOf() : value) || value instanceof BlankDrop
|
||||
}
|
||||
gt () {
|
||||
return false
|
||||
|
||||
@@ -6,7 +6,7 @@ import { isComparable } from '../drop/icomparable'
|
||||
import { NullDrop } from '../drop/null-drop'
|
||||
import { EmptyDrop } from '../drop/empty-drop'
|
||||
import { BlankDrop } from '../drop/blank-drop'
|
||||
import { isDrop } from '../drop/idrop'
|
||||
import { Drop } from '../drop/drop'
|
||||
|
||||
const binaryOperators: {[key: string]: (lhs: any, rhs: any) => boolean} = {
|
||||
'==': (l: any, r: any) => {
|
||||
@@ -74,7 +74,7 @@ export function parseExp (exp: string, scope: Scope): any {
|
||||
|
||||
export function evalExp (str: string, scope: Scope): any {
|
||||
const value = parseExp(str, scope)
|
||||
return isDrop(value) ? value.valueOf() : value
|
||||
return value instanceof Drop ? value.valueOf() : value
|
||||
}
|
||||
|
||||
function parseValue (str: string, scope: Scope): any {
|
||||
@@ -93,7 +93,7 @@ function parseValue (str: string, scope: Scope): any {
|
||||
|
||||
export function evalValue (str: string, scope: Scope): any {
|
||||
const value = parseValue(str, scope)
|
||||
return isDrop(value) ? value.valueOf() : value
|
||||
return value instanceof Drop ? value.valueOf() : value
|
||||
}
|
||||
|
||||
export function isTruthy (val: any): boolean {
|
||||
|
||||
Reference in New Issue
Block a user