mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-14 03:40:38 -07:00
feat: async filters, closes #232
This commit is contained in:
@@ -20,6 +20,6 @@ export class Filter {
|
||||
if (isKeyValuePair(arg)) argv.push([arg[0], yield evalToken(arg[1], context)])
|
||||
else argv.push(yield evalToken(arg, context))
|
||||
}
|
||||
return this.impl.apply({ context }, [value, ...argv])
|
||||
return yield this.impl.apply({ context }, [value, ...argv])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,13 +45,13 @@ describe('.parseAndRender()', function () {
|
||||
const result2 = await engine.render(template, ctx)
|
||||
expect(result2).to.equal('1,2')
|
||||
})
|
||||
it('should render filters', async function () {
|
||||
it('should support the "join" filter', async function () {
|
||||
var ctx = { names: ['alice', 'bob'] }
|
||||
var template = engine.parse('<p>{{names | join: ","}}</p>')
|
||||
const html = await engine.render(template, ctx)
|
||||
return expect(html).to.equal('<p>alice,bob</p>')
|
||||
})
|
||||
it('should render accessive filters', async function () {
|
||||
it('should support the "first" filter', async function () {
|
||||
var src = '{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}' +
|
||||
'{{ my_array | first }}'
|
||||
const html = await engine.parseAndRender(src)
|
||||
|
||||
@@ -4,21 +4,33 @@ import { Liquid } from '../../../src/liquid'
|
||||
describe('liquid#registerFilter()', function () {
|
||||
const liquid = new Liquid()
|
||||
|
||||
describe('object arguments', function () {
|
||||
describe('key-value arguments', function () {
|
||||
liquid.registerFilter('obj_test', function (...args) {
|
||||
return JSON.stringify(args)
|
||||
})
|
||||
it('should support object', async () => {
|
||||
it('should support key-value arguments', async () => {
|
||||
const src = `{{ "a" | obj_test: k1: "v1", k2: foo }}`
|
||||
const dst = '["a",["k1","v1"],["k2","bar"]]'
|
||||
const html = await liquid.parseAndRender(src, { foo: 'bar' })
|
||||
return expect(html).to.equal(dst)
|
||||
})
|
||||
it('should support mixed object', async () => {
|
||||
it('should support mixed arguments', async () => {
|
||||
const src = `{{ "a" | obj_test: "something", k1: "v1", k2: foo }}`
|
||||
const dst = '["a","something",["k1","v1"],["k2","bar"]]'
|
||||
const html = await liquid.parseAndRender(src, { foo: 'bar' })
|
||||
return expect(html).to.equal(dst)
|
||||
})
|
||||
})
|
||||
|
||||
describe('async filters', () => {
|
||||
liquid.registerFilter('get_user_data', function (userId) {
|
||||
return Promise.resolve({ userId, userName: userId.toUpperCase() })
|
||||
})
|
||||
it('should support async filter', async () => {
|
||||
const src = `{{ userId | get_user_data | json }}`
|
||||
const dst = '{"userId":"alice","userName":"ALICE"}'
|
||||
const html = await liquid.parseAndRender(src, { userId: 'alice' })
|
||||
return expect(html).to.equal(dst)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user