diff --git a/src/builtin/filters/object.ts b/src/builtin/filters/object.ts index 6631f30d6..97d3531b9 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 78f97e59f..23dfe0637 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"]')) + }) })