-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement logical functions and greatCircleDistance function
- Loading branch information
Showing
7 changed files
with
329 additions
and
20 deletions.
There are no files selected for viewing
177 changes: 177 additions & 0 deletions
177
v3/src/models/formula/functions/logic-functions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
import { UNDEF_RESULT } from "./function-utils" | ||
import { math } from "./math" | ||
|
||
describe("boolean", () => { | ||
it("works as expected", () => { | ||
const fn = math.compile("boolean(x)") | ||
expect(fn.evaluate({ x: undefined })).toBe(UNDEF_RESULT) | ||
expect(fn.evaluate({ x: "" })).toBe(UNDEF_RESULT) | ||
expect(fn.evaluate({ x: true })).toBe(true) | ||
expect(fn.evaluate({ x: false })).toBe(false) | ||
expect(fn.evaluate({ x: "true" })).toBe(true) | ||
expect(fn.evaluate({ x: "false" })).toBe(false) | ||
expect(fn.evaluate({ x: "TRUE" })).toBe(true) | ||
expect(fn.evaluate({ x: "FALSE" })).toBe(false) | ||
expect(fn.evaluate({ x: 1 })).toBe(true) | ||
expect(fn.evaluate({ x: 0 })).toBe(false) | ||
expect(fn.evaluate({ x: -1 })).toBe(true) | ||
expect(fn.evaluate({ x: NaN })).toBe(true) | ||
expect(fn.evaluate({ x: "0" })).toBe(false) | ||
// NaN !== 0 | ||
expect(fn.evaluate({ x: "foo" })).toBe(true) | ||
expect(fn.evaluate({ x: "bar" })).toBe(true) | ||
}) | ||
}) | ||
|
||
describe("isBoolean", () => { | ||
it("works as expected", () => { | ||
const fn = math.compile("isBoolean(x)") | ||
expect(fn.evaluate({ x: undefined })).toBe(false) | ||
expect(fn.evaluate({ x: "" })).toBe(false) | ||
expect(fn.evaluate({ x: true })).toBe(true) | ||
expect(fn.evaluate({ x: false })).toBe(true) | ||
expect(fn.evaluate({ x: "true" })).toBe(true) | ||
expect(fn.evaluate({ x: "false" })).toBe(true) | ||
expect(fn.evaluate({ x: "TRUE" })).toBe(true) | ||
expect(fn.evaluate({ x: "FALSE" })).toBe(true) | ||
expect(fn.evaluate({ x: 1 })).toBe(false) | ||
expect(fn.evaluate({ x: 0 })).toBe(false) | ||
expect(fn.evaluate({ x: -1 })).toBe(false) | ||
expect(fn.evaluate({ x: NaN })).toBe(false) | ||
expect(fn.evaluate({ x: "0" })).toBe(false) | ||
expect(fn.evaluate({ x: "foo" })).toBe(false) | ||
expect(fn.evaluate({ x: "bar" })).toBe(false) | ||
}) | ||
}) | ||
|
||
describe("isBoundary", () => { | ||
it("works as expected", () => { | ||
const fn = math.compile("isBoundary(x)") | ||
expect(fn.evaluate({ x: undefined })).toBe(false) | ||
expect(fn.evaluate({ x: "" })).toBe(false) | ||
expect(fn.evaluate({ x: true })).toBe(false) | ||
expect(fn.evaluate({ x: false })).toBe(false) | ||
expect(fn.evaluate({ x: "true" })).toBe(false) | ||
expect(fn.evaluate({ x: "false" })).toBe(false) | ||
expect(fn.evaluate({ x: "TRUE" })).toBe(false) | ||
expect(fn.evaluate({ x: "FALSE" })).toBe(false) | ||
expect(fn.evaluate({ x: 1 })).toBe(false) | ||
expect(fn.evaluate({ x: 0 })).toBe(false) | ||
expect(fn.evaluate({ x: -1 })).toBe(false) | ||
expect(fn.evaluate({ x: NaN })).toBe(false) | ||
expect(fn.evaluate({ x: "0" })).toBe(false) | ||
expect(fn.evaluate({ x: "foo" })).toBe(false) | ||
expect(fn.evaluate({ x: "bar" })).toBe(false) | ||
expect(fn.evaluate({ x: { jsonBoundaryObject: {} } })).toBe(true) | ||
}) | ||
}) | ||
|
||
describe("isColor", () => { | ||
it("works as expected", () => { | ||
const fn = math.compile("isColor(x)") | ||
expect(fn.evaluate({ x: undefined })).toBe(false) | ||
expect(fn.evaluate({ x: "" })).toBe(false) | ||
expect(fn.evaluate({ x: true })).toBe(false) | ||
expect(fn.evaluate({ x: false })).toBe(false) | ||
expect(fn.evaluate({ x: "true" })).toBe(false) | ||
expect(fn.evaluate({ x: "false" })).toBe(false) | ||
expect(fn.evaluate({ x: "TRUE" })).toBe(false) | ||
expect(fn.evaluate({ x: "FALSE" })).toBe(false) | ||
expect(fn.evaluate({ x: 1 })).toBe(false) | ||
expect(fn.evaluate({ x: 0 })).toBe(false) | ||
expect(fn.evaluate({ x: -1 })).toBe(false) | ||
expect(fn.evaluate({ x: NaN })).toBe(false) | ||
expect(fn.evaluate({ x: "0" })).toBe(false) | ||
expect(fn.evaluate({ x: "foo" })).toBe(false) | ||
expect(fn.evaluate({ x: "bar" })).toBe(false) | ||
expect(fn.evaluate({ x: "#aabbcc" })).toBe(true) | ||
expect(fn.evaluate({ x: "#aabbcc88" })).toBe(true) | ||
expect(fn.evaluate({ x: "rgb(1, 2, 3)" })).toBe(true) | ||
}) | ||
}) | ||
|
||
describe("isDate", () => { | ||
it("works as expected", () => { | ||
const fn = math.compile("isDate(x)") | ||
expect(fn.evaluate({ x: undefined })).toBe(false) | ||
expect(fn.evaluate({ x: "" })).toBe(false) | ||
expect(fn.evaluate({ x: true })).toBe(false) | ||
expect(fn.evaluate({ x: false })).toBe(false) | ||
expect(fn.evaluate({ x: "true" })).toBe(false) | ||
expect(fn.evaluate({ x: "false" })).toBe(false) | ||
expect(fn.evaluate({ x: "TRUE" })).toBe(false) | ||
expect(fn.evaluate({ x: "FALSE" })).toBe(false) | ||
expect(fn.evaluate({ x: 1 })).toBe(false) | ||
expect(fn.evaluate({ x: 0 })).toBe(false) | ||
expect(fn.evaluate({ x: -1 })).toBe(false) | ||
expect(fn.evaluate({ x: NaN })).toBe(false) | ||
expect(fn.evaluate({ x: "0" })).toBe(false) | ||
expect(fn.evaluate({ x: "foo" })).toBe(false) | ||
expect(fn.evaluate({ x: "bar" })).toBe(false) | ||
expect(fn.evaluate({ x: new Date() })).toBe(true) | ||
expect(fn.evaluate({ x: "2024-10-22" })).toBe(true) | ||
}) | ||
}) | ||
|
||
describe("isFinite", () => { | ||
it("works as expected", () => { | ||
const fn = math.compile("isFinite(x)") | ||
expect(fn.evaluate({ x: undefined })).toBe(false) | ||
expect(fn.evaluate({ x: "" })).toBe(false) | ||
expect(fn.evaluate({ x: true })).toBe(true) | ||
expect(fn.evaluate({ x: false })).toBe(true) | ||
expect(fn.evaluate({ x: "true" })).toBe(false) | ||
expect(fn.evaluate({ x: "false" })).toBe(false) | ||
expect(fn.evaluate({ x: "TRUE" })).toBe(false) | ||
expect(fn.evaluate({ x: "FALSE" })).toBe(false) | ||
expect(fn.evaluate({ x: 1 })).toBe(true) | ||
expect(fn.evaluate({ x: 0 })).toBe(true) | ||
expect(fn.evaluate({ x: -1 })).toBe(true) | ||
expect(fn.evaluate({ x: NaN })).toBe(false) | ||
expect(fn.evaluate({ x: "0" })).toBe(true) | ||
expect(fn.evaluate({ x: "foo" })).toBe(false) | ||
expect(fn.evaluate({ x: "bar" })).toBe(false) | ||
}) | ||
}) | ||
|
||
describe("isMissing", () => { | ||
it("works as expected", () => { | ||
const fn = math.compile("isMissing(x)") | ||
expect(fn.evaluate({ x: undefined })).toBe(true) | ||
expect(fn.evaluate({ x: "" })).toBe(true) | ||
expect(fn.evaluate({ x: true })).toBe(false) | ||
expect(fn.evaluate({ x: false })).toBe(false) | ||
expect(fn.evaluate({ x: "true" })).toBe(false) | ||
expect(fn.evaluate({ x: "false" })).toBe(false) | ||
expect(fn.evaluate({ x: "TRUE" })).toBe(false) | ||
expect(fn.evaluate({ x: "FALSE" })).toBe(false) | ||
expect(fn.evaluate({ x: 1 })).toBe(false) | ||
expect(fn.evaluate({ x: 0 })).toBe(false) | ||
expect(fn.evaluate({ x: -1 })).toBe(false) | ||
expect(fn.evaluate({ x: NaN })).toBe(false) | ||
expect(fn.evaluate({ x: "0" })).toBe(false) | ||
expect(fn.evaluate({ x: "foo" })).toBe(false) | ||
expect(fn.evaluate({ x: "bar" })).toBe(false) | ||
}) | ||
}) | ||
|
||
describe("isNumber", () => { | ||
it("works as expected", () => { | ||
const fn = math.compile("isNumber(x)") | ||
expect(fn.evaluate({ x: undefined })).toBe(false) | ||
expect(fn.evaluate({ x: "" })).toBe(false) | ||
expect(fn.evaluate({ x: true })).toBe(true) | ||
expect(fn.evaluate({ x: false })).toBe(true) | ||
expect(fn.evaluate({ x: "true" })).toBe(false) | ||
expect(fn.evaluate({ x: "false" })).toBe(false) | ||
expect(fn.evaluate({ x: "TRUE" })).toBe(false) | ||
expect(fn.evaluate({ x: "FALSE" })).toBe(false) | ||
expect(fn.evaluate({ x: 1 })).toBe(true) | ||
expect(fn.evaluate({ x: 0 })).toBe(true) | ||
expect(fn.evaluate({ x: -1 })).toBe(true) | ||
expect(fn.evaluate({ x: NaN })).toBe(false) | ||
expect(fn.evaluate({ x: "0" })).toBe(true) | ||
expect(fn.evaluate({ x: "foo" })).toBe(false) | ||
expect(fn.evaluate({ x: "bar" })).toBe(false) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { colord, Colord } from "colord" | ||
import { isDateString } from "../../../utilities/date-parser" | ||
import { isDate } from "../../../utilities/date-utils" | ||
import { hasOwnProperty } from "../../../utilities/js-utils" | ||
import { isValueEmpty, isValueNonEmpty } from "../../../utilities/math-utils" | ||
import { FValue, IFormulaMathjsFunction } from "../formula-types" | ||
|
||
export const logicFunctions: Record<string, IFormulaMathjsFunction> = { | ||
|
||
boolean: { | ||
numOfRequiredArguments: 1, | ||
evaluate: (arg: FValue) => { | ||
if (isValueEmpty(arg)) return "" | ||
if (arg === true || arg === false) return arg | ||
if (typeof arg === "string" && arg.toLowerCase() === "false") return false | ||
if (typeof arg === "string" && arg.toLowerCase() === "true") return true | ||
return Number(arg) !== 0 | ||
} | ||
}, | ||
|
||
isBoolean: { | ||
numOfRequiredArguments: 1, | ||
evaluate: (arg: FValue) => { | ||
if (typeof arg === "string") { | ||
arg = arg.toLowerCase() | ||
} | ||
const boolValues: FValue[] = [true, false, "true", "false"] | ||
return boolValues.indexOf(arg) >= 0 | ||
} | ||
}, | ||
|
||
isBoundary: { | ||
numOfRequiredArguments: 1, | ||
evaluate: (arg: FValue) => { | ||
return typeof arg === "object" && hasOwnProperty(arg, "jsonBoundaryObject") | ||
} | ||
}, | ||
|
||
isColor: { | ||
numOfRequiredArguments: 1, | ||
evaluate: (arg: FValue) => { | ||
return (arg instanceof Colord || typeof arg === "string") | ||
? colord(arg).isValid() | ||
: false | ||
} | ||
}, | ||
|
||
isDate: { | ||
numOfRequiredArguments: 1, | ||
evaluate: (arg: FValue) => { | ||
return isDate(arg) || isDateString(arg) | ||
} | ||
}, | ||
|
||
isFinite: { | ||
numOfRequiredArguments: 1, | ||
evaluate: (arg: FValue) => { | ||
return isValueNonEmpty(arg) && isFinite(Number(arg)) | ||
} | ||
}, | ||
|
||
isMissing: { | ||
numOfRequiredArguments: 1, | ||
evaluate: (arg: FValue) => { | ||
return isValueEmpty(arg) | ||
} | ||
}, | ||
|
||
isNumber: { | ||
numOfRequiredArguments: 1, | ||
evaluate: (arg: FValue) => { | ||
return isValueNonEmpty(arg) && !isNaN(Number(arg)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.