Skip to content
Draft
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
59 changes: 58 additions & 1 deletion packages/lib/raqb/jsonLogic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("jsonLogic", () => {
expect(jsonLogic.apply({ "==": ["hello", "world"] })).toBe(false);
});

it.skip("should compare arrays case-insensitively", () => {
it("should compare arrays case-insensitively", () => {
expect(
jsonLogic.apply({
"==": [
Expand All @@ -34,20 +34,77 @@ describe("jsonLogic", () => {
expect(jsonLogic.apply({ "===": ["hello", "HELLO"] })).toBe(true);
expect(jsonLogic.apply({ "===": ["hello", "world"] })).toBe(false);
});

it("should compare arrays case-insensitively", () => {
expect(
jsonLogic.apply({
"===": [
["hello", "WORLD"],
["HELLO", "world"],
],
})
).toBe(true);
expect(
jsonLogic.apply({
"===": [
["hello"],
["hello", "world"],
],
})
).toBe(false);
});
});

describe("!== operation", () => {
it("should compare strings case-insensitively", () => {
expect(jsonLogic.apply({ "!==": ["hello", "HELLO"] })).toBe(false);
expect(jsonLogic.apply({ "!==": ["hello", "world"] })).toBe(true);
});

it("should compare arrays case-insensitively", () => {
expect(
jsonLogic.apply({
"!==": [
["hello", "WORLD"],
["HELLO", "world"],
],
})
).toBe(false);
expect(
jsonLogic.apply({
"!==": [
["hello"],
["hi"],
],
})
).toBe(true);
});
});

describe("!= operation", () => {
it("should compare strings case-insensitively", () => {
expect(jsonLogic.apply({ "!=": ["hello", "HELLO"] })).toBe(false);
expect(jsonLogic.apply({ "!=": ["hello", "world"] })).toBe(true);
});

it("should compare arrays case-insensitively", () => {
expect(
jsonLogic.apply({
"!=": [
["hello", "WORLD"],
["HELLO", "world"],
],
})
).toBe(false);
expect(
jsonLogic.apply({
"!=": [
["a", "b"],
["c", "d"],
],
})
).toBe(true);
});
});

describe("in operation", () => {
Expand Down
20 changes: 16 additions & 4 deletions packages/lib/raqb/jsonLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,36 @@ function normalize<T extends string | string[]>(input: T): T {
return input;
}

// JS == and === compare array references, not values.
// This compares normalized arrays element-by-element.
function areEqual(a: any, b: any): boolean {
const na = normalize(a);
const nb = normalize(b);
if (Array.isArray(na) && Array.isArray(nb)) {
if (na.length !== nb.length) return false;
return na.every((val: any, i: number) => val === nb[i]);
}
return na === nb;
}

/**
* Single Select equals and not equals uses it
* Short Text equals and not equals uses it
*/
jsonLogic.add_operation("==", function (a: any, b: any) {
return normalize(a) == normalize(b);
return areEqual(a, b);
});

jsonLogic.add_operation("===", function (a: any, b: any) {
return normalize(a) === normalize(b);
return areEqual(a, b);
});

jsonLogic.add_operation("!==", function (a: any, b: any) {
return normalize(a) !== normalize(b);
return !areEqual(a, b);
});

jsonLogic.add_operation("!=", function (a: any, b: any) {
return normalize(a) != normalize(b);
return !areEqual(a, b);
});

/**
Expand Down
Loading