Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private hasTokenIndex(source: string, regex: RegExp): { matched: boolean, index: number } {
// This part is different from C# because no Regex RightToLeft option in JS
let result = { matched: false, index: -1 };
let matchResult = RegExpUtility.getMatches(regex, source);
let match = matchResult[matchResult.length - 1];
if (match && !source.substr(match.index + match.length).trim().length) {
result.matched = true;
result.index = match.index;
}
return result;
}
}
private parseBasicRegexMatch(text: string, referenceTime: Date): DateTimeResolutionResult {
let trimmedText = text.trim().toLowerCase();
let offset = 0;
let matches = RegExpUtility.getMatches(this.config.atRegex, trimmedText);
if (matches.length === 0) {
matches = RegExpUtility.getMatches(this.config.atRegex, this.config.timeTokenPrefix + trimmedText);
offset = this.config.timeTokenPrefix.length;
}
if (matches.length > 0 && matches[0].index === offset && matches[0].length === trimmedText.length) {
return this.match2Time(matches[0], referenceTime);
}
// parse hour pattern, like "twenty one", "16"
// create a extract result which content the pass-in text
let hour = this.config.numbers.get(text) || Number(text);
if (hour) {
if (hour >= 0 && hour <= 24) {
let ret = new DateTimeResolutionResult();
if (hour === 24) {
hour = 0;
else {
min = Number.parseInt(minStr, 10);
hasMin = true;
}
// get second
let secStr = match.groups('sec').value.toLowerCase();
if (!StringUtility.isNullOrWhitespace(secStr)) {
second = Number.parseInt(secStr, 10);
hasSec = true;
}
}
// adjust by desc string
let descStr = match.groups('desc').value.toLowerCase();
if (RegExpUtility.getMatches(this.config.utilityConfiguration.amDescRegex, descStr).length > 0
|| RegExpUtility.getMatches(this.config.utilityConfiguration.amPmDescRegex, descStr).length > 0
|| !StringUtility.isNullOrEmpty(match.groups('iam').value)) {
if (hour >= 12) {
hour -= 12;
}
if (RegExpUtility.getMatches(this.config.utilityConfiguration.amPmDescRegex, descStr).length === 0) {
hasAm = true;
}
}
else if (RegExpUtility.getMatches(this.config.utilityConfiguration.pmDescRegex, descStr).length > 0
|| !StringUtility.isNullOrEmpty(match.groups('ipm').value)) {
if (hour < 12) {
hour += 12;
second = Number.parseInt(secStr, 10);
hasSec = true;
}
}
// adjust by desc string
let descStr = match.groups('desc').value.toLowerCase();
if (RegExpUtility.getMatches(this.config.utilityConfiguration.amDescRegex, descStr).length > 0
|| RegExpUtility.getMatches(this.config.utilityConfiguration.amPmDescRegex, descStr).length > 0
|| !StringUtility.isNullOrEmpty(match.groups('iam').value)) {
if (hour >= 12) {
hour -= 12;
}
if (RegExpUtility.getMatches(this.config.utilityConfiguration.amPmDescRegex, descStr).length === 0) {
hasAm = true;
}
}
else if (RegExpUtility.getMatches(this.config.utilityConfiguration.pmDescRegex, descStr).length > 0
|| !StringUtility.isNullOrEmpty(match.groups('ipm').value)) {
if (hour < 12) {
hour += 12;
}
hasPm = true;
}
// adjust min by prefix
let timePrefix = match.groups('prefix').value.toLowerCase();
if (!StringUtility.isNullOrWhitespace(timePrefix)) {
// adjust by desc string
let descStr = match.groups('desc').value.toLowerCase();
if (RegExpUtility.getMatches(this.config.utilityConfiguration.amDescRegex, descStr).length > 0
|| RegExpUtility.getMatches(this.config.utilityConfiguration.amPmDescRegex, descStr).length > 0
|| !StringUtility.isNullOrEmpty(match.groups('iam').value)) {
if (hour >= 12) {
hour -= 12;
}
if (RegExpUtility.getMatches(this.config.utilityConfiguration.amPmDescRegex, descStr).length === 0) {
hasAm = true;
}
}
else if (RegExpUtility.getMatches(this.config.utilityConfiguration.pmDescRegex, descStr).length > 0
|| !StringUtility.isNullOrEmpty(match.groups('ipm').value)) {
if (hour < 12) {
hour += 12;
}
hasPm = true;
}
// adjust min by prefix
let timePrefix = match.groups('prefix').value.toLowerCase();
if (!StringUtility.isNullOrWhitespace(timePrefix)) {
let adjust = { hour: hour, min: min, hasMin: hasMin };
this.config.adjustByPrefix(timePrefix, adjust);
hour = adjust.hour; min = adjust.min; hasMin = adjust.hasMin;
}
specialsRegexMatch(text: string, refDate: Date): Token[] {
let ret = [];
// handle "ish"
if (this.config.ishRegex !== null) {
let matches = RegExpUtility.getMatches(this.config.ishRegex, text);
matches.forEach(match => {
ret.push(new Token(match.index, match.index + match.length));
});
}
return ret;
}
}
private checkCalendarFilterList(ers: ExtractResult[], text: string) {
for (let er of ers.reverse()) {
for (let negRegex of this.config.filterWordRegexList) {
let match = RegExpUtility.getMatches(negRegex, er.text).pop();
if (match) {
ers.splice(ers.indexOf(er));
}
}
}
}
this.regexesDictionary.forEach((value, regex) => {
let matches = RegExpUtility.getMatches(regex, source);
if (matches.length > 0) {
collections.push({ matches: matches, value: value });
}
});
if (hour <= 12 && hour !== 0) {
ret.comment = "ampm";
}
ret.timex = "T" + DateTimeFormatUtil.toString(hour, 2);
ret.futureValue = ret.pastValue =
DateUtils.safeCreateFromMinValue(referenceTime.getFullYear(), referenceTime.getMonth(), referenceTime.getDate(), hour, 0, 0);
ret.success = true;
return ret;
}
}
for (let regex of this.config.timeRegexes) {
offset = 0;
matches = RegExpUtility.getMatches(regex, trimmedText);
if (matches.length && matches[0].index === offset && matches[0].length === trimmedText.length) {
return this.match2Time(matches[0], referenceTime);
}
}
return new DateTimeResolutionResult();
}
extractResults.forEach(extractResult => {
if (extractResult.type === Constants.SYS_DATETIME_TIME
|| extractResult.type === Constants.SYS_DATETIME_DATETIME) {
let stringAfter = text.substring(extractResult.start + extractResult.length);
let match = RegExpUtility.getMatches(this.config.numberEndingPattern, stringAfter);
if (match != null && match.length) {
let newTime = match[0].groups("newTime");
let numRes = this.config.integerExtractor.extract(newTime.value);
if (numRes.length === 0) {
return;
}
let startPosition = extractResult.start + extractResult.length + newTime.index;
tokens.push(new Token(startPosition, startPosition + newTime.length));
}
}
});