mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
!timezone: add fuzzy matching for timezone name; add reset
This commit is contained in:
parent
4ae8cf85a3
commit
753ceda5ec
6 changed files with 90 additions and 7 deletions
32
backend/src/utils/parseFuzzyTimezone.ts
Normal file
32
backend/src/utils/parseFuzzyTimezone.ts
Normal file
|
@ -0,0 +1,32 @@
|
|||
import moment from "moment-timezone";
|
||||
import escapeStringRegexp from "escape-string-regexp";
|
||||
|
||||
const normalizeTzName = str => str.replace(/[^a-zA-Z0-9+\-]/g, "").toLowerCase();
|
||||
|
||||
const validTimezones = moment.tz.names();
|
||||
const normalizedTimezoneMap = validTimezones.reduce((map, tz) => {
|
||||
map.set(normalizeTzName(tz), tz);
|
||||
return map;
|
||||
}, new Map());
|
||||
const normalizedTimezones = Array.from(normalizedTimezoneMap.keys());
|
||||
|
||||
export function parseFuzzyTimezone(input: string) {
|
||||
const normalizedInput = normalizeTzName(input);
|
||||
|
||||
if (normalizedTimezoneMap.has(normalizedInput)) {
|
||||
return normalizedTimezoneMap.get(normalizedInput);
|
||||
}
|
||||
|
||||
const searchRegex = new RegExp(`.*${escapeStringRegexp(normalizedInput)}.*`);
|
||||
for (const tz of normalizedTimezones) {
|
||||
if (searchRegex.test(tz)) {
|
||||
const result = normalizedTimezoneMap.get(tz);
|
||||
// Ignore Etc/GMT timezones unless explicitly specified, as they have confusing functionality
|
||||
// with the inverted +/- sign
|
||||
if (result.startsWith("Etc/GMT")) continue;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue