Skip to content

Commit

Permalink
fix: context for group_by_exp/where_exp/find_exp, #790
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Jan 19, 2025
1 parent b070594 commit a5070af
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/filters/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ export function * where_exp<T extends object> (this: FilterImpl, arr: T[], itemN
const array = toArray(arr)
this.context.memoryLimit.use(array.length)
for (const item of array) {
const value = yield keyTemplate.value(this.context.spawn({ [itemName]: item }))
this.context.push({ [itemName]: item })
const value = yield keyTemplate.value(this.context)
this.context.pop()
if (value) filtered.push(item)
}
return filtered
Expand All @@ -165,7 +167,9 @@ export function * group_by_exp<T extends object> (this: FilterImpl, arr: T[], it
arr = toEnumerable(arr)
this.context.memoryLimit.use(arr.length)
for (const item of arr) {
const key = yield keyTemplate.value(this.context.spawn({ [itemName]: item }))
this.context.push({ [itemName]: item })
const key = yield keyTemplate.value(this.context)
this.context.pop()
if (!map.has(key)) map.set(key, [])
map.get(key).push(item)
}
Expand All @@ -185,7 +189,9 @@ export function * find_exp<T extends object> (this: FilterImpl, arr: T[], itemNa
const predicate = new Value(stringify(exp), this.liquid)
const array = toArray(arr)
for (const item of array) {
const value = yield predicate.value(this.context.spawn({ [itemName]: item }))
this.context.push({ [itemName]: item })
const value = yield predicate.value(this.context)
this.context.pop()
if (value) return item
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/integration/filters/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,20 @@ describe('filters/array', function () {
- Garlic press
`)
})
it('should be aware context', function () {
const tpl = `{% assign kitchen_products = products | where_exp: "item", "item.type == target" %}
Kitchen products:
{% for product in kitchen_products -%}
- {{ product.title }}
{% endfor %}`
const scope = { products, target: 'kitchen' }
const html = `
Kitchen products:
- Spatula
- Garlic press
`
return test(tpl, scope, html)
})
})
describe('group_by', function () {
const members = [
Expand Down

0 comments on commit a5070af

Please sign in to comment.