feat: introduce where_exp filter from Jekyll

This commit is contained in:
Yang Jun
2024-04-28 23:17:01 +08:00
committed by Jun Yang
parent 303a0d44b5
commit 8c7cef9f95
7 changed files with 109 additions and 2 deletions
+10
View File
@@ -102,6 +102,16 @@ export function * where<T extends object> (this: FilterImpl, arr: T[], property:
})
}
export function * where_exp<T extends object> (this: FilterImpl, arr: T[], itemName: string, exp: string): IterableIterator<unknown> {
const filtered: unknown[] = []
const keyTemplate = new Value(stringify(exp), this.liquid)
for (const item of toArray(arr)) {
const value = yield keyTemplate.value(new Context({ [itemName]: item }))
if (value) filtered.push(item)
}
return filtered
}
export function * group_by<T extends object> (arr: T[], property: string): IterableIterator<unknown> {
const map = new Map()
arr = toArray(arr)