Skip to content

Instantly share code, notes, and snippets.

@naugtur
Created November 16, 2019 22:15
Show Gist options
  • Save naugtur/962a4b4e008268d49d2476ad170141f3 to your computer and use it in GitHub Desktop.
Save naugtur/962a4b4e008268d49d2476ad170141f3 to your computer and use it in GitHub Desktop.
const locales = ['en', 'pl']
export function likelyContainsTodaysDate(text: string): number {
text = text.toLocaleLowerCase()
const today = new Date()
const numDay = today.getDate()
const numMonth = today.getUTCMonth() + 1
const separators = ['.', '/', '-']
const vocabulary = [
...locales.map(loc => today.toLocaleString(loc, { month: 'long' })),
...locales.map(loc => today.toLocaleString(loc, { month: 'short' })),
...separators.map(se => numDay + se + numMonth),
...separators.map(se => numMonth + se + numDay),
'' + numMonth,
'' + numDay
]
return vocabulary.reduce((score, bit) => {
if (text.includes(bit)) {
score++
}
return score;
}, 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment