feat: support json filter, closes #192

This commit is contained in:
harttle
2020-03-04 03:42:18 +08:00
parent 152778b6e3
commit aa27a6cd7b
2 changed files with 9 additions and 0 deletions
+3
View File
@@ -4,5 +4,8 @@ import { toValue } from '../../util/underscore'
export default {
'default': function<T1, T2> (v: string | T1, arg: T2): string | T1 | T2 {
return isFalsy(toValue(v)) || v === '' ? arg : v
},
'json': function (v: any) {
return JSON.stringify(v)
}
}
@@ -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"]'))
})
})