Created
June 3, 2020 17:56
-
-
Save aljosamrak/b9544f0126ff15ea5026ab106b9e3756 to your computer and use it in GitHub Desktop.
Modification to https://github.com/defmethodinc/just-not-sorry to check dates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addWarning(node, regex, message) { | |
// create regex with a gontext group and insede a date regex group | |
const pattern = new RegExp('([^.,;\\n]*(' + regex + ')[^.,;\\n]*)', 'ig'); | |
const promises = []; | |
const warningClass = this.warningClass; | |
const promisifiedMatchCallback = (match, range) => { | |
// parse teh date - use just the default parses | |
const date = new Date(Date.parse(match[2])) | |
// check if there is any day name present in the date context | |
const text = match[1].toLowerCase() | |
DAY_NAMES.forEach(function (dayName, index) { | |
if (text.includes(dayName)) { | |
if (index !== date.getDay()) { | |
// create the mighlighting with a mesage | |
const matchPromise = HighlightGenerator.highlightMatches( | |
`The date '${match[2]}' is not a '${DAY_NAMES[index]}' but it is a '${DAY_NAMES[date.getDay()]}'`, | |
warningClass, | |
node | |
).call(node, match, range); | |
promises.push(matchPromise); | |
} | |
} | |
}); | |
// ignore the hour | |
date.setHours(0,0,0,0) | |
// search through all saved events from Google calendar | |
events.forEach( event => { | |
if (event.date.getTime() === date.getTime()) { | |
// create the mighlighting with a mesage | |
const matchPromise = HighlightGenerator.highlightMatches( | |
`You have an event on that date '${match[2]}'. '${event.name}'`, | |
warningClass, | |
node | |
).call(node, match, range); | |
promises.push(matchPromise); | |
} | |
}) | |
}; | |
domRegexpMatch(node, pattern, promisifiedMatchCallback); | |
return Promise.all(promises); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment