Skip to content

Commit

Permalink
Change semantics of jekyllStyle, add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brunodccarvalho committed Feb 12, 2025
1 parent 72fb19e commit 77dfdb4
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 152 deletions.
33 changes: 18 additions & 15 deletions src/filters/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,14 @@ export function slice<T> (this: FilterImpl, v: T[] | string, begin: number, leng
return v.slice(begin, begin + length)
}

function contextMatcher (this: FilterImpl, expected: any): (v: any) => boolean {
if (expected === undefined) return (v: any) => isTruthy(v, this.context)
return this.context.opts.jekyllWhere
? (v: any) => EmptyDrop.is(expected) ? equals(v, expected) : (isArray(v) ? arrayIncludes(v, expected) : equals(v, expected))
: (v: any) => equals(v, expected)
function expectedMatcher (this: FilterImpl, expected: any): (v: any) => boolean {
if (this.context.opts.jekyllWhere) {
return (v: any) => EmptyDrop.is(expected) ? equals(v, expected) : (isArray(v) ? arrayIncludes(v, expected) : equals(v, expected))
} else if (expected === undefined) {
return (v: any) => isTruthy(v, this.context)
} else {
return (v: any) => equals(v, expected)
}
}

function * filter<T extends object> (this: FilterImpl, include: boolean, arr: T[], property: string, expected: any): IterableIterator<unknown> {
Expand All @@ -132,18 +135,10 @@ function * filter<T extends object> (this: FilterImpl, include: boolean, arr: T[
for (const item of arr) {
values.push(yield evalToken(token, this.context.spawn(item)))
}
const matcher = contextMatcher.call(this, expected)
const matcher = expectedMatcher.call(this, expected)
return arr.filter((_, i) => matcher(values[i]) === include)
}

export function * where<T extends object> (this: FilterImpl, arr: T[], property: string, expected?: any): IterableIterator<unknown> {
return yield * filter.call(this, true, arr, property, expected)
}

export function * reject<T extends object> (this: FilterImpl, arr: T[], property: string, expected?: any): IterableIterator<unknown> {
return yield * filter.call(this, false, arr, property, expected)
}

function * filter_exp<T extends object> (this: FilterImpl, include: boolean, arr: T[], itemName: string, exp: string): IterableIterator<unknown> {
const filtered: unknown[] = []
const keyTemplate = new Value(stringify(exp), this.liquid)
Expand All @@ -158,6 +153,14 @@ function * filter_exp<T extends object> (this: FilterImpl, include: boolean, arr
return filtered
}

export function * where<T extends object> (this: FilterImpl, arr: T[], property: string, expected?: any): IterableIterator<unknown> {
return yield * filter.call(this, true, arr, property, expected)
}

export function * reject<T extends object> (this: FilterImpl, arr: T[], property: string, expected?: any): IterableIterator<unknown> {
return yield * filter.call(this, false, arr, property, expected)
}

export function * where_exp<T extends object> (this: FilterImpl, arr: T[], itemName: string, exp: string): IterableIterator<unknown> {
return yield * filter_exp.call(this, true, arr, itemName, exp)
}
Expand Down Expand Up @@ -197,7 +200,7 @@ export function * group_by_exp<T extends object> (this: FilterImpl, arr: T[], it
function * search<T extends object> (this: FilterImpl, arr: T[], property: string, expected: string): IterableIterator<unknown> {
const token = new Tokenizer(stringify(property)).readScopeValue()
const array = toArray(arr)
const matcher = contextMatcher.call(this, expected)
const matcher = expectedMatcher.call(this, expected)
for (let index = 0; index < array.length; index++) {
const value = yield evalToken(token, this.context.spawn(array[index]))
if (matcher(value)) return [index, array[index]]
Expand Down
Loading

0 comments on commit 77dfdb4

Please sign in to comment.