Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #1300 fix: fuzzydate strict date option regex #2354

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion bits/20_jsutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,53 @@ 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
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})$",
"^(?:(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/*string*/)/*:Date*/ {
var o = new Date(s), n = new Date(NaN);

if(strictDate) {
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;
}

var y = o.getYear(), m = o.getMonth(), d = o.getDate();
if(isNaN(d)) return n;
if(y < 0 || y > 8099) return n;
Expand Down
2 changes: 1 addition & 1 deletion bits/40_harb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down