Skip to content

Commit

Permalink
feat: support json filter, closes #192
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Mar 3, 2020
1 parent 152778b commit aa27a6c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/builtin/filters/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
6 changes: 6 additions & 0 deletions test/integration/builtin/filters/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]'))
})
})

0 comments on commit aa27a6c

Please sign in to comment.