commit
message
Using DateRegExBuilder for 'YYYY/MM/DD HH(am|pm)'
author
Ben Vogt <[email protected]>
date
2017-03-26 23:05:21
stats
2 file(s) changed,
31 insertions(+),
5 deletions(-)
files
src/RawFormulas/Date.ts
src/RawFormulas/Utils.ts
1diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
2index dd4c79e..f64f2e8 100644
3--- a/src/RawFormulas/Date.ts
4+++ b/src/RawFormulas/Date.ts
5@@ -136,7 +136,7 @@ var DATEVALUE = function (...values) : number {
6 var dateString = TypeCaster.firstValueAsString(values[0]);
7 var m;
8
9- // Check YEAR_MONTHDIG_DAY_SLASH_DELIMIT, YYYY/MM/DD, 1992/06/24
10+ // Check YEAR_MONTHDIG_DAY_SLASH_DELIMIT, YYYY/MM/DD, "1992/06/24"
11 if (m === undefined) {
12 // For reference: https://regex101.com/r/uusfi7/5
13 const REG = DateRegExBuilder.DateRegExBuilder()
14@@ -165,7 +165,7 @@ var DATEVALUE = function (...values) : number {
15 }
16 }
17
18- // Check MONTHDIG_DAY_YEAR_SLASH_DELIMIT, MM/DD/YY(YY), 06/24/1992 or 06/24/92
19+ // Check MONTHDIG_DAY_YEAR_SLASH_DELIMIT, MM/DD/YY(YY), "06/24/1992" or "06/24/92"
20 if (m === undefined) {
21 // For reference: https://regex101.com/r/yHraci/5
22 const REG = DateRegExBuilder.DateRegExBuilder()
23@@ -194,10 +194,15 @@ var DATEVALUE = function (...values) : number {
24 }
25 }
26
27- // Check YYYY/MM/DD HH(am|pm)
28+ // Check YEAR_MONTHDIG_DAY_SLASH_DELIMIT_WITH_HOUR_MERIDIEM, YYYY/MM/DD HH(am|pm), "1992/06/24 12pm"
29 if (m === undefined) {
30 // For reference: https://regex101.com/r/m8FSCr/6
31- var matches = dateString.match(/^\s*(([0-9][0-9][0-9][0-9])|([1-9][0-9][0-9]))\/([1-9]|0[1-9]|1[0-2])\/([1-9]|[0-2][0-9]|3[0-1])\s*([0-9]|0[0-9]|1[0-2])\s*(am|pm)\s*$/i);
32+ const REG = DateRegExBuilder.DateRegExBuilder()
33+ .start()
34+ .YYYY().SLASH_DELIMITOR().MM().SLASH_DELIMITOR().DD().zeroOrMoreSpaces().HH().MERIDIEM()
35+ .end()
36+ .build();
37+ var matches = dateString.match(REG);
38 if (matches && matches.length === 8) {
39 var years = parseInt(matches[1]);
40 var months = parseInt(matches[4]) - 1; // Months are zero indexed.
41diff --git a/src/RawFormulas/Utils.ts b/src/RawFormulas/Utils.ts
42index 62a3500..187537d 100644
43--- a/src/RawFormulas/Utils.ts
44+++ b/src/RawFormulas/Utils.ts
45@@ -451,12 +451,30 @@ class DateRegExBuilder {
46 return this;
47 }
48
49+ /**
50+ *
51+ * @returns {DateRegExBuilder}
52+ * @constructor
53+ */
54+ HH(): DateRegExBuilder {
55+ this.regexString += "([0-9]|0[0-9]|1[0-2])";
56+ return this;
57+ }
58+
59+ MERIDIEM(): DateRegExBuilder {
60+ this.regexString += "(am|pm)";
61+ return this;
62+ }
63+
64+
65+
66 /**
67 * Builds the regular expression.
68 * @returns {RegExp} Built up string as a regular expression.
69 */
70 build() : RegExp {
71- return new RegExp(this.regexString);
72+ // Always ignore case.
73+ return new RegExp(this.regexString, 'i');
74 }
75 }
76