Skip to content

Commit 7b87ea8

Browse files
committed
feat: JSON format by space in json filter
1 parent 8037f7b commit 7b87ea8

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

src/filters/misc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export function Default<T1 extends boolean, T2> (this: FilterImpl, value: T1, de
99
return isFalsy(value, this.context) ? defaultValue : value
1010
}
1111

12-
export function json (value: any) {
13-
return JSON.stringify(value)
12+
export function json (value: any, space = 0) {
13+
return JSON.stringify(value, null, space)
1414
}
1515

1616
export const raw = {

src/util/timezone-date.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('TimezoneDate', () => {
2626
})
2727
it('should support .toLocaleTimeString()', () => {
2828
const date = new TimezoneDate('2021-10-06T00:00:00.001+00:00', -480)
29-
expect(date.toLocaleTimeString('en-US')).toBe('8:00:00 AM')
29+
expect(date.toLocaleTimeString('en-US')).toMatch(/^8:00:00\sAM$/)
3030
expect(() => date.toLocaleDateString()).not.toThrow()
3131
})
3232
it('should support .toLocaleDateString()', () => {

test/integration/filters/misc.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,10 @@ describe('filters/object', function () {
2424
it('should stringify number', async () => expect(await liquid.parseAndRender('{{2 | json}}')).toBe('2'))
2525
it('should stringify object', async () => expect(await liquid.parseAndRender('{{obj | json}}', { obj: { foo: 'bar' } })).toBe('{"foo":"bar"}'))
2626
it('should stringify array', async () => expect(await liquid.parseAndRender('{{arr | json}}', { arr: [-2, 'a'] })).toBe('[-2,"a"]'))
27+
it('should support space', () => {
28+
const scope = { obj: { foo: 'foo', bar: 'bar' } }
29+
const result = '{\n "foo": "foo",\n "bar": "bar"\n}'
30+
expect(liquid.parseAndRenderSync('{{obj | json: 4}}', scope)).toBe(result)
31+
})
2732
})
2833
})

0 commit comments

Comments
 (0)