2017-04-28 22:02:46 -07:00
|
|
|
/**
|
2017-05-01 11:33:40 -07:00
|
|
|
* Matches a url string with a `urn` query. urn query with letters or underscore segment of any length greater
|
2017-05-01 22:58:16 -07:00
|
|
|
* than 1 followed by colon and 3 forward slashes and a segment containing letters, {, }, _ or /, or none
|
2017-04-28 22:02:46 -07:00
|
|
|
* The value following the urn key is retained
|
|
|
|
* @type {RegExp}
|
|
|
|
*/
|
2017-09-20 14:25:27 -07:00
|
|
|
const urnRegex = /([a-z_]+):\/{3}([a-z0-9_\-/{}]*)/i;
|
2017-05-15 12:45:28 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Matches urn's that occur in flow urls
|
|
|
|
* @type {RegExp}
|
|
|
|
*/
|
2017-09-20 14:25:27 -07:00
|
|
|
const specialFlowUrnRegex = /(?:\?urn=)([a-z0-9_\-/{}\s]+)/i;
|
|
|
|
|
2017-04-28 13:44:31 -07:00
|
|
|
/**
|
|
|
|
* Asserts that a provided string matches the urn pattern above
|
2017-10-19 18:17:16 -07:00
|
|
|
* @param {string} candidateUrn the string to test on
|
2017-04-28 13:44:31 -07:00
|
|
|
*/
|
2017-10-19 18:17:16 -07:00
|
|
|
const isUrn = (candidateUrn: string) => urnRegex.test(String(candidateUrn));
|
2017-04-28 13:44:31 -07:00
|
|
|
|
2017-10-19 18:17:16 -07:00
|
|
|
/**
|
|
|
|
* Extracts the platform string from the candidate urn string
|
|
|
|
* @param {string} candidateUrn the urn string with leading platform identifier
|
|
|
|
* @returns {string | void}
|
|
|
|
*/
|
|
|
|
const getPlatformFromUrn = (candidateUrn: string) => {
|
|
|
|
const matches = urnRegex.exec(candidateUrn);
|
|
|
|
if (matches) {
|
|
|
|
const [, platform] = matches;
|
|
|
|
return platform.toUpperCase();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default isUrn;
|
|
|
|
|
|
|
|
export { urnRegex, specialFlowUrnRegex, getPlatformFromUrn };
|