mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
feat: support json filter, closes #192
This commit is contained in:
@@ -4,5 +4,8 @@ import { toValue } from '../../util/underscore'
|
|||||||
export default {
|
export default {
|
||||||
'default': function<T1, T2> (v: string | T1, arg: T2): string | T1 | T2 {
|
'default': function<T1, T2> (v: string | T1, arg: T2): string | T1 | T2 {
|
||||||
return isFalsy(toValue(v)) || v === '' ? arg : v
|
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('true should not use default', () => test('{{true | default: "a"}}', 'true'))
|
||||||
it('0 should not use default', () => test('{{0 | default: "a"}}', '0'))
|
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"]'))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user