Skip to content

Commit

Permalink
feat: JSON format by space in json filter
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Mar 21, 2023
1 parent 8037f7b commit 7b87ea8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/filters/misc.ts
Expand Up @@ -9,8 +9,8 @@ export function Default<T1 extends boolean, T2> (this: FilterImpl, value: T1, de
return isFalsy(value, this.context) ? defaultValue : value
}

export function json (value: any) {
return JSON.stringify(value)
export function json (value: any, space = 0) {
return JSON.stringify(value, null, space)
}

export const raw = {
Expand Down
2 changes: 1 addition & 1 deletion src/util/timezone-date.spec.ts
Expand Up @@ -26,7 +26,7 @@ describe('TimezoneDate', () => {
})
it('should support .toLocaleTimeString()', () => {
const date = new TimezoneDate('2021-10-06T00:00:00.001+00:00', -480)
expect(date.toLocaleTimeString('en-US')).toBe('8:00:00 AM')
expect(date.toLocaleTimeString('en-US')).toMatch(/^8:00:00\sAM$/)
expect(() => date.toLocaleDateString()).not.toThrow()
})
it('should support .toLocaleDateString()', () => {
Expand Down
5 changes: 5 additions & 0 deletions test/integration/filters/misc.spec.ts
Expand Up @@ -24,5 +24,10 @@ describe('filters/object', function () {
it('should stringify number', async () => expect(await liquid.parseAndRender('{{2 | json}}')).toBe('2'))
it('should stringify object', async () => expect(await liquid.parseAndRender('{{obj | json}}', { obj: { foo: 'bar' } })).toBe('{"foo":"bar"}'))
it('should stringify array', async () => expect(await liquid.parseAndRender('{{arr | json}}', { arr: [-2, 'a'] })).toBe('[-2,"a"]'))
it('should support space', () => {
const scope = { obj: { foo: 'foo', bar: 'bar' } }
const result = '{\n "foo": "foo",\n "bar": "bar"\n}'
expect(liquid.parseAndRenderSync('{{obj | json: 4}}', scope)).toBe(result)
})
})
})

0 comments on commit 7b87ea8

Please sign in to comment.