Skip to content

Commit

Permalink
fix: default filter is not working with an empty string, #122
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Apr 26, 2019
1 parent 87489f6 commit 6075c0a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/builtin/filters/object.ts
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 7 additions & 2 deletions test/integration/builtin/filters/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
})
})

0 comments on commit 6075c0a

Please sign in to comment.