Skip to content

Commit c9700e4

Browse files
committed
fix: ensure eq always returns a boolean
1 parent e56d91c commit c9700e4

File tree

3 files changed

+32
-38
lines changed

3 files changed

+32
-38
lines changed

src/compile.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ for (const [category, testGroups] of Object.entries(testsByCategory)) {
3737
for (const group of testGroups) {
3838
describe(group.description, () => {
3939
for (const currentTest of group.tests) {
40-
const description = `input = '${currentTest.input}'`
40+
const description = `input=${JSON.stringify(currentTest.input)}, query=${JSON.stringify(currentTest.query)}`
4141

4242
if (isTestException(currentTest)) {
4343
test(description, () => {

src/is.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export const isArray = <T>(value: unknown): value is T[] => Array.isArray(value)
22

33
export const isObject = (value: unknown): value is object =>
4-
value && typeof value === 'object' && !isArray(value)
4+
value !== null && typeof value === 'object' && !isArray(value)
55

66
export const isString = (value: unknown): value is string => typeof value === 'string'
77

@@ -11,7 +11,7 @@ export const isEqual = <T>(a: T, b: T): boolean => {
1111
return true
1212
}
1313

14-
const bothObject = a && b && typeof a === 'object' && typeof b === 'object'
14+
const bothObject = a !== null && b !== null && typeof a === 'object' && typeof b === 'object'
1515

1616
return (
1717
bothObject &&

test-suite/compile.test.json

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,29 +1195,34 @@
11951195
"input": { "a": 3, "b": 2 },
11961196
"query": ["eq", ["get", "a"], ["get", "b"]],
11971197
"output": false
1198-
}
1199-
]
1200-
},
1201-
{
1202-
"category": "eq",
1203-
"description": "should calculate equal comparing array and number (no type coercion)",
1204-
"tests": [
1205-
{
1206-
"input": null,
1207-
"query": ["eq", ["array", 2], 2],
1208-
"output": false
1209-
}
1198+
},
1199+
{ "input": { "a": 0.1 }, "query": ["eq", ["get", "a"], 0.1], "output": true },
1200+
{ "input": { "a": 0.1 }, "query": ["eq", ["get", "a"], 0.2], "output": false },
1201+
{ "input": { "a": 0.1 }, "query": ["eq", ["get", "a"], -0.1], "output": false },
1202+
{ "input": null, "query": ["eq", "a", "a"], "output": true },
1203+
{ "input": null, "query": ["eq", "a", "b"], "output": false },
1204+
{ "input": null, "query": ["eq", "a", "A"], "output": false },
1205+
{ "input": null, "query": ["eq", "abc", "abc"], "output": true },
1206+
{ "input": null, "query": ["eq", "abc", "ab"], "output": false },
1207+
{ "input": null, "query": ["eq", true, true], "output": true },
1208+
{ "input": null, "query": ["eq", true, false], "output": false },
1209+
{ "input": null, "query": ["eq", false, true], "output": false },
1210+
{ "input": null, "query": ["eq", false, true], "output": false },
1211+
{ "input": null, "query": ["eq", null, null], "output": true }
12101212
]
12111213
},
12121214
{
12131215
"category": "eq",
1214-
"description": "should calculate equal comparing number and string (no type coercion)",
1216+
"description": "should calculate equal comparing mixed types (no type coercion)",
12151217
"tests": [
1216-
{
1217-
"input": null,
1218-
"query": ["eq", "2", 2],
1219-
"output": false
1220-
}
1218+
{ "input": null, "query": ["eq", "2", 2], "output": false },
1219+
{ "input": null, "query": ["eq", "", 0], "output": false },
1220+
{ "input": null, "query": ["eq", 0, ""], "output": false },
1221+
{ "input": null, "query": ["eq", "", null], "output": false },
1222+
{ "input": null, "query": ["eq", null, ""], "output": false },
1223+
{ "input": null, "query": ["eq", 0, null], "output": false },
1224+
{ "input": null, "query": ["eq", ["array"], null], "output": false },
1225+
{ "input": null, "query": ["eq", ["array", 2], 2], "output": false }
12211226
]
12221227
},
12231228
{
@@ -1550,24 +1555,13 @@
15501555
},
15511556
{
15521557
"category": "ne",
1553-
"description": "should calculate not equal comparing array and number (no type coercion)",
1554-
"tests": [
1555-
{
1556-
"input": null,
1557-
"query": ["ne", ["array", 2], 2],
1558-
"output": true
1559-
}
1560-
]
1561-
},
1562-
{
1563-
"category": "ne",
1564-
"description": "should calculate not equal comparing number and string (no type coercion)",
1558+
"description": "should calculate not equal comparing mixed types (no type coercion)",
15651559
"tests": [
1566-
{
1567-
"input": null,
1568-
"query": ["ne", "2", 2],
1569-
"output": true
1570-
}
1560+
{ "input": null, "query": ["ne", "2", 2], "output": true },
1561+
{ "input": null, "query": ["ne", "", 0], "output": true },
1562+
{ "input": null, "query": ["ne", "", null], "output": true },
1563+
{ "input": null, "query": ["ne", 0, null], "output": true },
1564+
{ "input": null, "query": ["ne", ["array", 2], 2], "output": true }
15711565
]
15721566
},
15731567
{

0 commit comments

Comments
 (0)