spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Refactoring some of the previous regular expressions to make them more flexible.
author
Ben Vogt <[email protected]>
date
2017-03-26 20:28:03
stats
2 file(s) changed, 58 insertions(+), 69 deletions(-)
files
src/RawFormulas/Date.ts
tests/DateFormulasTest.ts
  1diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
  2index c6100a2..d1dc4dc 100644
  3--- a/src/RawFormulas/Date.ts
  4+++ b/src/RawFormulas/Date.ts
  5@@ -338,31 +338,15 @@ var DATEVALUE = function (...values) : number {
  6     }
  7   }
  8 
  9-
 10-  // Check Month DD
 11-  if (m === undefined) {
 12-    // For reference: https://regex101.com/r/hujaIk/7
 13-    var matches = dateString.match(/^\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*|\s*-?\/?\s*|\s*\.?\s+)(0?[1-9]|[1-2][0-9]|3[0-1])\s*$/i);
 14-    if (matches && matches.length === 4) {
 15-      var monthName = matches[1];
 16-      var days = parseInt(matches[3]) - 1; // Days are zero indexed.
 17-      var tmpMoment = moment.utc([moment.utc().year()]).startOf('year').month(monthName);
 18-      // If we're specifying more days than there are in this month
 19-      if (days > tmpMoment.daysInMonth() - 1) {
 20-        throw new CellError(VALUE_ERROR, "DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
 21-      }
 22-      m = tmpMoment.add({"days": days});
 23-    }
 24-  }
 25-
 26-  // Check DD Month
 27+  // Check DD Month (Year)
 28   if (m === undefined) {
 29-    // For reference: https://regex101.com/r/GM7KgL/2
 30-    var matches = dateString.match(/^\s*(0?[1-9]|[1-2][0-9]|3[0-1])(,?\s*|\s*-?\/?\s*|\s*\.?\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*$/i);
 31-    if (matches && matches.length === 4) {
 32+    // For reference: https://regex101.com/r/mOnd0i/4
 33+    var matches = dateString.match(/^\s*(0?[1-9]|[1-2][0-9]|3[0-1])(,?\s*|\s*-?\/?\s*|\s*\.?\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*,?\s*\/?\s*(\.?\s+)?([0-9]{4}|[1-9][0-9]{2}|[0-9]{2})?$/i);
 34+    if (matches && (matches.length >= 4 || matches.length <= 6)) {
 35       var monthName = matches[3];
 36       var days = parseInt(matches[1]) - 1; // Days are zero indexed.
 37-      var tmpMoment = moment.utc([moment.utc().year()]).startOf('year').month(monthName);
 38+      var year = matches.length === 5 ? moment.utc().year() : parseInt(matches[5]);
 39+      var tmpMoment = moment.utc([year]).startOf('year').month(monthName);
 40       // If we're specifying more days than there are in this month
 41       if (days > tmpMoment.daysInMonth() - 1) {
 42         throw new CellError(VALUE_ERROR, "DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
 43diff --git a/tests/DateFormulasTest.ts b/tests/DateFormulasTest.ts
 44index 312e1f2..d1d9c1c 100644
 45--- a/tests/DateFormulasTest.ts
 46+++ b/tests/DateFormulasTest.ts
 47@@ -375,51 +375,58 @@ assertEquals(DATEVALUE("December 0100"), -657100);
 48 catchAndAssertEquals(function() {
 49   DATEVALUE("December 100");// need 4 digits
 50 }, ERRORS.VALUE_ERROR);
 51-// Month DD
 52-// WARNING: Overriding Date prototype to lock in 'now' tests------------------------------------------------------------
 53-Date.now = function() { return 1430462280860; }; // 2015-05-01T01:38:00.860
 54-Date.prototype.getTime = Date.now;
 55-// END WARNING----------------------------------------------------------------------------------------------------------
 56-assertEquals(DATEVALUE("Jan 20"), 42024);
 57-assertEquals(DATEVALUE("Feb 20"), 42055);
 58-assertEquals(DATEVALUE("Mar 20"), 42083);
 59-assertEquals(DATEVALUE("Apr 20"), 42114);
 60-assertEquals(DATEVALUE("May 20"), 42144);
 61-assertEquals(DATEVALUE("Jun 20"), 42175);
 62-assertEquals(DATEVALUE("Jul 20"), 42205);
 63-assertEquals(DATEVALUE("Aug 20"), 42236);
 64-assertEquals(DATEVALUE("Sep 20"), 42267);
 65-assertEquals(DATEVALUE("Oct 20"), 42297);
 66-assertEquals(DATEVALUE("Nov 20"), 42328);
 67-assertEquals(DATEVALUE("Dec 20"), 42358);
 68-assertEquals(DATEVALUE("Dec. 20"), 42358);
 69-assertEquals(DATEVALUE("Dec, 20"), 42358);
 70-assertEquals(DATEVALUE("Dec/ 20"), 42358);
 71-assertEquals(DATEVALUE("Dec- 20"), 42358);
 72-assertEquals(DATEVALUE("Dec-20"), 42358);
 73-assertEquals(DATEVALUE("June 2"), 42157);
 74 catchAndAssertEquals(function() {
 75   DATEVALUE("Dec.20");// need space if using period
 76 }, ERRORS.VALUE_ERROR);
 77 // DD Month
 78-assertEquals(DATEVALUE("20 Jan"), 42024);
 79-assertEquals(DATEVALUE("20 Feb"), 42055);
 80-assertEquals(DATEVALUE("20 Mar"), 42083);
 81-assertEquals(DATEVALUE("20 Apr"), 42114);
 82-assertEquals(DATEVALUE("20 May"), 42144);
 83-assertEquals(DATEVALUE("20 Jun"), 42175);
 84-assertEquals(DATEVALUE("20 Jul"), 42205);
 85-assertEquals(DATEVALUE("20 Aug"), 42236);
 86-assertEquals(DATEVALUE("20 Sep"), 42267);
 87-assertEquals(DATEVALUE("20 Oct"), 42297);
 88-assertEquals(DATEVALUE("20 Nov"), 42328);
 89-assertEquals(DATEVALUE("20 Dec"), 42358);
 90-assertEquals(DATEVALUE("20. Dec"), 42358);
 91-assertEquals(DATEVALUE("20, Dec"), 42358);
 92-assertEquals(DATEVALUE("20/ Dec"), 42358);
 93-assertEquals(DATEVALUE("20- Dec"), 42358);
 94-assertEquals(DATEVALUE("20-Dec"), 42358);
 95-assertEquals(DATEVALUE("2 June"), 42157);
 96+assertEquals(DATEVALUE("01 Jan 2017"), 42736);
 97+assertEquals(DATEVALUE("01 Feb 2017"), 42767);
 98+assertEquals(DATEVALUE("01 Mar 2017"), 42795);
 99+assertEquals(DATEVALUE("01 Apr 2017"), 42826);
100+assertEquals(DATEVALUE("01 May 2017"), 42856);
101+assertEquals(DATEVALUE("01 Jun 2017"), 42887);
102+assertEquals(DATEVALUE("01 Jul 2017"), 42917);
103+assertEquals(DATEVALUE("01 Aug 2017"), 42948);
104+assertEquals(DATEVALUE("01 Sep 2017"), 42979);
105+assertEquals(DATEVALUE("01 Oct 2017"), 43009);
106+assertEquals(DATEVALUE("01 Nov 2017"), 43040);
107+assertEquals(DATEVALUE(" 1 Dec 2017"), 43070);
108+assertEquals(DATEVALUE("20 Jan 2015"), 42024);
109+assertEquals(DATEVALUE("20 Feb 2015"), 42055);
110+assertEquals(DATEVALUE("20 Mar 2015"), 42083);
111+assertEquals(DATEVALUE("20 Apr 2015"), 42114);
112+assertEquals(DATEVALUE("20 May 2015"), 42144);
113+assertEquals(DATEVALUE("20 Jun 2015"), 42175);
114+assertEquals(DATEVALUE("20 Jul 2015"), 42205);
115+assertEquals(DATEVALUE("20 Aug 2015"), 42236);
116+assertEquals(DATEVALUE("20 Sep 2015"), 42267);
117+assertEquals(DATEVALUE("20 Oct 2015"), 42297);
118+assertEquals(DATEVALUE("20 Nov 2015"), 42328);
119+assertEquals(DATEVALUE("20 Dec 2015"), 42358);
120+assertEquals(DATEVALUE("20. Dec 2015"), 42358);
121+assertEquals(DATEVALUE("20, Dec 2015"), 42358);
122+assertEquals(DATEVALUE("20/ Dec 2015"), 42358);
123+assertEquals(DATEVALUE("20- Dec 2015"), 42358);
124+assertEquals(DATEVALUE("20-Dec 2015"), 42358);
125+assertEquals(DATEVALUE("2 June 2015"), 42157);
126+assertEquals(DATEVALUE("2 / june 2015"), 42157);
127+assertEquals(DATEVALUE("2 . june . 2015"), 42157);
128+assertEquals(DATEVALUE("2, june 2015"), 42157);
129+assertEquals(DATEVALUE("2, june2015"), 42157);
130+assertEquals(DATEVALUE("2,June2015"), 42157);
131+assertEquals(DATEVALUE("2June2015"), 42157);
132+assertEquals(DATEVALUE("2. June. 2015"), 42157);
133+assertEquals(DATEVALUE("2/June/2015"), 42157);
134+assertEquals(DATEVALUE("2 /June / 2015"), 42157);
135 catchAndAssertEquals(function() {
136-  DATEVALUE("20.Dec");// need space if using period
137+  DATEVALUE("20.Dec 2015");// need space if using period
138+}, ERRORS.VALUE_ERROR);
139+catchAndAssertEquals(function() {
140+  DATEVALUE("20.Dec.2015");// need space if using period
141 }, ERRORS.VALUE_ERROR);
142+catchAndAssertEquals(function() {
143+  DATEVALUE("20 Dec.2015");// need space if using period
144+}, ERRORS.VALUE_ERROR);
145+catchAndAssertEquals(function() {
146+  DATEVALUE("20.Dec");// need space if using period
147+}, ERRORS.VALUE_ERROR);
148\ No newline at end of file