Repository: leifg/formulon (js)

Issue:
Make time comparable
Simple formula like this returns an error:

```
TIMENOW() >= TIMEVALUE("04:00:00")
```

Pull request:
Make time comparable
closes #1238


Patch:
diff --git a/src/functionDispatcher.js b/src/functionDispatcher.js
index 1472e60..1aefb71 100644
--- a/src/functionDispatcher.js
+++ b/src/functionDispatcher.js
@@ -111,11 +111,11 @@ const existingFunctions = {
     returnType: 'text',
   },
   greaterThan: {
-    validations: [minNumOfParams(2), maxNumOfParams(2), paramTypes(['number', 'date', 'datetime']), sameParamType()],
+    validations: [minNumOfParams(2), maxNumOfParams(2), paramTypes(['number', 'date', 'datetime', 'time']), sameParamType()],
     returnType: 'checkbox',
   },
   greaterThanOrEqual: {
-    validations: [minNumOfParams(2), maxNumOfParams(2), paramTypes(['number', 'date', 'datetime'])],
+    validations: [minNumOfParams(2), maxNumOfParams(2), paramTypes(['number', 'date', 'datetime', 'time'])],
     returnType: 'checkbox',
   },
   hour: {
@@ -163,11 +163,11 @@ const existingFunctions = {
     returnType: 'number',
   },
   lessThan: {
-    validations: [minNumOfParams(2), maxNumOfParams(2), paramTypes(['number', 'date', 'datetime']), sameParamType()],
+    validations: [minNumOfParams(2), maxNumOfParams(2), paramTypes(['number', 'date', 'datetime', 'time']), sameParamType()],
     returnType: 'checkbox',
   },
   lessThanOrEqual: {
-    validations: [minNumOfParams(2), maxNumOfParams(2), paramTypes(['number', 'date', 'datetime']), sameParamType()],
+    validations: [minNumOfParams(2), maxNumOfParams(2), paramTypes(['number', 'date', 'datetime', 'time']), sameParamType()],
     returnType: 'checkbox',
   },
   ln: {


Test patch:
diff --git a/test/unit/functions.spec.js b/test/unit/functions.spec.js
index fe0c2b7..526d07e 100644
--- a/test/unit/functions.spec.js
+++ b/test/unit/functions.spec.js
@@ -840,6 +840,20 @@ describe('greaterThan', () => {
     });
   });
 
+  describe('Time', () => {
+    it('greater than', () => {
+      expect(dispatch('greaterThan', [buildTimeLiteral(45000001), buildTimeLiteral(45000000)])).to.deep.eq(buildLiteralFromJs(true));
+    });
+
+    it('smaller than', () => {
+      expect(dispatch('greaterThan', [buildTimeLiteral(45000000), buildTimeLiteral(45000001)])).to.deep.eq(buildLiteralFromJs(false));
+    });
+
+    it('equal', () => {
+      expect(dispatch('greaterThan', [buildTimeLiteral(45000000), buildTimeLiteral(45000000)])).to.deep.eq(buildLiteralFromJs(false));
+    });
+  });
+
   describe('different types', () => {
     it('returns an ArgumentError', () => {
       expect(dispatch('greaterThan', [buildDateLiteral(2020, 2, 10), buildLiteralFromJs(1)])).to.deep.eq(buildErrorLiteral('ArgumentError', "Incorrect parameter type for function 'GREATERTHAN()'. Expected Date, received Number", { function: 'greaterThan', expected: 'date', received: 'number' }));
@@ -890,6 +904,20 @@ describe('greaterThanOrEqual', () => {
     });
   });
 
+  describe('Time', () => {
+    it('greater than', () => {
+      expect(dispatch('greaterThanOrEqual', [buildTimeLiteral(45000001), buildTimeLiteral(45000000)])).to.deep.eq(buildLiteralFromJs(true));
+    });
+
+    it('smaller than', () => {
+      expect(dispatch('greaterThanOrEqual', [buildTimeLiteral(45000000), buildTimeLiteral(45000001)])).to.deep.eq(buildLiteralFromJs(false));
+    });
+
+    it('equal', () => {
+      expect(dispatch('greaterThanOrEqual', [buildTimeLiteral(45000000), buildTimeLiteral(45000000)])).to.deep.eq(buildLiteralFromJs(true));
+    });
+  });
+
   describe('different types', () => {
     it('returns an ArgumentError', () => {
       expect(dispatch('greaterThan', [buildDateLiteral(2020, 2, 10), buildLiteralFromJs(1)])).to.deep.eq(buildErrorLiteral('ArgumentError', "Incorrect parameter type for function 'GREATERTHAN()'. Expected Date, received Number", { function: 'greaterThan', expected: 'date', received: 'number' }));
@@ -940,6 +968,20 @@ describe('lessThan', () => {
     });
   });
 
+  describe('Time', () => {
+    it('greater than', () => {
+      expect(dispatch('lessThan', [buildTimeLiteral(45000001), buildTimeLiteral(45000000)])).to.deep.eq(buildLiteralFromJs(false));
+    });
+
+    it('smaller than', () => {
+      expect(dispatch('lessThan', [buildTimeLiteral(45000000), buildTimeLiteral(45000001)])).to.deep.eq(buildLiteralFromJs(true));
+    });
+
+    it('equal', () => {
+      expect(dispatch('lessThan', [buildTimeLiteral(45000000), buildTimeLiteral(45000000)])).to.deep.eq(buildLiteralFromJs(false));
+    });
+  });
+
   describe('different types', () => {
     it('returns an ArgumentError', () => {
       expect(dispatch('lessThan', [buildDateLiteral(2020, 2, 10), buildLiteralFromJs(1)])).to.deep.eq(buildErrorLiteral('ArgumentError', "Incorrect parameter type for function 'LESSTHAN()'. Expected Date, received Number", { function: 'lessThan', expected: 'date', received: 'number' }));
@@ -990,6 +1032,20 @@ describe('lessThanOrEqual', () => {
     });
   });
 
+  describe('Time', () => {
+    it('greater than', () => {
+      expect(dispatch('lessThanOrEqual', [buildTimeLiteral(45000001), buildTimeLiteral(45000000)])).to.deep.eq(buildLiteralFromJs(false));
+    });
+
+    it('smaller than', () => {
+      expect(dispatch('lessThanOrEqual', [buildTimeLiteral(45000000), buildTimeLiteral(45000001)])).to.deep.eq(buildLiteralFromJs(true));
+    });
+
+    it('equal', () => {
+      expect(dispatch('lessThanOrEqual', [buildTimeLiteral(45000000), buildTimeLiteral(45000000)])).to.deep.eq(buildLiteralFromJs(true));
+    });
+  });
+
   describe('different types', () => {
     it('returns an ArgumentError', () => {
       expect(dispatch('lessThanOrEqual', [buildDateLiteral(2020, 2, 10), buildLiteralFromJs(1)])).to.deep.eq(buildErrorLiteral('ArgumentError', "Incorrect parameter type for function 'LESSTHANOREQUAL()'. Expected Date, received Number", { function: 'lessThanOrEqual', expected: 'date', received: 'number' }));