From 1937aa1f1dce01ee6332f39a6e658e85cbe4f30b Mon Sep 17 00:00:00 2001 From: Santi Albo Date: Wed, 14 Feb 2024 03:19:32 +0100 Subject: [PATCH] fix: Rely on equal for computing contains (#668) * fix: rely on equal for computing contains This change allows contains to be computed using the toValue of the items in the array. Before this change if I had an array of "Drop" `contains` would not match against the value. * Apply suggestions from code review Co-authored-by: Jun Yang --------- Co-authored-by: Jun Yang --- src/render/expression.spec.ts | 14 ++++++++++++++ src/render/operator.ts | 9 +++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/render/expression.spec.ts b/src/render/expression.spec.ts index fee7ae127..a1ad20936 100644 --- a/src/render/expression.spec.ts +++ b/src/render/expression.spec.ts @@ -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) diff --git a/src/render/operator.ts b/src/render/operator.ts index 048eb9700..77ea07c10 100644 --- a/src/render/operator.ts +++ b/src/render/operator.ts @@ -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; @@ -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),