Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
pastDate.setMonth(pastDate.getMonth() - 1);
}
}
else {
futureDate = DateUtils.safeCreateFromMinValue(year, month + 1, day);
pastDate = DateUtils.safeCreateFromMinValue(year, month - 1, day);
}
result.futureValue = futureDate;
result.pastValue = pastDate;
result.success = true;
return result;
}
// handle "today", "the day before yesterday"
match = RegExpUtility.getMatches(this.config.specialDayRegex, trimmedSource).pop();
if (match && match.index === 0 && match.length === trimmedSource.length) {
let swift = this.config.getSwiftDay(match.value);
let today = DateUtils.safeCreateFromMinValue(referenceDate.getFullYear(), referenceDate.getMonth(), referenceDate.getDate());
let value = DateUtils.addDays(today, swift);
result.timex = DateTimeFormatUtil.luisDateFromDate(value);
result.futureValue = value;
result.pastValue = value;
result.success = true;
return result;
}
// handle "two days from tomorrow"
match = RegExpUtility.getMatches(this.config.specialDayWithNumRegex, trimmedSource).pop();
if (match && match.index === 0 && match.length === trimmedSource.length) {
let swift = this.config.getSwiftDay(match.groups('day').value);
let numErs = this.config.integerExtractor.extract(trimmedSource);
er.forEach(result => {
let num = toNumber(this.config.numberParser.parse(result).value);
if (num < 1 || num > 31) {
return;
}
if (result.start >= 0) {
let frontString = source.substring(0, result.start | 0);
let match = RegExpUtility.getMatches(this.config.monthEnd, frontString)[0];
if (match && match.length) {
ret.push(new Token(match.index, match.index + match.length + result.length));
return;
}
// handling cases like 'for the 25th'
let matches = RegExpUtility.getMatches(this.config.forTheRegex, source);
let isFound = false;
matches.forEach(matchCase => {
if (matchCase) {
let ordinalNum = matchCase.groups('DayOfMonth').value;
if (ordinalNum === result.text) {
let length = matchCase.groups('end').value.length;
ret.push(new Token(matchCase.index, matchCase.index + matchCase.length - length));
isFound = true;
}
}
});
if (isFound) {
return;
}
private matchNumberWithUnit(source: string): Token[] {
let tokens: Token[] = new Array();
let durations = new Array();
this.config.integerExtractor.extract(source).forEach(er => {
let afterStr = source.substr(er.start + er.length);
let followedUnitMatch = RegExpUtility.getMatches(this.config.followedUnit, afterStr).pop();
if (followedUnitMatch && followedUnitMatch.index === 0) {
durations.push(new Token(er.start, er.start + er.length + followedUnitMatch.length));
}
});
RegExpUtility.getMatches(this.config.numberCombinedWithUnit, source).forEach(match => {
durations.push(new Token(match.index, match.index + match.length));
});
durations.forEach(duration => {
let beforeStr = source.substr(0, duration.start).toLowerCase();
if (StringUtility.isNullOrWhitespace(beforeStr)) {
return;
}
let match = RegExpUtility.getMatches(this.config.pastRegex, beforeStr).pop();
if (match && StringUtility.isNullOrWhitespace(beforeStr.substr(match.index + match.length))) {
tokens.push(new Token(match.index, duration.end));
return;
}
match = RegExpUtility.getMatches(this.config.futureRegex, beforeStr).pop();
getMatchedTimeRange(source: string): { timeStr: string, beginHour: number, endHour: number, endMin: number, success: boolean } {
let timeStr: string;
let beginHour = 0;
let endHour = 0;
let endMin = 0;
let success = false;
if (RegExpUtility.getMatches(this.morningStartEndRegex, source).length > 0) {
timeStr = 'TMO';
beginHour = 8;
endHour = 12;
success = true;
}
else if (RegExpUtility.getMatches(this.afternoonStartEndRegex, source).length > 0) {
timeStr = 'TAF';
beginHour = 12;
endHour = 16;
success = true;
}
else if (RegExpUtility.getMatches(this.eveningStartEndRegex, source).length > 0) {
timeStr = 'TEV';
beginHour = 16;
endHour = 20;
success = true;
private getResultFromRegex(regex: RegExp, source: string, num: number): DateTimeResolutionResult {
let result = new DateTimeResolutionResult();
let match = RegExpUtility.getMatches(regex, source).pop();
if (!match) {
return result;
}
let sourceUnit = match.groups('unit').value;
if (!this.config.unitMap.has(sourceUnit)) {
return result;
}
let unitStr = this.config.unitMap.get(sourceUnit);
result.timex = `P${this.isLessThanDay(unitStr) ? 'T' : ''}${num}${unitStr[0]}`;
result.futureValue = num * this.config.unitValueMap.get(sourceUnit);
result.pastValue = result.futureValue;
result.success = true;
return result;
}
date = DateUtils.addMonths(referenceDate, -number);
break;
case Constants.TimexYear:
date = DateUtils.addYears(referenceDate, -number);
break;
default:
return result;
}
result.timex = DateTimeFormatUtil.luisDateFromDate(date);
result.futureValue = result.pastValue = date;
result.success = true;
return result;
}
let afterMatch = RegExpUtility.getMatches(ChineseDateExtractor.afterRegex, suffix).pop();
if (afterMatch && suffix.startsWith(afterMatch.value)) {
let date: Date;
switch (unitStr) {
case Constants.TimexDay:
date = DateUtils.addDays(referenceDate, number);
break;
case Constants.TimexWeek:
date = DateUtils.addDays(referenceDate, 7 * number);
break;
case Constants.TimexMonthFull:
date = DateUtils.addMonths(referenceDate, number);
break;
case Constants.TimexYear:
date = DateUtils.addYears(referenceDate, number);
break;
default:
this.config.durationExtractor.extract(source, refDate).forEach(er => {
if (RegExpUtility.getMatches(this.config.lastRegex, er.text).length > 0) {
return;
}
let beforeStr = source.substr(0, er.start);
let matches = RegExpUtility.getMatches(this.config.eachPrefixRegex, beforeStr);
if (matches && matches.length > 0) {
ret.push(new Token(matches[0].index, er.start + er.length));
}
});
return ret;
else {
return tokens;
}
}
let idx = 0;
while (idx < er.length - 1) {
let middleBegin = er[idx].start + (er[idx].length || 0);
let middleEnd = er[idx + 1].start || 0;
if (middleBegin >= middleEnd) {
idx++;
continue;
}
let middleStr = source.substr(middleBegin, middleEnd - middleBegin).trim().toLowerCase();
let match = RegExpUtility.getMatches(this.config.tillRegex, middleStr);
if (match && match.length > 0 && match[0].index === 0 && match[0].length === middleStr.length) {
let periodBegin = er[idx].start || 0;
let periodEnd = (er[idx + 1].start || 0) + (er[idx + 1].length || 0);
let beforeStr = source.substring(0, periodBegin).trim().toLowerCase();
let fromTokenIndex = this.config.getFromTokenIndex(beforeStr);
let betweenTokenIndex = this.config.getBetweenTokenIndex(beforeStr);
if (fromTokenIndex.matched || betweenTokenIndex.matched) {
periodBegin = fromTokenIndex.matched ? fromTokenIndex.index : betweenTokenIndex.index;
}
tokens.push(new Token(periodBegin, periodEnd));
idx += 2;
continue;
}
if (this.config.hasConnectorToken(middleStr)) {
protected parseNowAsDate(source: string, referenceDate: Date): DateTimeParseResult {
let pr = new DateTimeParseResult();
let matches = RegExpUtility.getMatches(this.config.nowRegex, source);
if (matches.length) {
let value = DateUtils.safeCreateFromMinValue(referenceDate.getFullYear(), referenceDate.getMonth(), referenceDate.getDate());
let retNow = new DateTimeResolutionResult();
retNow.timex = DateTimeFormatUtil.luisDateFromDate(value);
retNow.futureValue = value;
retNow.pastValue = value;
pr.text = matches[0].value;
pr.start = matches[0].index;
pr.length = matches[0].length;
pr.value = retNow;
pr.timexStr = retNow.timex;
}
return pr;
}
protected parseEach(extractor: IDateTimeExtractor, parser: IDateTimeParser, text: string, refDate: Date): DateTimeResolutionResult {
let ret = new DateTimeResolutionResult();
let success = false;
let er: ExtractResult[];
let match = RegExpUtility.getMatches(this.config.setEachRegex, text).pop();
if (match) {
let trimmedText = text.substr(0, match.index) + text.substr(match.index + match.length);
er = extractor.extract(trimmedText, refDate);
if (er.length === 1 && er[0].length === trimmedText.length) {
success = true;
}
}
match = RegExpUtility.getMatches(this.config.setWeekDayRegex, text).pop();
if (match) {
let trimmedText = text.substr(0, match.index) + match.groups('weekday').value + text.substr(match.index + match.length);
er = extractor.extract(trimmedText, refDate);
if (er.length === 1 && er[0].length === trimmedText.length) {
success = true;
}
}
if (success) {