-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: nil/null/empty/blank literals, resolves #102
- Loading branch information
Showing
19 changed files
with
355 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export abstract class Drop { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 '' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Liquid from '../..' | ||
import { expect, use } from 'chai' | ||
import * as chaiAsPromised from 'chai-as-promised' | ||
|
||
use(chaiAsPromised) | ||
|
||
describe('drop', function () { | ||
var engine: Liquid | ||
beforeEach(function () { | ||
engine = new Liquid() | ||
}) | ||
it('should test blank strings', async function () { | ||
const src = ` | ||
{% unless settings.fp_heading == blank %} | ||
<h1>{{ settings.fp_heading }}</h1> | ||
{% endunless %}` | ||
var ctx = { settings: { fp_heading: '' } } | ||
const html = await engine.parseAndRender(src, ctx) | ||
return expect(html).to.match(/^\s+$/) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.