From 89636736a454a24f0a073640205a20495969d982 Mon Sep 17 00:00:00 2001 From: harttle Date: Thu, 8 Dec 2016 23:25:38 +0800 Subject: [PATCH] robust date filter --- filters.js | 2 +- test/filters.js | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/filters.js b/filters.js index 0ed743611..3ea8e75b8 100644 --- a/filters.js +++ b/filters.js @@ -23,7 +23,7 @@ var filters = { 'ceil': v => Math.ceil(v), 'date': (v, arg) => { if (v === 'now') v = new Date(); - return strftime(v, arg); + return v instanceof Date ? strftime(v, arg) : ''; }, 'default': (v, arg) => v || arg, 'divided_by': (v, arg) => Math.floor(v / arg), diff --git a/test/filters.js b/test/filters.js index 95033fd23..0aef8554b 100644 --- a/test/filters.js +++ b/test/filters.js @@ -51,6 +51,9 @@ describe('filters', function() { it('should create a new Date when given "now"', function() { return test('{{ "now" | date: "%Y"}}', (new Date).getFullYear().toString()); }); + it('should render as empty string when invalid', function() { + return test('{{ "" | date: "%Y"}}', ""); + }); }); describe('default', function() {