spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Getting closer to having secondary parsers for the timestamp part of the date values
author
Ben Vogt <[email protected]>
date
2017-04-01 17:29:25
stats
3 file(s) changed, 49 insertions(+), 9 deletions(-)
files
src/RawFormulas/Date.ts
src/RawFormulas/Utils.ts
tests/DateFormulasTest.ts
  1diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
  2index 25ffaa6..465e524 100644
  3--- a/src/RawFormulas/Date.ts
  4+++ b/src/RawFormulas/Date.ts
  5@@ -45,8 +45,8 @@ var DATE = function (...values) {
  6 
  7 const YEAR_MONTHDIG_DAY = DateRegExBuilder.DateRegExBuilder()
  8   .start()
  9-  .OPTIONAL_DAYNAME().OPTIONAL_COMMA().YYYY().FLEX_DELIMITER().MM().FLEX_DELIMITER().DD()
 10-  .end()
 11+  .OPTIONAL_DAYNAME().OPTIONAL_COMMA().YYYY().FLEX_DELIMITER().MM().FLEX_DELIMITER().DD_W_SPACE().OPTIONAL_TIMESTAMP_CAPTURE_GROUP()
 12+  .simpleEnd()
 13   .build();
 14 const MONTHDIG_DAY_YEAR = DateRegExBuilder.DateRegExBuilder()
 15   .start()
 16@@ -55,8 +55,8 @@ const MONTHDIG_DAY_YEAR = DateRegExBuilder.DateRegExBuilder()
 17   .build();
 18 const DAY_MONTHNAME_YEAR = DateRegExBuilder.DateRegExBuilder()
 19   .start()
 20-  .OPTIONAL_DAYNAME().OPTIONAL_COMMA().DD().FLEX_DELIMITER().MONTHNAME().FLEX_DELIMITER().YYYY14()
 21-  .end()
 22+  .OPTIONAL_DAYNAME().OPTIONAL_COMMA().DD().FLEX_DELIMITER().MONTHNAME().FLEX_DELIMITER().YYYY14_W_SPACE().OPTIONAL_TIMESTAMP_CAPTURE_GROUP()
 23+  .simpleEnd()
 24   .build();
 25 const YEAR_MONTHDIG = DateRegExBuilder.DateRegExBuilder()
 26   .start()
 27@@ -79,6 +79,7 @@ const MONTHNAME_YEAR = DateRegExBuilder.DateRegExBuilder()
 28   .end()
 29   .build();
 30 
 31+
 32 /**
 33  * Converts a provided date string in a known format to a date value.
 34  * @param values[0] date_string - The string representing the date. Understood formats include any date format which is
 35@@ -129,6 +130,9 @@ var DATEVALUE = function (...values) : number {
 36   if (m === undefined) {
 37     var matches = dateString.match(YEAR_MONTHDIG_DAY);
 38     if (matches && matches.length >= 8) {
 39+      if (matches.length >= 9 && matches[8] !== undefined) {
 40+        console.log("YEAR_MONTHDIG_DAY matched timestamp", matches[8]);
 41+      }
 42       // Check delimiters. If they're not the same, throw error.
 43       if (matches[4].replace(/\s*/g, '') !== matches[6].replace(/\s*/g, '')) {
 44         throw new CellError(VALUE_ERROR, "DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
 45@@ -181,6 +185,9 @@ var DATEVALUE = function (...values) : number {
 46   if (m === undefined) {
 47     var matches = dateString.match(DAY_MONTHNAME_YEAR);
 48     if (matches && matches.length >= 8) {
 49+      if (matches.length >= 9 && matches[8] !== undefined) {
 50+        console.log("DAY_MONTHNAME_YEAR matched timestamp", matches[8]);
 51+      }
 52       var years = parseInt(matches[7]);
 53       var monthName = matches[5];
 54       var days = parseInt(matches[3]) - 1; // Days are zero indexed.
 55diff --git a/src/RawFormulas/Utils.ts b/src/RawFormulas/Utils.ts
 56index e370055..75fc0b6 100644
 57--- a/src/RawFormulas/Utils.ts
 58+++ b/src/RawFormulas/Utils.ts
 59@@ -385,6 +385,11 @@ class DateRegExBuilder {
 60     return this;
 61   }
 62 
 63+  simpleEnd(): DateRegExBuilder {
 64+    this.regexString += "$";
 65+    return this;
 66+  }
 67+
 68   /**
 69    * Adds all month full name and short names to the regular expression.
 70    * @returns {DateRegExBuilder}
 71@@ -414,6 +419,11 @@ class DateRegExBuilder {
 72     return this;
 73   }
 74 
 75+  DD_W_SPACE() : DateRegExBuilder {
 76+    this.regexString += "(0?[0-9]|1[0-9]|2[0-9]|3[0-1]|0?[0-9]\\s+|1[0-9]\\s+|2[0-9]\\s+|3[0-1]\\s+)";
 77+    return this;
 78+  }
 79+
 80   YYYY() : DateRegExBuilder {
 81     this.regexString += "([0-9]{4}|[1-9][0-9][0-9])";
 82     return this;
 83@@ -424,18 +434,18 @@ class DateRegExBuilder {
 84     return this;
 85   }
 86 
 87-  FLEX_DELIMITER() : DateRegExBuilder {
 88-    this.regexString += "(,?\\s+|\\s*-?\\.?-?\\/?\\s*)";
 89+  YYYY14_W_SPACE() : DateRegExBuilder {
 90+    this.regexString += "([0-9]{1,4}|[0-9]{1,4}\\s+)";
 91     return this;
 92   }
 93 
 94-  HH(): DateRegExBuilder {
 95-    this.regexString += "([0-9]|0[0-9]|1[0-2])";
 96+  FLEX_DELIMITER() : DateRegExBuilder {
 97+    this.regexString += "(,?\\s+|\\s*-?\\.?-?\\/?\\s*)";
 98     return this;
 99   }
100 
101   OPTIONAL_TIMESTAMP_CAPTURE_GROUP() : DateRegExBuilder {
102-    this.regexString += "((\\s+[0-9]+\\s*am|[0-9]+\\s*pm$)|(\\s+[0-9]+:\\s*[0-9]+$)|(\\s+[0-9]+:\\s*[0-9]+\\s*am|\\s+[0-9]+:\\s*[0-9]+\\s*pm$)|(\\s+[0-9]+:\\s*[0-9]+:\\s*[0-9]+$)|(\\s+[0-9]+:\\s*[0-9]+:\\s*[0-9]+\\s*am|[0-9]+:\\s*[0-9]+:\\s*[0-9]+\\s*pm$))?";
103+    this.regexString += "((\\s+[0-9]+\\s*am\\s*$|[0-9]+\\s*pm\\s*$)|(\\s+[0-9]+:\\s*[0-9]+\\s*$)|(\\s+[0-9]+:\\s*[0-9]+\\s*am\\s*$|\\s+[0-9]+:\\s*[0-9]+\\s*pm\\s*$)|(\\s+[0-9]+:\\s*[0-9]+:\\s*[0-9]+\\s*$)|(\\s+[0-9]+:\\s*[0-9]+:\\s*[0-9]+\\s*am\\s*$|[0-9]+:\\s*[0-9]+:\\s*[0-9]+\\s*pm\\s*$))?";
104     return this;
105   }
106 
107diff --git a/tests/DateFormulasTest.ts b/tests/DateFormulasTest.ts
108index 356b2eb..f3edf7a 100644
109--- a/tests/DateFormulasTest.ts
110+++ b/tests/DateFormulasTest.ts
111@@ -181,6 +181,16 @@ catchAndAssertEquals(function() {
112 catchAndAssertEquals(function() {
113   DATEVALUE("2005/1/44");
114 }, ERRORS.VALUE_ERROR);
115+assertEquals(DATEVALUE("1992-6-24 10am"), 33779); // TODO: come back to these. right now just testing to make sure they don't break anything.
116+assertEquals(DATEVALUE("1992-6-24 10:10"), 33779);
117+assertEquals(DATEVALUE("1992-6-24 10:10am"), 33779);
118+assertEquals(DATEVALUE("1992-6-24 10:10:10"), 33779);
119+assertEquals(DATEVALUE("1992-6-24 10:10:10am"), 33779);
120+assertEquals(DATEVALUE("1992-6-24  10  am"), 33779);
121+assertEquals(DATEVALUE("1992-6-24 10: 10 "), 33779);
122+assertEquals(DATEVALUE("1992-6-24 10: 10 pm"), 33779);
123+assertEquals(DATEVALUE("1992-6-24 10: 10: 10"), 33779);
124+assertEquals(DATEVALUE("1992-6-24  101120: 10: 10    am  "), 33779);
125 // DAY_MONTHNAME_YEAR, DD(fd)Month(fd)YYYY =============================================================================
126 assertEquals(DATEVALUE("Sun 09 Feb 2017"), 42775);
127 assertEquals(DATEVALUE("Sun 9 Feb 2017"), 42775);
128@@ -222,6 +232,17 @@ catchAndAssertEquals(function() {
129 catchAndAssertEquals(function() {
130   DATEVALUE("09,Feb,2017");
131 }, ERRORS.VALUE_ERROR);
132+// timestamp tests
133+assertEquals(DATEVALUE("24/June/1992 10am"), 33779); // TODO: come back to these. right now just testing to make sure they don't break anything.
134+assertEquals(DATEVALUE("24/June/1992 10:10"), 33779);
135+assertEquals(DATEVALUE("24/June/1992 10:10am"), 33779);
136+assertEquals(DATEVALUE("24/June/1992 10:10:10"), 33779);
137+assertEquals(DATEVALUE("24/June/1992 10:10:10am"), 33779);
138+assertEquals(DATEVALUE("24/June/1992  10  am"), 33779);
139+assertEquals(DATEVALUE("24/June/1992 10: 10 "), 33779);
140+assertEquals(DATEVALUE("24/June/1992 10: 10 pm"), 33779);
141+assertEquals(DATEVALUE("24/June/1992 10: 10: 10"), 33779);
142+assertEquals(DATEVALUE("24/June/1992  101120: 10: 10    am  "), 33779);
143 // YEAR_MONTHDIG, YYYY(fd)MM, '1992/06' ================================================================================
144 assertEquals(DATEVALUE("2017/01"), 42736);
145 assertEquals(DATEVALUE("2017/02"), 42767);