4421930942
Most errors highglighted by TS were due to a lack of type definitions and were ignored by settings types to `any`. Some minor bugs were resolved where the fix was obvious. TODO marks left where `any` needs to be later removed or bugfix verified
52 lines
1.0 KiB
TypeScript
52 lines
1.0 KiB
TypeScript
/**
|
|
* Utility functions for parsing
|
|
*
|
|
*/
|
|
|
|
/**
|
|
* Parse a string as positive integer or NaN
|
|
*
|
|
* @param {string} value
|
|
* @returns {number} integer or NaN
|
|
*/
|
|
function strictParseInt(value) {
|
|
if (/^([1-9][0-9]*)$/.test(value)) {
|
|
return Number(value);
|
|
}
|
|
return NaN;
|
|
}
|
|
|
|
/**
|
|
* Parse building ID from URL
|
|
*
|
|
* @param {String} url
|
|
* @returns {number|undefined}
|
|
*/
|
|
function parseBuildingURL(url) {
|
|
const re = /\/building\/([^/]+).html/;
|
|
const matches = re.exec(url);
|
|
|
|
if (matches && matches.length >= 2) {
|
|
return strictParseInt(matches[1])
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
/**
|
|
* Parse category slug from URL
|
|
*
|
|
* @param {String} url
|
|
* @returns {String} [age]
|
|
*/
|
|
function parseCategoryURL(url) {
|
|
const defaultCat = 'age';
|
|
if (url === '/') {
|
|
return defaultCat;
|
|
}
|
|
const matches = /^\/(view|edit|multi-edit)\/([^/.]+)/.exec(url);
|
|
const cat = (matches && matches.length >= 3) ? matches[2] : defaultCat;
|
|
return cat;
|
|
}
|
|
|
|
export { strictParseInt, parseBuildingURL, parseCategoryURL };
|