spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Starting work on adding optional timestamp capture group to regular expressions
author
Ben Vogt <[email protected]>
date
2017-04-01 16:43:46
stats
3 file(s) changed, 11 insertions(+), 50 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 0c263fc..25ffaa6 100644
  3--- a/src/RawFormulas/Date.ts
  4+++ b/src/RawFormulas/Date.ts
  5@@ -118,7 +118,7 @@ var DATEVALUE = function (...values) : number {
  6   // NOTE: Must come before YEAR_MONTHDIG_DAY matching.
  7   if (m === undefined) {
  8     var matches = dateString.match(YEAR_MONTHDIG);
  9-    if (matches && matches.length === 6) {
 10+    if (matches && matches.length >= 6) {
 11       var years = parseInt(matches[3]);
 12       var months = parseInt(matches[5]) - 1; // Months are zero indexed.
 13       m = createMoment(years, months, 0);
 14@@ -128,7 +128,7 @@ var DATEVALUE = function (...values) : number {
 15   // Check YEAR_MONTHDIG_DAY, YYYY(fd)MM(fd)DD, "1992/06/24"
 16   if (m === undefined) {
 17     var matches = dateString.match(YEAR_MONTHDIG_DAY);
 18-    if (matches && matches.length === 8) {
 19+    if (matches && matches.length >= 8) {
 20       // Check delimiters. If they're not the same, throw error.
 21       if (matches[4].replace(/\s*/g, '') !== matches[6].replace(/\s*/g, '')) {
 22         throw new CellError(VALUE_ERROR, "DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
 23@@ -144,7 +144,7 @@ var DATEVALUE = function (...values) : number {
 24   // NOTE: Must come before MONTHDIG_DAY_YEAR matching.
 25   if (m === undefined) {
 26     var matches = dateString.match(MONTHDIG_YEAR);
 27-    if (matches && matches.length === 6) {
 28+    if (matches && matches.length >= 6) {
 29       var years = parseInt(matches[5]);
 30       var months = parseInt(matches[3]) - 1; // Months are zero indexed.
 31       m = createMoment(years, months, 0);
 32@@ -154,7 +154,7 @@ var DATEVALUE = function (...values) : number {
 33   // Check MONTHDIG_DAY_YEAR, MM(fd)DD(fd)YYYY, "06/24/1992"
 34   if (m === undefined) {
 35     var matches = dateString.match(MONTHDIG_DAY_YEAR);
 36-    if (matches && matches.length === 8) {
 37+    if (matches && matches.length >= 8) {
 38       // Check delimiters. If they're not the same, throw error.
 39       if (matches[4].replace(/\s*/g, '') !== matches[6].replace(/\s*/g, '')) {
 40         throw new CellError(VALUE_ERROR, "DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
 41@@ -170,7 +170,7 @@ var DATEVALUE = function (...values) : number {
 42   // NOTE: Needs to come before DAY_MONTHNAME_YEAR matching.
 43   if (m === undefined) {
 44     var matches = dateString.match(MONTHNAME_YEAR);
 45-    if (matches && matches.length === 6) {
 46+    if (matches && matches.length >= 6) {
 47       var years = parseInt(matches[5]);
 48       var monthName = matches[3];
 49       m = createMoment(years, monthName, 0);
 50@@ -180,7 +180,7 @@ var DATEVALUE = function (...values) : number {
 51   // Check DAY_MONTHNAME_YEAR, DD(fd)Month(fd)YYYY, '24/July/1992'
 52   if (m === undefined) {
 53     var matches = dateString.match(DAY_MONTHNAME_YEAR);
 54-    if (matches && matches.length === 8) {
 55+    if (matches && matches.length >= 8) {
 56       var years = parseInt(matches[7]);
 57       var monthName = matches[5];
 58       var days = parseInt(matches[3]) - 1; // Days are zero indexed.
 59@@ -197,7 +197,7 @@ var DATEVALUE = function (...values) : number {
 60   // Check YEAR_MONTHNAME, YYYY(fd)Month, '1992/Aug'
 61   if (m === undefined) {
 62     var matches = dateString.match(YEAR_MONTHNAME);
 63-    if (matches && matches.length === 6) {
 64+    if (matches && matches.length >= 6) {
 65       var years = parseInt(matches[3]);
 66       var monthName = matches[5];
 67       m = createMoment(years, monthName, 0);
 68diff --git a/src/RawFormulas/Utils.ts b/src/RawFormulas/Utils.ts
 69index 3319aeb..e370055 100644
 70--- a/src/RawFormulas/Utils.ts
 71+++ b/src/RawFormulas/Utils.ts
 72@@ -370,7 +370,6 @@ class Serializer {
 73 class DateRegExBuilder {
 74   private regexString = "";
 75   private static ZERO_OR_MORE_SPACES = "\\s*";
 76-  private static ONE_OR_MORE_SPACES = "\\s+";
 77 
 78   static DateRegExBuilder() : DateRegExBuilder {
 79     return new DateRegExBuilder();
 80@@ -382,30 +381,7 @@ class DateRegExBuilder {
 81   }
 82 
 83   end() : DateRegExBuilder {
 84-    this.regexString += DateRegExBuilder.ZERO_OR_MORE_SPACES + "$";
 85-    return this;
 86-  }
 87-
 88-  /**
 89-   * Adds a match group for zero or more whitespace tokens.
 90-   * @returns {DateRegExBuilder}
 91-   */
 92-  N_SPACES() : DateRegExBuilder {
 93-    this.regexString += DateRegExBuilder.ZERO_OR_MORE_SPACES;
 94-    return this;
 95-  }
 96-
 97-  /**
 98-   * Adds a match group for zero or more whitespace tokens.
 99-   * @returns {DateRegExBuilder}
100-   */
101-  ONE_OR_N_SPACES() : DateRegExBuilder {
102-    this.regexString += DateRegExBuilder.ONE_OR_MORE_SPACES;
103-    return this;
104-  }
105-
106-  SLASH_DELIMITOR() : DateRegExBuilder {
107-    this.regexString += "\\/";
108+    this.regexString += "\\s*$";
109     return this;
110   }
111 
112@@ -458,23 +434,8 @@ class DateRegExBuilder {
113     return this;
114   }
115 
116-  MERIDIEM(): DateRegExBuilder {
117-    this.regexString += "(am|pm)";
118-    return this;
119-  }
120-
121-  OVERLOAD_MINITES() : DateRegExBuilder {
122-    this.regexString += "([0-9]{2,})";
123-    return this;
124-  }
125-
126-  OVERLOAD_HH() : DateRegExBuilder {
127-    this.regexString += "([0-9]{1,})";
128-    return this;
129-  }
130-
131-  SEMICOLON(): DateRegExBuilder {
132-    this.regexString += ":" + DateRegExBuilder.ZERO_OR_MORE_SPACES;
133+  OPTIONAL_TIMESTAMP_CAPTURE_GROUP() : DateRegExBuilder {
134+    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$))?";
135     return this;
136   }
137 
138diff --git a/tests/DateFormulasTest.ts b/tests/DateFormulasTest.ts
139index 6e7e326..356b2eb 100644
140--- a/tests/DateFormulasTest.ts
141+++ b/tests/DateFormulasTest.ts
142@@ -44,7 +44,7 @@ assertEquals(DATE(-1900, 1, 1).toNumber(), 2);
143 
144 
145 // Test DATEVALUE
146-// YEAR_MONTHDIG_DAY, YYYY(fd)MM(fd)DD =================================================================================
147+// MONTHDIG_DAY_YEAR, MM(fd)DD(fd)YYYY =================================================================================
148 assertEquals(DATEVALUE("6/24/92"), 33779);
149 assertEquals(DATEVALUE("6/24/1992"), 33779);
150 assertEquals(DATEVALUE("06/24/1992"), 33779);
151@@ -107,7 +107,7 @@ catchAndAssertEquals(function() {
152 catchAndAssertEquals(function() {
153   DATEVALUE("1/44/2005");
154 }, ERRORS.VALUE_ERROR);
155-// MONTHDIG_DAY_YEAR, MM(fd)DD(fd)YYYY =================================================================================
156+// YEAR_MONTHDIG_DAY, YYYY(fd)MM(fd)DD =================================================================================
157 assertEquals(DATEVALUE("1992/6/24"), 33779);
158 assertEquals(DATEVALUE("1992/06/24"), 33779);
159 assertEquals(DATEVALUE("1999/1/01"), 36161);