Changeset View
Changeset View
Standalone View
Standalone View
lib/utils/url-utils.js
// @flow | // @flow | ||||
import { pendingThreadIDRegex } from '../shared/thread-utils.js'; | import { pendingThreadIDRegex } from '../shared/thread-utils.js'; | ||||
export type URLInfo = { | export type URLInfo = { | ||||
+year?: number, | +year?: number, | ||||
+month?: number, // 1-indexed | +month?: number, // 1-indexed | ||||
+verify?: string, | +verify?: string, | ||||
+calendar?: boolean, | +calendar?: boolean, | ||||
+chat?: boolean, | +chat?: boolean, | ||||
+apps?: boolean, | |||||
+thread?: string, | +thread?: string, | ||||
+settings?: 'account' | 'danger-zone', | +settings?: 'account' | 'danger-zone', | ||||
+threadCreation?: boolean, | +threadCreation?: boolean, | ||||
+selectedUserList?: $ReadOnlyArray<string>, | +selectedUserList?: $ReadOnlyArray<string>, | ||||
... | ... | ||||
}; | }; | ||||
// We use groups to capture parts of the URL and any changes | // We use groups to capture parts of the URL and any changes | ||||
// to regexes must be reflected in infoFromURL. | // to regexes must be reflected in infoFromURL. | ||||
const yearRegex = new RegExp('(/|^)year/([0-9]+)(/|$)', 'i'); | const yearRegex = new RegExp('(/|^)year/([0-9]+)(/|$)', 'i'); | ||||
const monthRegex = new RegExp('(/|^)month/([0-9]+)(/|$)', 'i'); | const monthRegex = new RegExp('(/|^)month/([0-9]+)(/|$)', 'i'); | ||||
const threadRegex = new RegExp('(/|^)thread/([0-9]+)(/|$)', 'i'); | const threadRegex = new RegExp('(/|^)thread/([0-9]+)(/|$)', 'i'); | ||||
const verifyRegex = new RegExp('(/|^)verify/([a-f0-9]+)(/|$)', 'i'); | const verifyRegex = new RegExp('(/|^)verify/([a-f0-9]+)(/|$)', 'i'); | ||||
const calendarRegex = new RegExp('(/|^)calendar(/|$)', 'i'); | const calendarRegex = new RegExp('(/|^)calendar(/|$)', 'i'); | ||||
const chatRegex = new RegExp('(/|^)chat(/|$)', 'i'); | const chatRegex = new RegExp('(/|^)chat(/|$)', 'i'); | ||||
const appsRegex = new RegExp('(/|^)apps(/|$)', 'i'); | |||||
const accountSettingsRegex = new RegExp('(/|^)settings/account(/|$)', 'i'); | const accountSettingsRegex = new RegExp('(/|^)settings/account(/|$)', 'i'); | ||||
const dangerZoneRegex = new RegExp('(/|^)settings/danger-zone(/|$)', 'i'); | const dangerZoneRegex = new RegExp('(/|^)settings/danger-zone(/|$)', 'i'); | ||||
const threadPendingRegex = new RegExp( | const threadPendingRegex = new RegExp( | ||||
`(/|^)thread/(${pendingThreadIDRegex})(/|$)`, | `(/|^)thread/(${pendingThreadIDRegex})(/|$)`, | ||||
'i', | 'i', | ||||
); | ); | ||||
const threadCreationRegex = new RegExp( | const threadCreationRegex = new RegExp( | ||||
'(/|^)thread/new(/([0-9]+([+][0-9]+)*))?(/|$)', | '(/|^)thread/new(/([0-9]+([+][0-9]+)*))?(/|$)', | ||||
'i', | 'i', | ||||
); | ); | ||||
function infoFromURL(url: string): URLInfo { | function infoFromURL(url: string): URLInfo { | ||||
const yearMatches = yearRegex.exec(url); | const yearMatches = yearRegex.exec(url); | ||||
const monthMatches = monthRegex.exec(url); | const monthMatches = monthRegex.exec(url); | ||||
const threadMatches = threadRegex.exec(url); | const threadMatches = threadRegex.exec(url); | ||||
const verifyMatches = verifyRegex.exec(url); | const verifyMatches = verifyRegex.exec(url); | ||||
const calendarTest = calendarRegex.test(url); | const calendarTest = calendarRegex.test(url); | ||||
const chatTest = chatRegex.test(url); | const chatTest = chatRegex.test(url); | ||||
const appsTest = appsRegex.test(url); | |||||
const accountSettingsTest = accountSettingsRegex.test(url); | const accountSettingsTest = accountSettingsRegex.test(url); | ||||
const dangerZoneTest = dangerZoneRegex.test(url); | const dangerZoneTest = dangerZoneRegex.test(url); | ||||
const threadPendingMatches = threadPendingRegex.exec(url); | const threadPendingMatches = threadPendingRegex.exec(url); | ||||
const threadCreateMatches = threadCreationRegex.exec(url); | const threadCreateMatches = threadCreationRegex.exec(url); | ||||
const returnObj = {}; | const returnObj = {}; | ||||
if (yearMatches) { | if (yearMatches) { | ||||
returnObj.year = parseInt(yearMatches[2], 10); | returnObj.year = parseInt(yearMatches[2], 10); | ||||
Show All 17 Lines | function infoFromURL(url: string): URLInfo { | ||||
} | } | ||||
if (verifyMatches) { | if (verifyMatches) { | ||||
returnObj.verify = verifyMatches[2]; | returnObj.verify = verifyMatches[2]; | ||||
} | } | ||||
if (calendarTest) { | if (calendarTest) { | ||||
returnObj.calendar = true; | returnObj.calendar = true; | ||||
} else if (chatTest) { | } else if (chatTest) { | ||||
returnObj.chat = true; | returnObj.chat = true; | ||||
} else if (appsTest) { | |||||
returnObj.apps = true; | |||||
} else if (accountSettingsTest) { | } else if (accountSettingsTest) { | ||||
returnObj.settings = 'account'; | returnObj.settings = 'account'; | ||||
} else if (dangerZoneTest) { | } else if (dangerZoneTest) { | ||||
returnObj.settings = 'danger-zone'; | returnObj.settings = 'danger-zone'; | ||||
} | } | ||||
return returnObj; | return returnObj; | ||||
} | } | ||||
const setURLPrefix = 'SET_URL_PREFIX'; | const setURLPrefix = 'SET_URL_PREFIX'; | ||||
export { infoFromURL, setURLPrefix }; | export { infoFromURL, setURLPrefix }; |