Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Rely on equal for computing contains #668

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/render/expression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ describe('Expression', function () {
const ctx = new Context({ x: 'XXX', X: new TemplateDrop() })
expect(await toPromise(create('x contains X').evaluate(ctx, false))).toBe(true)
})
it('should support Drops for "x contains "x"" when x is an array', async () => {
class TemplateDrop extends Drop {
valueOf () { return 'X' }
}
const ctx = new Context({ x: [new TemplateDrop()], X: 'X' })
expect(await toPromise(create('x contains X').evaluate(ctx, false))).toBe(true)
})
it('should support Drops for "x contains "x"" when x is an array on both operands', async () => {
class TemplateDrop extends Drop {
valueOf () { return 'X' }
}
const ctx = new Context({ x: [new TemplateDrop()], X: new TemplateDrop() })
expect(await toPromise(create('x contains X').evaluate(ctx, false))).toBe(true)
})
it('should support value and !=', async function () {
const ctx = new Context({ empty: '' })
expect(await toPromise(create('empty and empty != ""').evaluate(ctx, false))).toBe(false)
Expand Down
9 changes: 5 additions & 4 deletions src/render/operator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { isComparable } from '../drop/comparable'
import { Context } from '../context'
import { isFunction, toValue } from '../util'
import { toValue } from '../util'
import { isFalsy, isTruthy } from '../render/boolean'
import { isArray } from '../util/underscore'
import { isArray, isString } from '../util/underscore'

export type UnaryOperatorHandler = (operand: any, ctx: Context) => boolean;
export type BinaryOperatorHandler = (lhs: any, rhs: any, ctx: Context) => boolean;
Expand Down Expand Up @@ -34,8 +34,9 @@ export const defaultOperators: Operators = {
},
'contains': (l: any, r: any) => {
l = toValue(l)
r = toValue(r)
return l && isFunction(l.indexOf) ? l.indexOf(r) > -1 : false
if (isArray(l)) return l.some((i) => equal(i, r))
if (isString(l)) return l.indexOf(toValue(r)) > -1
return false
},
'not': (v: any, ctx: Context) => isFalsy(toValue(v), ctx),
'and': (l: any, r: any, ctx: Context) => isTruthy(toValue(l), ctx) && isTruthy(toValue(r), ctx),
Expand Down
Loading