Closed pie6k closed 6 years ago
Hi ... sorry I'm massively late on this one... this probably isn't even on your radar anymore, but hopefully I can answer in case someone else has this problem.
The biggest problem I see here is that Sugar is made to parse a wide variety of formats with multiple optional tokens... So you could have two days from now
as well as two days from now at 4:45pm
. There are a ton of permutations of potential formats and it's likely that you wouldn't want to show them all, so the best way, if it's possible at all, that I can think of would be to extract and parse the regex sources themselves to inject example data, but it would be a lot of work.
The other issue is that the regexes are set up so that partial strings won't parse... it must be a fully recognized date, so you'd have to do some massaging of them to accept partial matches. Here's a vastly oversimplified example:
var reg = Sugar.Date.getLocale('en').compiledFormats[0].reg;
// reg is at this point /^ *(last|the|this|next) ?(week|weeks)? ?(Sunday|Monday|Tuesday|...)(?:(?:,?[\s\u3000]| ?(?:at) ?)([0-2]?\d)(?:[.:]([0-5]\d)(?:[.:]([0-5]\d(?:[,.]\d+)?))? ?(AM|PM|A\.M\.|P\.M\.|a|p)?| ?(AM|PM|A\.M\.|P\.M\.|a|p)))? *$/i
var partialSrc = reg.source.replace(/(\(.+]\))(\?)/, function(all, capturingGroup, optional) {
return capturingGroup + '?'; // force all capturing groups to be optional
});
var example = reg.source.replace(/\((.+)\)(\?)/, function(all, capturingGroupInner, optional) {
return capturingGroupInner.split('|')[0]; // Take the first token as an example
});
One issue I can already see is that Sugar regexes are designed to "over-parse", meaning that the string the week Monday
would parse even though it isn't natural. You might be able to tapdance around this issue, but there's no guarantees. For the examples maybe hardcoding ones to your liking might be a better solution. https://sugarjs.com/dates/ is a good place to start as it has a pre-vetted list of natural examples that will parse which should be a good representative of most. If you chose a few representative examples for each regex you might be able to get away with it...
I'm going to close this issue for now but feel free to respond if you have more questions.
I wonder how to approach building autocomplete system for Sugar Dates.
eg. If you write 'two days' it'd suggest few options like
etc.
Any ideas how to 'catch it'?