feat: nil/null/empty/blank literals, resolves #102

This commit is contained in:
harttle
2019-02-24 21:50:01 +08:00
parent 54b2adce12
commit 88c9e96392
19 changed files with 355 additions and 52 deletions
+12
View File
@@ -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)
}
}
+2
View File
@@ -0,0 +1,2 @@
export abstract class Drop {
}
+27
View File
@@ -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 ''
}
}
+13
View File
@@ -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)
}
+10
View File
@@ -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)
}
+26
View File
@@ -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
}
}