wanasit / chrono

A natural language date parser in Javascript
MIT License
4.61k stars 339 forks source link

Is it possible to support MMDDYYYY without hyphens or slashes? #477

Open daniel-gener8tor opened 2 years ago

wanasit commented 1 year ago

Sorry for my slow reply. I don't know if we want to have that in the default Chrono.

Would adding a custom parser work for your case? https://github.com/wanasit/chrono#parser

const custom = chrono.casual.clone();
custom.parsers.push({
    pattern: () => { return /\b(\d{2})(\d{2})(\d{4})\b/i },
    extract: (context, match) => {
        return {
            day: parseInt(match[2]), 
            month: parseInt(match[1]), 
            year: parseInt(match[3]), 
        }
    }
});

custom.parseDate("I'll arrive at 01142023");
arunr14 commented 7 months ago

Hi @wanasit

I know this is a very old issue but I wrote the exact same custom parser as your code above and it does not function as expected. The parsed result returned is []

I can confirm with debug statements that the pattern matches and the extract function is executed. However it does not return any results.

I tested your code above and my own version of it and neither work as expected. Do you have insight into this issue ?

arunr14 commented 7 months ago

We have identified the "UnlikelyFormatFilter" refiner as the reason why the parsedResult is being filtered out. We worked around it by replacing the refiner with a version that does NOT do the following refinement

if (result.text.replace(" ", "").match(/^\d(.\d)?$/)) { context.debug(() => { console.log(Removing unlikely result '${result.text}'); });

        return false;
    }

At the moment we are replacing the refiner using its index. We tried using constructor name but that did not work on production.

@wanasit could you expose the refiner names so users are able to replace/modify them as required.