fix: default filter is not working with an empty string, #122

This commit is contained in:
harttle
2019-04-27 00:26:17 +08:00
parent 87489f6098
commit 6075c0a54f
2 changed files with 9 additions and 4 deletions
+2 -2
View File
@@ -1,5 +1,5 @@
import { isTruthy } from '../../render/syntax'
import { isFalsy } from '../../render/syntax'
export default {
'default': <T1, T2>(v: T1, arg: T2): T1 | T2 => isTruthy(v) ? v : arg
'default': <T1, T2>(v: string | T1, arg: T2): string | T1 | T2 => isFalsy(v) || v === '' ? arg : v
}
+7 -2
View File
@@ -2,7 +2,12 @@ import { test } from '../../../stub/render'
describe('filters/object', function () {
describe('default', function () {
it('should use default when falsy', () => test('{{false |default: "a"}}', 'a'))
it('should not use default when truthy', () => test('{{true |default: "a"}}', 'true'))
it('false should use default', () => test('{{false | default: "a"}}', 'a'))
it('empty string should use default', () => test('{{"" | default: "a"}}', 'a'))
it('non-empty string should not use default', () => test('{{" " | default: "a"}}', ' '))
it('nil should use default', () => test('{{nil | default: "a"}}', 'a'))
it('undefined should use default', () => test('{{not_defined | default: "a"}}', 'a'))
it('true should not use default', () => test('{{true | default: "a"}}', 'true'))
it('0 should not use default', () => test('{{0 | default: "a"}}', '0'))
})
})