spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Using DateRegExBuilder for 'MONTHNAME_DAY_YEAR_COMMON_DELIMITERS, Month DD YYYY, September 20 1992'
author
Ben Vogt <[email protected]>
date
2017-03-27 00:02:11
stats
3 file(s) changed, 35 insertions(+), 5 deletions(-)
files
README.md
src/RawFormulas/Date.ts
src/RawFormulas/Utils.ts
 1diff --git a/README.md b/README.md
 2index 6573009..c12f1b0 100644
 3--- a/README.md
 4+++ b/README.md
 5@@ -64,7 +64,7 @@ MM-DD-YYYY       MONTHDIG_DAY_YEAR_HYPHEN_DELIMIT        months, days, years
 6 MM.DD.YYYY       MONTHDIG_DAY_YEAR_DOT_DELIMIT           months, days, years
 7 MM DD YYYY       MONTHDIG_DAY_YEAR_SPACE_DELIMIT         months, days, years
 8 MM, DD, YYYY     MONTHDIG_DAY_YEAR_COMMA_DELIMIT         months, days, years
 9-Month DD YYYY    MONTHNAME_DAY_YEAR_COMMON_DELIMITERS    monthName, days, years
10+Month DD YYYY    MONTHNAME_DAY_YEAR_COMMON_DELIMITERS    monthName, days, years        // TODO: right now only comma...
11 DD Month YYYY    DAY_MONTHNAME_YEAR_COMMON_DELIMITERS    days, monthName, years
12 Month DD         MONTHNAME_DAY_COMMON_DELIMITERS         monthName, days
13 DD Month         DAY_MONTHNAME_COMMON_DELIMITERS         days, monthName
14diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
15index c0c90a2..bfbd3cb 100644
16--- a/src/RawFormulas/Date.ts
17+++ b/src/RawFormulas/Date.ts
18@@ -359,11 +359,17 @@ var DATEVALUE = function (...values) : number {
19     }
20   }
21 
22-  // Check (Dayname) Month DD YYYY
23+  // Check MONTHNAME_DAY_YEAR_COMMON_DELIMITERS, Month DD YYYY, September 20 1992
24   if (m === undefined) {
25     // For reference: https://regex101.com/r/xPcm7v/11
26-    var matches = dateString.match(/^\s*(sunday|monday|tuesday|wednesday|thursday|friday|saturday|sun|mon|tues|wed|thur|fri|sat)?,?\s*(january|february|march|april|may|june|july|august|september|october|november|december|jan|feb|mar|apr|jun|jul|aug|sep|oct|nov|dec),?\s*(0?[0-9]|1[0-9]|2[0-9]|3[0-1]),?\s+([0-9]{4}|[1-9][0-9]{2}|[0-9]{2})\s*$/i);
27-    if (matches && matches.length === 5) {
28+    const REG = DateRegExBuilder.DateRegExBuilder()
29+      .start()
30+      .OPTIONAL_DAYNAME().OPTIONAL_COMMA().N_SPACES().MONTHNAME().OPTIONAL_COMMA().N_SPACES().DD().OPTIONAL_COMMA()
31+      .ONE_OR_N_SPACES().YY_OP_YY()
32+      .end()
33+      .build();
34+    var matches = dateString.match(REG);
35+    if (matches && matches.length === 7) {
36       var years = parseInt(matches[4]);
37       var monthName = matches[2];
38       var days = parseInt(matches[3]) - 1; // Days are zero indexed.
39diff --git a/src/RawFormulas/Utils.ts b/src/RawFormulas/Utils.ts
40index 40a1005..aa2e328 100644
41--- a/src/RawFormulas/Utils.ts
42+++ b/src/RawFormulas/Utils.ts
43@@ -399,7 +399,7 @@ class DateRegExBuilder {
44    * Adds a match group for zero or more whitespace tokens.
45    * @returns {DateRegExBuilder}
46    */
47-  oneOrMoreSpaces() : DateRegExBuilder {
48+  ONE_OR_N_SPACES() : DateRegExBuilder {
49     this.regexString += DateRegExBuilder.ONE_OR_MORE_SPACES;
50     return this;
51   }
52@@ -413,11 +413,31 @@ class DateRegExBuilder {
53    * Adds all month full name and short names to the regular expression.
54    * @returns {DateRegExBuilder}
55    */
56-  monthNameCaptureGroup() : DateRegExBuilder {
57+  MONTHNAME() : DateRegExBuilder {
58     this.regexString += "(january|february|march|april|may|june|july|august|september|october|november|december|jan|feb|mar|apr|jun|jul|aug|sep|oct|nov|dec)";
59     return this;
60   }
61 
62+  /**
63+   *
64+   * @returns {DateRegExBuilder}
65+   * @constructor
66+   */
67+  OPTIONAL_DAYNAME() : DateRegExBuilder {
68+    this.regexString += "(sunday|monday|tuesday|wednesday|thursday|friday|saturday|sun|mon|tues|wed|thur|fri|sat)?";
69+    return this;
70+  }
71+
72+  /**
73+   *
74+   * @returns {DateRegExBuilder}
75+   * @constructor
76+   */
77+  OPTIONAL_COMMA() : DateRegExBuilder {
78+    this.regexString += ",?";
79+    return this;
80+  }
81+
82   /**
83    * Adds month digit to the regular expression.
84    * @returns {DateRegExBuilder}
85@@ -432,7 +452,7 @@ class DateRegExBuilder {
86    * @returns {DateRegExBuilder}
87    */
88   DD() : DateRegExBuilder {
89-    this.regexString += "([1-9]|[0-2][0-9]|3[0-1])";
90+    this.regexString += "(0?[0-9]|1[0-9]|2[0-9]|3[0-1])";
91     return this;
92   }
93