From aa27a6cd7b0e820972f76cc4ea05020b2be7db99 Mon Sep 17 00:00:00 2001 From: harttle Date: Tue, 3 Mar 2020 12:38:47 +0800 Subject: [PATCH] feat: support json filter, closes #192 --- src/builtin/filters/object.ts | 3 +++ test/integration/builtin/filters/object.ts | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/src/builtin/filters/object.ts b/src/builtin/filters/object.ts index 6631f30d63..97d3531b96 100644 --- a/src/builtin/filters/object.ts +++ b/src/builtin/filters/object.ts @@ -4,5 +4,8 @@ import { toValue } from '../../util/underscore' export default { 'default': function (v: string | T1, arg: T2): string | T1 | T2 { return isFalsy(toValue(v)) || v === '' ? arg : v + }, + 'json': function (v: any) { + return JSON.stringify(v) } } diff --git a/test/integration/builtin/filters/object.ts b/test/integration/builtin/filters/object.ts index 78f97e59f4..23dfe0637f 100644 --- a/test/integration/builtin/filters/object.ts +++ b/test/integration/builtin/filters/object.ts @@ -10,4 +10,10 @@ describe('filters/object', function () { it('true should not use default', () => test('{{true | default: "a"}}', 'true')) it('0 should not use default', () => test('{{0 | default: "a"}}', '0')) }) + describe('json', function () { + it('should stringify string', () => test('{{"foo" | json}}', '"foo"')) + it('should stringify number', () => test('{{2 | json}}', '2')) + it('should stringify object', () => test('{{obj | json}}', '{"foo":"bar"}')) + it('should stringify array', () => test('{{arr | json}}', '[-2,"a"]')) + }) })