mirror of
https://github.com/xuthus83/pigallery2.git
synced 2024-11-03 21:04:03 +08:00
Improving date pattern autocomplete #660
This commit is contained in:
parent
27380c73c4
commit
167fe9e2f0
@ -161,6 +161,10 @@ export class SearchQueryParser {
|
|||||||
return timestamp;
|
return timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static humanToRegexpStr(str: string) {
|
||||||
|
return str.replace(/%d/g, '\\d*');
|
||||||
|
}
|
||||||
|
|
||||||
public parse(str: string, implicitAND = true): SearchQueryDTO {
|
public parse(str: string, implicitAND = true): SearchQueryDTO {
|
||||||
str = str
|
str = str
|
||||||
.replace(/\s\s+/g, ' ') // remove double spaces
|
.replace(/\s\s+/g, ' ') // remove double spaces
|
||||||
@ -168,9 +172,6 @@ export class SearchQueryParser {
|
|||||||
.trim();
|
.trim();
|
||||||
|
|
||||||
|
|
||||||
const humanToRegexpStr = (str: string) => {
|
|
||||||
return str.replace(/%d/g, '\\d*');
|
|
||||||
};
|
|
||||||
const intFromRegexp = (str: string) => {
|
const intFromRegexp = (str: string) => {
|
||||||
return parseInt(new RegExp(/\d+/).exec(str)[0], 10);
|
return parseInt(new RegExp(/\d+/).exec(str)[0], 10);
|
||||||
};
|
};
|
||||||
@ -365,7 +366,7 @@ export class SearchQueryParser {
|
|||||||
|
|
||||||
|
|
||||||
if (kwStartsWith(str, this.keywords.sameDay) ||
|
if (kwStartsWith(str, this.keywords.sameDay) ||
|
||||||
new RegExp('^' + humanToRegexpStr(this.keywords.lastNDays) + '!?:').test(str)) {
|
new RegExp('^' + SearchQueryParser.humanToRegexpStr(this.keywords.lastNDays) + '!?:').test(str)) {
|
||||||
const freqStr = str.slice(str.indexOf(':') + 1);
|
const freqStr = str.slice(str.indexOf(':') + 1);
|
||||||
let freq: DatePatternFrequency = null;
|
let freq: DatePatternFrequency = null;
|
||||||
let ago;
|
let ago;
|
||||||
@ -375,16 +376,16 @@ export class SearchQueryParser {
|
|||||||
freq = DatePatternFrequency.every_month;
|
freq = DatePatternFrequency.every_month;
|
||||||
} else if (freqStr == this.keywords.every_year) {
|
} else if (freqStr == this.keywords.every_year) {
|
||||||
freq = DatePatternFrequency.every_year;
|
freq = DatePatternFrequency.every_year;
|
||||||
} else if (new RegExp('^' + humanToRegexpStr(this.keywords.days_ago) + '$').test(freqStr)) {
|
} else if (new RegExp('^' + SearchQueryParser.humanToRegexpStr(this.keywords.days_ago) + '$').test(freqStr)) {
|
||||||
freq = DatePatternFrequency.days_ago;
|
freq = DatePatternFrequency.days_ago;
|
||||||
ago = intFromRegexp(freqStr);
|
ago = intFromRegexp(freqStr);
|
||||||
} else if (new RegExp('^' + humanToRegexpStr(this.keywords.weeks_ago) + '$').test(freqStr)) {
|
} else if (new RegExp('^' + SearchQueryParser.humanToRegexpStr(this.keywords.weeks_ago) + '$').test(freqStr)) {
|
||||||
freq = DatePatternFrequency.weeks_ago;
|
freq = DatePatternFrequency.weeks_ago;
|
||||||
ago = intFromRegexp(freqStr);
|
ago = intFromRegexp(freqStr);
|
||||||
} else if (new RegExp('^' + humanToRegexpStr(this.keywords.months_ago) + '$').test(freqStr)) {
|
} else if (new RegExp('^' + SearchQueryParser.humanToRegexpStr(this.keywords.months_ago) + '$').test(freqStr)) {
|
||||||
freq = DatePatternFrequency.months_ago;
|
freq = DatePatternFrequency.months_ago;
|
||||||
ago = intFromRegexp(freqStr);
|
ago = intFromRegexp(freqStr);
|
||||||
} else if (new RegExp('^' + humanToRegexpStr(this.keywords.years_ago) + '$').test(freqStr)) {
|
} else if (new RegExp('^' + SearchQueryParser.humanToRegexpStr(this.keywords.years_ago) + '$').test(freqStr)) {
|
||||||
freq = DatePatternFrequency.years_ago;
|
freq = DatePatternFrequency.years_ago;
|
||||||
ago = intFromRegexp(freqStr);
|
ago = intFromRegexp(freqStr);
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,15 @@ export class AutoCompleteService {
|
|||||||
k !== this.searchQueryParserService.keywords.kmFrom &&
|
k !== this.searchQueryParserService.keywords.kmFrom &&
|
||||||
k !== this.searchQueryParserService.keywords.NSomeOf &&
|
k !== this.searchQueryParserService.keywords.NSomeOf &&
|
||||||
k !== this.searchQueryParserService.keywords.minRating &&
|
k !== this.searchQueryParserService.keywords.minRating &&
|
||||||
k !== this.searchQueryParserService.keywords.maxRating
|
k !== this.searchQueryParserService.keywords.maxRating &&
|
||||||
|
k !== this.searchQueryParserService.keywords.every_week &&
|
||||||
|
k !== this.searchQueryParserService.keywords.every_month &&
|
||||||
|
k !== this.searchQueryParserService.keywords.every_year &&
|
||||||
|
k !== this.searchQueryParserService.keywords.weeks_ago &&
|
||||||
|
k !== this.searchQueryParserService.keywords.days_ago &&
|
||||||
|
k !== this.searchQueryParserService.keywords.months_ago &&
|
||||||
|
k !== this.searchQueryParserService.keywords.years_ago &&
|
||||||
|
k !== this.searchQueryParserService.keywords.lastNDays
|
||||||
)
|
)
|
||||||
.map((k) => k + ':');
|
.map((k) => k + ':');
|
||||||
|
|
||||||
@ -40,6 +48,11 @@ export class AutoCompleteService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (let i = 1; i < 5; i++) {
|
||||||
|
this.keywords.push(
|
||||||
|
this.searchQueryParserService.keywords.lastNDays.replace(/%d/g, i.toString()) + ':'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
this.keywords.push(
|
this.keywords.push(
|
||||||
this.searchQueryParserService.keywords.to +
|
this.searchQueryParserService.keywords.to +
|
||||||
@ -303,6 +316,7 @@ export class AutoCompleteService {
|
|||||||
ret.push(generateMatch(mrKey));
|
ret.push(generateMatch(mrKey));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (text.current.toLowerCase().startsWith(mxrKey)) {
|
if (text.current.toLowerCase().startsWith(mxrKey)) {
|
||||||
for (let i = 1; i <= 5; ++i) {
|
for (let i = 1; i <= 5; ++i) {
|
||||||
ret.push(generateMatch(mxrKey + i));
|
ret.push(generateMatch(mxrKey + i));
|
||||||
@ -311,6 +325,23 @@ export class AutoCompleteService {
|
|||||||
ret.push(generateMatch(mxrKey));
|
ret.push(generateMatch(mxrKey));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Date patterns
|
||||||
|
if (new RegExp('^' +
|
||||||
|
SearchQueryParser.humanToRegexpStr(this.searchQueryParserService.keywords.lastNDays) + '!?:$', 'i')
|
||||||
|
.test(text.current) ||
|
||||||
|
new RegExp('^' + this.searchQueryParserService.keywords.sameDay + '!?:$', 'i')
|
||||||
|
.test(text.current)) {
|
||||||
|
ret.push(generateMatch(text.current + this.searchQueryParserService.keywords.every_week));
|
||||||
|
ret.push(generateMatch(text.current + this.searchQueryParserService.keywords.every_month));
|
||||||
|
ret.push(generateMatch(text.current + this.searchQueryParserService.keywords.every_year));
|
||||||
|
|
||||||
|
ret.push(generateMatch(text.current + this.searchQueryParserService.keywords.days_ago.replace(/%d/g, '2')));
|
||||||
|
ret.push(generateMatch(text.current + this.searchQueryParserService.keywords.weeks_ago.replace(/%d/g, '2')));
|
||||||
|
ret.push(generateMatch(text.current + this.searchQueryParserService.keywords.months_ago.replace(/%d/g, '2')));
|
||||||
|
ret.push(generateMatch(text.current + this.searchQueryParserService.keywords.years_ago.replace(/%d/g, '2')));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user