Skip to content

Commit

Permalink
fix: [expression] apply value equal for arrays, #589
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Mar 2, 2023
1 parent 42d2590 commit 9c0dc5f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/render/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@ import { isComparable } from '../drop/comparable'
import { Context } from '../context'
import { isFunction, toValue } from '../util'
import { isFalsy, isTruthy } from '../render/boolean'
import { isArray } from '../util/underscore';

export type UnaryOperatorHandler = (operand: any, ctx: Context) => boolean;
export type BinaryOperatorHandler = (lhs: any, rhs: any, ctx: Context) => boolean;
export type OperatorHandler = UnaryOperatorHandler | BinaryOperatorHandler;
export type Operators = Record<string, OperatorHandler>

export const defaultOperators: Operators = {
'==': (l: any, r: any) => {
if (isComparable(l)) return l.equals(r)
if (isComparable(r)) return r.equals(l)
return toValue(l) === toValue(r)
},
'!=': (l: any, r: any) => {
if (isComparable(l)) return !l.equals(r)
if (isComparable(r)) return !r.equals(l)
return toValue(l) !== toValue(r)
},
'==': equal,
'!=': (l: any, r: any) => !equal(l, r),
'>': (l: any, r: any) => {
if (isComparable(l)) return l.gt(r)
if (isComparable(r)) return r.lt(l)
Expand Down Expand Up @@ -48,3 +41,19 @@ export const defaultOperators: Operators = {
'and': (l: any, r: any, ctx: Context) => isTruthy(toValue(l), ctx) && isTruthy(toValue(r), ctx),
'or': (l: any, r: any, ctx: Context) => isTruthy(toValue(l), ctx) || isTruthy(toValue(r), ctx)
}

function equal(lhs: any, rhs: any): boolean {
if (isComparable(lhs)) return lhs.equals(rhs)
if (isComparable(rhs)) return rhs.equals(lhs)
lhs = toValue(lhs)
rhs = toValue(rhs)
if (isArray(lhs)) {
return isArray(rhs) && arrayEqual(lhs, rhs)
}
return lhs === rhs
}

function arrayEqual(lhs: any[], rhs: any[]): boolean {
if (lhs.length !== rhs.length) return false
return !lhs.some((value, i) => !equal(value, rhs[i]))
}
10 changes: 10 additions & 0 deletions test/e2e/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,14 @@ describe('Issues', function () {
const html = await engine.parseAndRender(template, { str })
expect(html).to.match(/^\s*$/)
})
it('#589 Arrays should compare values', async() => {
const engine = new Liquid()
const template = `
{% assign people1 = "alice, bob, carol" | split: ", " -%}
{% assign people2 = "alice, bob, carol" | split: ", " -%}
{% if people1 == people2 %}true{%else%}false{% endif %}
`;
const html = await engine.parseAndRender(template)
expect(html).to.contain('true')
})
})
9 changes: 9 additions & 0 deletions test/unit/render/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ describe('Expression', function () {
it('should return false for "1==2"', async () => {
expect(await toPromise(create('1==2').evaluate(ctx, false))).to.equal(false)
})
it('should apply deep equal for arrays', async () => {
const ctx = new Context({
arr1: [1, 2],
arr2: [1, 2],
arr3: [1, 2, 3],
})
expect(await toPromise(create('arr1==arr2').evaluate(ctx, false))).to.equal(true)
expect(await toPromise(create('arr1==arr3').evaluate(ctx, false))).to.equal(false)
})
it('should return true for "1<2"', async () => {
expect(await toPromise(create('1<2').evaluate(ctx, false))).to.equal(true)
})
Expand Down

0 comments on commit 9c0dc5f

Please sign in to comment.