From 6075c0a54f5ca2f995f7f86c76d1ff7d419ec96b Mon Sep 17 00:00:00 2001 From: harttle Date: Sat, 27 Apr 2019 00:26:17 +0800 Subject: [PATCH] fix: `default` filter is not working with an empty string, #122 --- src/builtin/filters/object.ts | 4 ++-- test/integration/builtin/filters/object.ts | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/builtin/filters/object.ts b/src/builtin/filters/object.ts index c8d86abfc..4aa6c8ec1 100644 --- a/src/builtin/filters/object.ts +++ b/src/builtin/filters/object.ts @@ -1,5 +1,5 @@ -import { isTruthy } from '../../render/syntax' +import { isFalsy } from '../../render/syntax' export default { - 'default': (v: T1, arg: T2): T1 | T2 => isTruthy(v) ? v : arg + 'default': (v: string | T1, arg: T2): string | T1 | T2 => isFalsy(v) || v === '' ? arg : v } diff --git a/test/integration/builtin/filters/object.ts b/test/integration/builtin/filters/object.ts index a135e4b82..78f97e59f 100644 --- a/test/integration/builtin/filters/object.ts +++ b/test/integration/builtin/filters/object.ts @@ -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')) }) })