Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
protected parseEachUnit(text: string): DateTimeResolutionResult {
let ret = new DateTimeResolutionResult();
// handle "each month"
let match = RegExpUtility.getMatches(this.config.eachUnitRegex, text).pop();
if (!match || match.length !== text.length) {
return ret;
}
let sourceUnit = match.groups("unit").value;
if (StringUtility.isNullOrEmpty(sourceUnit) || !this.config.unitMap.has(sourceUnit)) {
return ret;
}
let getMatchedUnitTimex = this.config.getMatchedUnitTimex(sourceUnit);
if (!getMatchedUnitTimex.matched) {
return ret;
}
ret.timex = getMatchedUnitTimex.timex;
ret.futureValue = "Set: " + ret.timex;
ret.pastValue = "Set: " + ret.timex;
ret.success = true;
return ret;
}
if (descCaptureIndex >= time1StartIndex && descCaptureIndex + descCapture.length <= time1EndIndex && StringUtility.isNullOrEmpty(leftDesc)) {
leftDesc = descCapture;
}
else if (descCaptureIndex >= time2StartIndex && descCaptureIndex + descCapture.length <= time2EndIndex && StringUtility.isNullOrEmpty(rightDesc)) {
rightDesc = descCapture;
}
lastGroupIndex = descCaptureIndex + 1;
}
let beginDateTime = DateUtils.safeCreateFromMinValue(year, month, day, beginHour, beginMinute >= 0 ? beginMinute : 0, beginSecond >= 0 ? beginSecond : 0);
let endDateTime = DateUtils.safeCreateFromMinValue(year, month, day, endHour, endMinute >= 0 ? endMinute : 0, endSecond >= 0 ? endSecond : 0);
let hasLeftAm = !StringUtility.isNullOrEmpty(leftDesc) && leftDesc.toLowerCase().startsWith('a');
let hasLeftPm = !StringUtility.isNullOrEmpty(leftDesc) && leftDesc.toLowerCase().startsWith('p');
let hasRightAm = !StringUtility.isNullOrEmpty(rightDesc) && rightDesc.toLowerCase().startsWith('a');
let hasRightPm = !StringUtility.isNullOrEmpty(rightDesc) && rightDesc.toLowerCase().startsWith('p');
let hasLeft = hasLeftAm || hasLeftPm;
let hasRight = hasRightAm || hasRightPm;
// Both timepoint has description like 'am' or 'pm'
if (hasLeft && hasRight) {
if (hasLeftAm) {
if (beginHour >= 12) {
beginDateTime = DateUtils.addHours(beginDateTime, -12);
}
}
else if (hasLeftPm) {
if (beginHour < 12) {
beginDateTime = DateUtils.addHours(beginDateTime, 12);
}
while (j < result.length && result[j].start + result[j].length < numErs[i].start) {
hasBehindExtraction = true;
j++;
}
if (!hasBehindExtraction) {
continue;
}
let middleBegin = result[j - 1].start + result[j - 1].length;
let middleEnd = numErs[i].start;
let middleStr = source.substring(middleBegin, middleEnd).trim().toLowerCase();
// Separated by whitespace
if (StringUtility.isNullOrEmpty(middleStr)) {
unitNumbers.push(numErs[i]);
continue;
}
// Separated by connectors
let match = RegExpUtility.getMatches(this.config.compoundUnitConnectorRegex, middleStr).pop();
if (match && match.index === 0 && match.length === middleStr.length) {
unitNumbers.push(numErs[i]);
}
}
unitNumbers.forEach(extractResult => {
let overlap = false;
result.forEach(er => {
if (er.start <= extractResult.start && er.start + er.length >= extractResult.start) {
overlap = true;
}
lastGroupIndex = secondCaptureIndex + 1;
}
lastGroupIndex = 0;
// Desc here means descriptions like "am / pm / o'clock"
// Get leftDesc (if exists) and rightDesc (if exists)
let leftDesc = match.groups('leftDesc').value;
let rightDesc = match.groups('rightDesc').value;
for (let i = 0; i < match.groups('desc').captures.length; i++) {
let descCapture = match.groups('desc').captures[i];
let descCaptureIndex = source.indexOf(descCapture, lastGroupIndex);
if (descCaptureIndex >= time1StartIndex && descCaptureIndex + descCapture.length <= time1EndIndex && StringUtility.isNullOrEmpty(leftDesc)) {
leftDesc = descCapture;
}
else if (descCaptureIndex >= time2StartIndex && descCaptureIndex + descCapture.length <= time2EndIndex && StringUtility.isNullOrEmpty(rightDesc)) {
rightDesc = descCapture;
}
lastGroupIndex = descCaptureIndex + 1;
}
let beginDateTime = DateUtils.safeCreateFromMinValue(year, month, day, beginHour, beginMinute >= 0 ? beginMinute : 0, beginSecond >= 0 ? beginSecond : 0);
let endDateTime = DateUtils.safeCreateFromMinValue(year, month, day, endHour, endMinute >= 0 ? endMinute : 0, endSecond >= 0 ? endSecond : 0);
let hasLeftAm = !StringUtility.isNullOrEmpty(leftDesc) && leftDesc.toLowerCase().startsWith('a');
let hasLeftPm = !StringUtility.isNullOrEmpty(leftDesc) && leftDesc.toLowerCase().startsWith('p');
let hasRightAm = !StringUtility.isNullOrEmpty(rightDesc) && rightDesc.toLowerCase().startsWith('a');
let hasRightPm = !StringUtility.isNullOrEmpty(rightDesc) && rightDesc.toLowerCase().startsWith('p');
private replaceUnit(value: string): string {
if (StringUtility.isNullOrEmpty(value)) {
return value;
}
let result = value;
this.config.unitMap.forEach((value: string, key: string) => {
result = result.replace(new RegExp(key, 'g'), value);
});
return result;
}
private parseAnUnit(source: string): DateTimeResolutionResult {
let result = new DateTimeResolutionResult();
let match = RegExpUtility.getMatches(this.config.anUnitRegex, source).pop();
if (!match) {
match = RegExpUtility.getMatches(this.config.halfDateUnitRegex, source).pop();
}
if (!match) {
return result;
}
let num = StringUtility.isNullOrEmpty(match.groups('half').value) ? 1 : 0.5;
num += this.parseNumberWithUnitAndSuffix(source);
let sourceUnit = match.groups('unit').value;
if (this.config.unitMap.has(sourceUnit)) {
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;
}
return result;
}
}
if (!match || match.length !== source.length) {
return result;
}
let cardinalStr = match.groups('cardinal').value;
let yearStr = match.groups('year').value;
let orderQuarterStr = match.groups('orderQuarter').value;
let orderStr = StringUtility.isNullOrEmpty(orderQuarterStr) ? match.groups('order').value : '';
let numberStr = match.groups('number').value;
let noSpecificYear = false;
let year = Number.parseInt(yearStr, 10);
if (isNaN(year)) {
let swift = StringUtility.isNullOrEmpty(orderQuarterStr) ? this.config.getSwiftYear(orderStr) : 0;
if (swift < -1) {
swift = 0;
noSpecificYear = true;
}
year = referenceDate.getFullYear() + swift;
}
let quarterNum: number;
if (!StringUtility.isNullOrEmpty(cardinalStr)) {
quarterNum = this.config.cardinalMap.get(cardinalStr);
}
else if (!StringUtility.isNullOrEmpty(orderQuarterStr)) {
let month = referenceDate.getMonth() + 1;
quarterNum = Math.ceil(month / Constants.TrimesterMonthCount);
let swift = this.config.getSwiftYear(orderQuarterStr);
quarterNum += swift;
private isDigitCJK(value: string): boolean {
return !StringUtility.isNullOrEmpty(value)
&& RegExpUtility.isMatch(this.config.digitNumRegex, value);
}
}
private handleChinese(extra: DateTimeExtra): TimeResult {
let hour = this.matchToValue(extra.namedEntity('hour').value);
let quarter = this.matchToValue(extra.namedEntity('quarter').value);
let minute = !StringUtility.isNullOrEmpty(extra.namedEntity('half').value)
? 30
: quarter !== -1 ? quarter * 15
: this.matchToValue(extra.namedEntity('min').value);
let second = this.matchToValue(extra.namedEntity('sec').value);
return new TimeResult(hour, minute, second);
}