mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 14:00:39 -07:00
feat: nil/null/empty/blank literals, resolves #102
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export abstract class Drop {
|
||||
}
|
||||
@@ -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 ''
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user