feat: jsonify, inspect, to_integer, normalize_whitespace filters

This commit is contained in:
Yang Jun
2024-05-06 21:10:27 +08:00
committed by Jun Yang
parent ea0eab93d1
commit 842b45c96a
19 changed files with 258 additions and 17 deletions
+29
View File
@@ -30,4 +30,33 @@ describe('filters/object', function () {
expect(liquid.parseAndRenderSync('{{obj | json: 4}}', scope)).toBe(result)
})
})
describe('jsonify', function () {
it('should stringify string', async () => expect(await liquid.parseAndRender('{{"foo" | jsonify}}')).toBe('"foo"'))
})
describe('inspect', function () {
it('should inspect string', async () => expect(await liquid.parseAndRender('{{"foo" | inspect}}')).toBe('"foo"'))
it('should inspect object', () => {
const text = '{{foo | inspect}}'
const foo = { bar: 'bar' }
const expected = '{"bar":"bar"}'
return expect(liquid.parseAndRenderSync(text, { foo })).toBe(expected)
})
it('should inspect cyclic object', () => {
const text = '{{foo | inspect}}'
const foo: any = { bar: 'bar' }
foo.foo = foo
const expected = '{"bar":"bar","foo":"[Circular]"}'
return expect(liquid.parseAndRenderSync(text, { foo })).toBe(expected)
})
it('should support space argument', () => {
const text = '{{foo | inspect: 4}}'
const foo: any = { bar: 'bar' }
foo.foo = foo
const expected = '{\n "bar": "bar",\n "foo": "[Circular]"\n}'
return expect(liquid.parseAndRenderSync(text, { foo })).toBe(expected)
})
})
describe('to_integer', function () {
it('should stringify string', () => expect(liquid.parseAndRenderSync('{{ "123" | to_integer | json }}')).toBe('123'))
})
})
+10
View File
@@ -1,6 +1,8 @@
import { test } from '../../stub/render'
import { Liquid } from '../../../src/liquid'
describe('filters/string', function () {
const liquid = new Liquid()
describe('append', function () {
it('should return "-3abc" for -3, "abc"',
() => test('{{ -3 | append: "abc" }}', '-3abc'))
@@ -228,4 +230,12 @@ describe('filters/string', function () {
'Take my protein pills and put my helmet on')
})
})
describe('normalize_whitespace', () => {
it('should replace " \n " with " "', () => {
expect(liquid.parseAndRenderSync('{{ "a \n b" | normalize_whitespace }}')).toEqual('a b')
})
it('should replace multiple occurrences', () => {
expect(liquid.parseAndRenderSync('{{ "a \n b c" | normalize_whitespace }}')).toEqual('a b c')
})
})
})