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;
}
}
return ret;
}
}
}
// get minute
let minStr = match.groups('min').value.toLowerCase();
if (StringUtility.isNullOrWhitespace(minStr)) {
minStr = match.groups('minnum').value;
if (!StringUtility.isNullOrWhitespace(minStr)) {
min = this.config.numbers.get(minStr);
hasMin = true;
}
let tensStr = match.groups('tens').value;
if (!StringUtility.isNullOrWhitespace(tensStr)) {
min += this.config.numbers.get(tensStr);
hasMin = true;
}
}
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;
}
}
}
else {
hour = Number.parseInt(hourStr, 10);
if (!hour) {
hour = this.config.numbers.get(hourStr);
if (!hour) {
return ret;
}
}
}
// get minute
let minStr = match.groups('min').value.toLowerCase();
if (StringUtility.isNullOrWhitespace(minStr)) {
minStr = match.groups('minnum').value;
if (!StringUtility.isNullOrWhitespace(minStr)) {
min = this.config.numbers.get(minStr);
hasMin = true;
}
let tensStr = match.groups('tens').value;
if (!StringUtility.isNullOrWhitespace(tensStr)) {
min += this.config.numbers.get(tensStr);
hasMin = true;
}
}
else {
min = Number.parseInt(minStr, 10);
hasMin = true;
}
// get second
}
else if (!StringUtility.isNullOrWhitespace(match.groups('midafternoon').value)) {
hour = 14;
min = 0;
second = 0;
}
else if (!StringUtility.isNullOrWhitespace(match.groups('midday').value)) {
hour = 12;
min = 0;
second = 0;
}
}
else {
// get hour
let hourStr = match.groups('hour').value;
if (StringUtility.isNullOrWhitespace(hourStr)) {
hourStr = match.groups('hournum').value.toLowerCase();
hour = this.config.numbers.get(hourStr);
if (!hour) {
return ret;
}
}
else {
hour = Number.parseInt(hourStr, 10);
if (!hour) {
hour = this.config.numbers.get(hourStr);
if (!hour) {
return ret;
}
}
}
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;
}
// adjust hour by suffix
let timeSuffix = match.groups('suffix').value.toLowerCase();
if (!StringUtility.isNullOrWhitespace(timeSuffix)) {
let adjust = { hour: hour, min: min, hasMin: hasMin, hasAm: hasAm, hasPm: hasPm };
this.config.adjustBySuffix(timeSuffix, adjust);
hour = adjust.hour; min = adjust.min; hasMin = adjust.hasMin; hasAm = adjust.hasAm; hasPm = adjust.hasPm;
}
if (hour === 24) {
hour = 0;
}