From 0a169a78d25200a86a62e3aa6a849a32e031984f Mon Sep 17 00:00:00 2001 From: Nandan V Date: Fri, 13 Aug 2021 13:08:30 -0500 Subject: [PATCH 1/2] fuzzydate strict date option regex --- bits/20_jsutils.js | 27 ++++++++++++++++++++++++++- bits/40_harb.js | 2 +- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/bits/20_jsutils.js b/bits/20_jsutils.js index f9a2db9..3f08ffb 100644 --- a/bits/20_jsutils.js +++ b/bits/20_jsutils.js @@ -130,8 +130,33 @@ function fuzzynum(s/*:string*/)/*:number*/ { if(!isNaN(v = Number(ss))) return v / wt; return v; } -function fuzzydate(s/*:string*/)/*:Date*/ { + +// date regex reference - https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s04.html +// matches mm/dd/yy, mm/dd/yyyy, dd/mm/yy, dd/mm/yyyy +const STRICT_DATE_REGEX = [ + "^(?:(1[0-2]|0[1-9])\\.(3[01]|[12][0-9]|0[1-9])|(3[01]|[12][0-9]|0[1-9])\\.(1[0-2]|0[1-9]))\\.([0-2][0-9]{3}|[0-9]{2})$", + "^(?:(1[0-2]|0[1-9])\/(3[01]|[12][0-9]|0[1-9])|(3[01]|[12][0-9]|0[1-9])\/(1[0-2]|0[1-9]))\/([0-2][0-9]{3}|[0-9]{2})$", + "^(?:(1[0-2]|0[1-9])-(3[01]|[12][0-9]|0[1-9])|(3[01]|[12][0-9]|0[1-9])-(1[0-2]|0[1-9]))-([0-2][0-9]{3}|[0-9]{2})$", + "^(?:(1[0-2]|0[1-9])\\.(3[01]|[12][0-9]|0[1-9])|(3[01]|[12][0-9]|0[1-9])\\.(1[0-2]|0[1-9]))\\.([0-2][0-9]{3}|[0-9]{2})$", + "^(?:(1[0-2]|0[1-9])\/(3[01]|[12][0-9]|0[1-9])|(3[01]|[12][0-9]|0[1-9])\/(1[0-2]|0[1-9]))\/([0-2][0-9]{3}|[0-9]{2})$", + "^(?:(1[0-2]|0[1-9])-(3[01]|[12][0-9]|0[1-9])|(3[01]|[12][0-9]|0[1-9])-(1[0-2]|0[1-9]))-([0-2][0-9]{3}|[0-9]{2})$" + ]; + +function fuzzydate(s/*:string*/, strictDate = false/*boolean*/)/*:Date*/ { var o = new Date(s), n = new Date(NaN); + + if(strictDate) { + STRICT_DATE_REGEX.map(regex => { + const matchStr = s.match(regex) + + if(matchStr){ + return o; + }; + }); + + return n; + } + var y = o.getYear(), m = o.getMonth(), d = o.getDate(); if(isNaN(d)) return n; if(y < 0 || y > 8099) return n; diff --git a/bits/40_harb.js b/bits/40_harb.js index 849d785..28f3866 100644 --- a/bits/40_harb.js +++ b/bits/40_harb.js @@ -883,7 +883,7 @@ var PRN = (function() { else if(s == "TRUE") { cell.t = 'b'; cell.v = true; } else if(s == "FALSE") { cell.t = 'b'; cell.v = false; } else if(!isNaN(v = fuzzynum(s))) { cell.t = 'n'; if(o.cellText !== false) cell.w = s; cell.v = v; } - else if(!isNaN(fuzzydate(s).getDate()) || _re && s.match(_re)) { + else if(!isNaN(fuzzydate(s, o.strictDate).getDate()) || _re && s.match(_re)) { cell.z = o.dateNF || SSF._table[14]; var k = 0; if(_re && s.match(_re)){ s=dateNF_fix(s, o.dateNF, (s.match(_re)||[])); k=1; } -- 2.34.1 From 79a49b61f50f3084c19a48a99e5b8395c08a9942 Mon Sep 17 00:00:00 2001 From: Nandan V Date: Wed, 6 Oct 2021 21:02:14 -0400 Subject: [PATCH 2/2] suggested fixes + support for custom date format --- bits/20_jsutils.js | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/bits/20_jsutils.js b/bits/20_jsutils.js index 3f08ffb..9c93eef 100644 --- a/bits/20_jsutils.js +++ b/bits/20_jsutils.js @@ -133,7 +133,7 @@ function fuzzynum(s/*:string*/)/*:number*/ { // date regex reference - https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s04.html // matches mm/dd/yy, mm/dd/yyyy, dd/mm/yy, dd/mm/yyyy -const STRICT_DATE_REGEX = [ +var STRICT_DATE_REGEX = [ "^(?:(1[0-2]|0[1-9])\\.(3[01]|[12][0-9]|0[1-9])|(3[01]|[12][0-9]|0[1-9])\\.(1[0-2]|0[1-9]))\\.([0-2][0-9]{3}|[0-9]{2})$", "^(?:(1[0-2]|0[1-9])\/(3[01]|[12][0-9]|0[1-9])|(3[01]|[12][0-9]|0[1-9])\/(1[0-2]|0[1-9]))\/([0-2][0-9]{3}|[0-9]{2})$", "^(?:(1[0-2]|0[1-9])-(3[01]|[12][0-9]|0[1-9])|(3[01]|[12][0-9]|0[1-9])-(1[0-2]|0[1-9]))-([0-2][0-9]{3}|[0-9]{2})$", @@ -142,17 +142,37 @@ const STRICT_DATE_REGEX = [ "^(?:(1[0-2]|0[1-9])-(3[01]|[12][0-9]|0[1-9])|(3[01]|[12][0-9]|0[1-9])-(1[0-2]|0[1-9]))-([0-2][0-9]{3}|[0-9]{2})$" ]; -function fuzzydate(s/*:string*/, strictDate = false/*boolean*/)/*:Date*/ { +function fuzzydate(s/*:string*/, strictDate/*string*/)/*:Date*/ { var o = new Date(s), n = new Date(NaN); if(strictDate) { - STRICT_DATE_REGEX.map(regex => { - const matchStr = s.match(regex) + var dateFormat = strictDate.toLowerCase().replace(/[.]|[-]/g, '/') + + STRICT_DATE_REGEX + .map(function (regex) { + var regexTestString = + var matchStr = regexTestString.test(s) if(matchStr){ + s.replace(/[.]|[-]/g, '/') + + if (dateFormat === "dd/mm") { + var splitDate = s.split('/') + var newDate = [s[1], s[0], s[2]] + + s = newDate.join('/') + + o = new Date(s) + return o + } + + o = new Date(s) return o; }; - }); + }) + .filter(function (result) { + return ((result !== undefined) && (result !== null)) + }); return n; } -- 2.34.1