Skip to content

Commit 9c0dc5f

Browse files
committed
fix: [expression] apply value equal for arrays, #589
1 parent 42d2590 commit 9c0dc5f

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

src/render/operator.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,16 @@ import { isComparable } from '../drop/comparable'
22
import { Context } from '../context'
33
import { isFunction, toValue } from '../util'
44
import { isFalsy, isTruthy } from '../render/boolean'
5+
import { isArray } from '../util/underscore';
56

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

1112
export const defaultOperators: Operators = {
12-
'==': (l: any, r: any) => {
13-
if (isComparable(l)) return l.equals(r)
14-
if (isComparable(r)) return r.equals(l)
15-
return toValue(l) === toValue(r)
16-
},
17-
'!=': (l: any, r: any) => {
18-
if (isComparable(l)) return !l.equals(r)
19-
if (isComparable(r)) return !r.equals(l)
20-
return toValue(l) !== toValue(r)
21-
},
13+
'==': equal,
14+
'!=': (l: any, r: any) => !equal(l, r),
2215
'>': (l: any, r: any) => {
2316
if (isComparable(l)) return l.gt(r)
2417
if (isComparable(r)) return r.lt(l)
@@ -48,3 +41,19 @@ export const defaultOperators: Operators = {
4841
'and': (l: any, r: any, ctx: Context) => isTruthy(toValue(l), ctx) && isTruthy(toValue(r), ctx),
4942
'or': (l: any, r: any, ctx: Context) => isTruthy(toValue(l), ctx) || isTruthy(toValue(r), ctx)
5043
}
44+
45+
function equal(lhs: any, rhs: any): boolean {
46+
if (isComparable(lhs)) return lhs.equals(rhs)
47+
if (isComparable(rhs)) return rhs.equals(lhs)
48+
lhs = toValue(lhs)
49+
rhs = toValue(rhs)
50+
if (isArray(lhs)) {
51+
return isArray(rhs) && arrayEqual(lhs, rhs)
52+
}
53+
return lhs === rhs
54+
}
55+
56+
function arrayEqual(lhs: any[], rhs: any[]): boolean {
57+
if (lhs.length !== rhs.length) return false
58+
return !lhs.some((value, i) => !equal(value, rhs[i]))
59+
}

test/e2e/issues.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,4 +400,14 @@ describe('Issues', function () {
400400
const html = await engine.parseAndRender(template, { str })
401401
expect(html).to.match(/^\s*$/)
402402
})
403+
it('#589 Arrays should compare values', async() => {
404+
const engine = new Liquid()
405+
const template = `
406+
{% assign people1 = "alice, bob, carol" | split: ", " -%}
407+
{% assign people2 = "alice, bob, carol" | split: ", " -%}
408+
{% if people1 == people2 %}true{%else%}false{% endif %}
409+
`;
410+
const html = await engine.parseAndRender(template)
411+
expect(html).to.contain('true')
412+
})
403413
})

test/unit/render/expression.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ describe('Expression', function () {
5353
it('should return false for "1==2"', async () => {
5454
expect(await toPromise(create('1==2').evaluate(ctx, false))).to.equal(false)
5555
})
56+
it('should apply deep equal for arrays', async () => {
57+
const ctx = new Context({
58+
arr1: [1, 2],
59+
arr2: [1, 2],
60+
arr3: [1, 2, 3],
61+
})
62+
expect(await toPromise(create('arr1==arr2').evaluate(ctx, false))).to.equal(true)
63+
expect(await toPromise(create('arr1==arr3').evaluate(ctx, false))).to.equal(false)
64+
})
5665
it('should return true for "1<2"', async () => {
5766
expect(await toPromise(create('1<2').evaluate(ctx, false))).to.equal(true)
5867
})

0 commit comments

Comments
 (0)