2018-09-13 15:36:14 -04:00
|
|
|
/**
|
|
|
|
* Utility functions for parsing
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a string as positive integer or NaN
|
|
|
|
*
|
|
|
|
* @param {string} value
|
|
|
|
*/
|
|
|
|
function strictParseInt(value) {
|
2018-09-30 16:53:20 -04:00
|
|
|
if (/^([1-9][0-9]*)$/.test(value))
|
2018-09-13 15:36:14 -04:00
|
|
|
return Number(value);
|
|
|
|
return NaN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function parseBuildingURL(url){
|
2018-11-30 04:26:28 -05:00
|
|
|
const re = /\/building\/([^/]+).html/;
|
2018-09-13 15:36:14 -04:00
|
|
|
const matches = re.exec(url);
|
2018-09-13 18:55:53 -04:00
|
|
|
|
|
|
|
if (matches && matches.length >= 2) {
|
2018-09-13 15:41:42 -04:00
|
|
|
return strictParseInt(matches[1])
|
2018-09-13 15:36:14 -04:00
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2018-11-30 04:26:28 -05:00
|
|
|
function parseCategoryURL(url) {
|
|
|
|
const default_cat = 'age';
|
|
|
|
if (url === "/") {
|
|
|
|
return default_cat
|
|
|
|
}
|
|
|
|
const matches = /^\/(view|edit)\/([^/.]+)/.exec(url);
|
|
|
|
const cat = (matches && matches.length >= 3)? matches[2] : default_cat;
|
|
|
|
return cat;
|
|
|
|
}
|
|
|
|
|
|
|
|
export { strictParseInt, parseBuildingURL, parseCategoryURL };
|