fix: comparison for empty/nil, fixes #321

This commit is contained in:
Jun Yang
2021-03-13 17:14:08 +08:00
parent aff9976042
commit 99d14e76d7
6 changed files with 36 additions and 11 deletions
+3 -1
View File
@@ -1,9 +1,11 @@
import { Drop } from './drop'
import { Comparable } from './comparable'
import { isObject, isString, isArray } from '../util/underscore'
import { isObject, isString, isArray, toValue } from '../util/underscore'
export class EmptyDrop extends Drop implements Comparable {
public equals (value: any) {
if (value instanceof EmptyDrop) return false
value = toValue(value)
if (isString(value) || isArray(value)) return value.length === 0
if (isObject(value)) return Object.keys(value).length === 0
return false
+1 -2
View File
@@ -1,11 +1,10 @@
import { Drop } from './drop'
import { Comparable } from './comparable'
import { isNil, toValue } from '../util/underscore'
import { BlankDrop } from '../drop/blank-drop'
export class NullDrop extends Drop implements Comparable {
public equals (value: any) {
return isNil(toValue(value)) || value instanceof BlankDrop
return isNil(toValue(value))
}
public gt () {
return false
+3 -2
View File
@@ -2,11 +2,12 @@ import { NullDrop } from '../drop/null-drop'
import { EmptyDrop } from '../drop/empty-drop'
import { BlankDrop } from '../drop/blank-drop'
const nil = new NullDrop()
export const literalValues = {
'true': true,
'false': false,
'nil': new NullDrop(),
'null': new NullDrop(),
'nil': nil,
'null': nil,
'empty': new EmptyDrop(),
'blank': new BlankDrop()
}