Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
chars.forEach(c => {
let codePoint = c.codePointAt(0) || c.charAt(0);
if (codePoint > 0xFFFF) {
// Character is in a Supplementary Unicode Plane. This is where emoji live so
// we're going to just break each character in this range out as its own token.
tokens.push(c);
if (!StringUtility.isNullOrWhitespace(token)) {
tokens.push(token);
token = '';
}
}
else if (!(this.config.tokenRegex.test(c) || StringUtility.isWhitespace(c))) {
token = token.concat(c);
}
else if (!StringUtility.isNullOrWhitespace(token)) {
tokens.push(token);
token = '';
}
});
afterHourIndex = trimmedText.indexOf(hourStr, hourGroup.index + 1) + hourStr.length;
if (afterHourIndex === trimmedText.length || !trimmedText.substr(afterHourIndex).trim().startsWith(':')) {
let endHour = this.config.numbers.get(hourStr);
if (!endHour) {
endHour = Number.parseInt(hourStr, 10);
}
// parse "pm"
let leftDesc = matches[0].groups("leftDesc").value;
let rightDesc = matches[0].groups("rightDesc").value;
let pmStr = matches[0].groups("pm").value;
let amStr = matches[0].groups("am").value;
// The "ampm" only occurs in time, don't have to consider it here
if (StringUtility.isNullOrWhitespace(leftDesc)) {
let rightAmValid = !StringUtility.isNullOrEmpty(rightDesc) &&
RegExpUtility.getMatches(this.config.utilityConfiguration.amDescRegex, rightDesc.toLowerCase()).length;
let rightPmValid = !StringUtility.isNullOrEmpty(rightDesc) &&
RegExpUtility.getMatches(this.config.utilityConfiguration.pmDescRegex, rightDesc.toLowerCase()).length;
if (!StringUtility.isNullOrEmpty(amStr) || rightAmValid) {
if (endHour >= 12) {
endHour -= 12;
}
if (beginHour >= 12 && beginHour - 12 < endHour) {
beginHour -= 12;
}
// Resolve case like "11 to 3am"
if (beginHour < 12 && beginHour > endHour) {
this.config.timeExtractor.extract(source, refDate).forEach(er => {
let afterStr = source.substr(er.start + er.length);
if (StringUtility.isNullOrWhitespace(afterStr) && this.config.beforeEachDayRegex) {
let beforeStr = source.substr(0, er.start);
let beforeMatches = RegExpUtility.getMatches(this.config.beforeEachDayRegex, beforeStr);
if (beforeMatches && beforeMatches.length > 0) {
ret.push(new Token(beforeMatches[0].index, er.start + er.length));
}
}
else {
let afterMatches = RegExpUtility.getMatches(this.config.eachDayRegex, afterStr);
if (afterMatches && afterMatches.length > 0) {
ret.push(new Token(er.start, er.start + er.length + afterMatches[0].length));
}
}
});
return ret;
ers.forEach(er => {
let beforeStr = source.substr(0, er.start);
let innerMatches = RegExpUtility.getMatches(this.config.nightRegex, er.text);
if (innerMatches && innerMatches.length > 0 && innerMatches[0].index === 0) {
beforeStr = source.substr(0, er.start + innerMatches[0].length);
}
if (StringUtility.isNullOrWhitespace(beforeStr)) {
return;
}
let matches = RegExpUtility.getMatches(this.config.timeOfTodayBeforeRegex, beforeStr);
if (matches && matches.length > 0) {
let begin = matches[0].index;
let end = er.start + er.length;
tokens.push(new Token(begin, end));
}
});
RegExpUtility.getMatches(this.config.simpleTimeOfTodayBeforeRegex, source)
let beforeStr = source.substr(0, match.index);
if (!StringUtility.isNullOrWhitespace(beforeStr)) {
let ers = this.config.singleDateExtractor.extract(beforeStr, refDate);
if (ers && ers.length > 0) {
let er = ers[ers.length - 1];
let begin = er.start;
let end = er.start + er.length;
let middleStr = beforeStr.substr(begin + er.length).trim().toLowerCase();
if (StringUtility.isNullOrWhitespace(middleStr) || RegExpUtility.getMatches(this.config.prepositionRegex, middleStr).length > 0) {
tokens.push(new Token(begin, match.index + match.length));
hasBeforeDate = true;
}
}
}
let followedStr = source.substr(match.index + match.length);
if (!StringUtility.isNullOrWhitespace(followedStr) && !hasBeforeDate) {
let ers = this.config.singleDateExtractor.extract(followedStr, refDate);
if (ers && ers.length > 0) {
let er = ers[0];
let begin = er.start;
let end = er.start + er.length;
let middleStr = followedStr.substr(0, begin).trim().toLowerCase();
if (StringUtility.isNullOrWhitespace(middleStr) || RegExpUtility.getMatches(this.config.prepositionRegex, middleStr).length > 0) {
tokens.push(new Token(match.index, match.index + match.length + end));
}
}
}
});
});
private replaceFullWithHalf(value: string): string {
if (StringUtility.isNullOrWhitespace(value)) {
return value;
}
let result = '';
for(let index = 0; index < value.length; index++) {
result = result.concat(this.config.fullToHalfMap.get(value.charAt(index)) || value.charAt(index));
}
return result;
}
ers.forEach(er => {
let afterStr = source.substr(er.start + er.length);
if (StringUtility.isNullOrWhitespace(afterStr)) {
return;
}
let matches = RegExpUtility.getMatches(this.config.timeOfTodayAfterRegex, afterStr);
if (matches && matches.length > 0) {
let begin = er.start;
let end = er.start + er.length + matches[0].length;
tokens.push(new Token(begin, end));
}
});
RegExpUtility.getMatches(this.config.simpleTimeOfTodayAfterRegex, source)
if (ers.length === 0 || ers[0].length !== beforeStr.length) {
let valid = false;
if (ers.length > 0 && ers[0].start === 0) {
let midStr = beforeStr.substr(ers[0].start + ers[0].length || 0);
if (StringUtility.isNullOrWhitespace(midStr.replace(',', ' '))) {
valid = true;
}
}
if (!valid) {
ers = this.config.dateExtractor.extract(afterStr);
if (ers.length === 0 || ers[0].length !== afterStr.length) {
if (ers.length > 0 && ers[0].start + ers[0].length === afterStr.length) {
let midStr = afterStr.substr(0, ers[0].start || 0);
if (StringUtility.isNullOrWhitespace(midStr.replace(',', ' '))) {
valid = true;
}
}
}
else {
valid = true;
}
if (!valid) {
return result;
}
}
}
let hasSpecificTimePeriod = false;
if (timePeriodErs.length > 0) {
values.forEach(token => {
if (StringUtility.isNullOrWhitespace(token) || dictionary.has(token)) {
return;
}
dictionary.set(token, key);
});
}