refactor: IDrop interface => abstract method

This commit is contained in:
harttle
2019-02-28 00:05:08 +08:00
parent d5b9916b2f
commit b69c3a3203
6 changed files with 12 additions and 20 deletions
+2 -2
View File
@@ -1,11 +1,11 @@
import { isNil, isString } from '../util/underscore' import { isNil, isString } from '../util/underscore'
import { isDrop } from '../drop/idrop' import { Drop } from '../drop/drop'
import { EmptyDrop } from '../drop/empty-drop' import { EmptyDrop } from '../drop/empty-drop'
export class BlankDrop extends EmptyDrop { export class BlankDrop extends EmptyDrop {
equals (value: any) { equals (value: any) {
if (value === false) return true 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) if (isString(value)) return /^\s*$/.test(value)
return super.equals(value) return super.equals(value)
} }
+4
View File
@@ -1,2 +1,6 @@
export abstract class Drop { export abstract class Drop {
abstract valueOf(): any;
liquid_method_missing (name: string) { // eslint-disable-line
}
} }
+1 -2
View File
@@ -1,9 +1,8 @@
import { Drop } from './drop' import { Drop } from './drop'
import { IComparable } from './icomparable' import { IComparable } from './icomparable'
import { isObject, isString, isArray } from '../util/underscore' 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) { equals (value: any) {
if (isString(value) || isArray(value)) return value.length === 0 if (isString(value) || isArray(value)) return value.length === 0
if (isObject(value)) return Object.keys(value).length === 0 if (isObject(value)) return Object.keys(value).length === 0
-10
View File
@@ -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)
}
+2 -3
View File
@@ -1,12 +1,11 @@
import { Drop } from './drop' import { Drop } from './drop'
import { IComparable } from './icomparable' import { IComparable } from './icomparable'
import { isNil } from '../util/underscore' import { isNil } from '../util/underscore'
import { IDrop, isDrop } from '../drop/idrop'
import { BlankDrop } from '../drop/blank-drop' import { BlankDrop } from '../drop/blank-drop'
export class NullDrop extends Drop implements IDrop, IComparable { export class NullDrop extends Drop implements IComparable {
equals (value: any) { 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 () { gt () {
return false return false
+3 -3
View File
@@ -6,7 +6,7 @@ import { isComparable } from '../drop/icomparable'
import { NullDrop } from '../drop/null-drop' import { NullDrop } from '../drop/null-drop'
import { EmptyDrop } from '../drop/empty-drop' import { EmptyDrop } from '../drop/empty-drop'
import { BlankDrop } from '../drop/blank-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} = { const binaryOperators: {[key: string]: (lhs: any, rhs: any) => boolean} = {
'==': (l: any, r: any) => { '==': (l: any, r: any) => {
@@ -74,7 +74,7 @@ export function parseExp (exp: string, scope: Scope): any {
export function evalExp (str: string, scope: Scope): any { export function evalExp (str: string, scope: Scope): any {
const value = parseExp(str, scope) 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 { 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 { export function evalValue (str: string, scope: Scope): any {
const value = parseValue(str, scope) const value = parseValue(str, scope)
return isDrop(value) ? value.valueOf() : value return value instanceof Drop ? value.valueOf() : value
} }
export function isTruthy (val: any): boolean { export function isTruthy (val: any): boolean {