mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
fix: default filter not applied for empty array
This commit is contained in:
@@ -1,10 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title></title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -28,7 +28,7 @@ export function escapeOnce (str: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function newlineToBr (v: string) {
|
export function newlineToBr (v: string) {
|
||||||
return v.replace(/\n/g, '<br />')
|
return v.replace(/\n/g, '<br/>')
|
||||||
}
|
}
|
||||||
|
|
||||||
export function stripHtml (v: string) {
|
export function stripHtml (v: string) {
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import { isFalsy } from '../../render/boolean'
|
import { isFalsy } from '../../render/boolean'
|
||||||
import { toValue } from '../../util/underscore'
|
import { isArray, isString, toValue } from '../../util/underscore'
|
||||||
|
|
||||||
export function Default<T1, T2> (v: string | T1, arg: T2): string | T1 | T2 {
|
export function Default<T1, T2> (v: string | T1, arg: T2): string | T1 | T2 {
|
||||||
return isFalsy(toValue(v)) || v === '' ? arg : v
|
if (isArray(v) || isString(v)) return v.length ? v : arg
|
||||||
|
return isFalsy(toValue(v)) ? arg : v
|
||||||
}
|
}
|
||||||
export function json (v: any) {
|
export function json (v: any) {
|
||||||
return JSON.stringify(v)
|
return JSON.stringify(v)
|
||||||
|
|||||||
@@ -29,9 +29,9 @@ describe('filters/html', function () {
|
|||||||
'there\n' +
|
'there\n' +
|
||||||
'{% endcapture %}' +
|
'{% endcapture %}' +
|
||||||
'{{ string_with_newlines | newline_to_br }}'
|
'{{ string_with_newlines | newline_to_br }}'
|
||||||
const dst = '<br />' +
|
const dst = '<br/>' +
|
||||||
'Hello<br />' +
|
'Hello<br/>' +
|
||||||
'there<br />'
|
'there<br/>'
|
||||||
return test(src, dst)
|
return test(src, dst)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,19 +1,22 @@
|
|||||||
import { test } from '../../../stub/render'
|
import { Liquid } from '../../../../src/liquid'
|
||||||
|
import { expect } from 'chai'
|
||||||
|
|
||||||
describe('filters/object', function () {
|
describe('filters/object', function () {
|
||||||
|
const liquid = new Liquid()
|
||||||
describe('default', function () {
|
describe('default', function () {
|
||||||
it('false should use default', () => test('{{false | default: "a"}}', 'a'))
|
it('false should use default', async () => expect(await liquid.parseAndRender('{{false | default: "a"}}')).to.equal('a'))
|
||||||
it('empty string should use default', () => test('{{"" | default: "a"}}', 'a'))
|
it('empty string should use default', async () => expect(await liquid.parseAndRender('{{"" | default: "a"}}')).to.equal('a'))
|
||||||
it('non-empty string should not use default', () => test('{{" " | default: "a"}}', ' '))
|
it('empty array should use default', async () => expect(await liquid.parseAndRender('{{arr | default: "a"}}', { arr: [] })).to.equal('a'))
|
||||||
it('nil should use default', () => test('{{nil | default: "a"}}', 'a'))
|
it('non-empty string should not use default', async () => expect(await liquid.parseAndRender('{{" " | default: "a"}}')).to.equal(' '))
|
||||||
it('undefined should use default', () => test('{{not_defined | default: "a"}}', 'a'))
|
it('nil should use default', async () => expect(await liquid.parseAndRender('{{nil | default: "a"}}')).to.equal('a'))
|
||||||
it('true should not use default', () => test('{{true | default: "a"}}', 'true'))
|
it('undefined should use default', async () => expect(await liquid.parseAndRender('{{not_defined | default: "a"}}')).to.equal('a'))
|
||||||
it('0 should not use default', () => test('{{0 | default: "a"}}', '0'))
|
it('true should not use default', async () => expect(await liquid.parseAndRender('{{true | default: "a"}}')).to.equal('true'))
|
||||||
|
it('0 should not use default', async () => expect(await liquid.parseAndRender('{{0 | default: "a"}}')).to.equal('0'))
|
||||||
})
|
})
|
||||||
describe('json', function () {
|
describe('json', function () {
|
||||||
it('should stringify string', () => test('{{"foo" | json}}', '"foo"'))
|
it('should stringify string', async () => expect(await liquid.parseAndRender('{{"foo" | json}}')).to.equal('"foo"'))
|
||||||
it('should stringify number', () => test('{{2 | json}}', '2'))
|
it('should stringify number', async () => expect(await liquid.parseAndRender('{{2 | json}}')).to.equal('2'))
|
||||||
it('should stringify object', () => test('{{obj | json}}', '{"foo":"bar"}'))
|
it('should stringify object', async () => expect(await liquid.parseAndRender('{{obj | json}}', { obj: { foo: 'bar' } })).to.equal('{"foo":"bar"}'))
|
||||||
it('should stringify array', () => test('{{arr | json}}', '[-2,"a"]'))
|
it('should stringify array', async () => expect(await liquid.parseAndRender('{{arr | json}}', { arr: [-2, 'a'] })).to.equal('[-2,"a"]'))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user