colouring-montreal/app/src/parse.ts

52 lines
1.0 KiB
TypeScript
Raw Normal View History

2018-09-13 15:36:14 -04:00
/**
* Utility functions for parsing
2019-04-27 08:24:50 -04:00
*
2018-09-13 15:36:14 -04:00
*/
/**
* Parse a string as positive integer or NaN
*
* @param {string} value
2019-04-27 08:24:50 -04:00
* @returns {number} integer or NaN
2018-09-13 15:36:14 -04:00
*/
function strictParseInt(value) {
2019-05-27 11:20:00 -04:00
if (/^([1-9][0-9]*)$/.test(value)) {
2018-09-13 15:36:14 -04:00
return Number(value);
2019-05-27 11:20:00 -04:00
}
2018-09-13 15:36:14 -04:00
return NaN;
}
2019-04-27 08:24:50 -04:00
/**
* Parse building ID from URL
*
* @param {String} url
* @returns {number|undefined}
*/
2019-02-24 14:28:11 -05:00
function parseBuildingURL(url) {
2019-10-24 07:20:48 -04:00
const re = /\/(\d+)(\/history)?$/;
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;
}
2019-04-27 08:24:50 -04:00
/**
* Parse category slug from URL
*
* @param {String} url
* @returns {String} [age]
*/
2018-11-30 04:26:28 -05:00
function parseCategoryURL(url) {
2019-05-27 13:26:29 -04:00
const defaultCat = 'age';
2019-05-27 11:31:48 -04:00
if (url === '/') {
2019-05-27 13:26:29 -04:00
return defaultCat;
2018-11-30 04:26:28 -05:00
}
2019-05-10 11:06:33 -04:00
const matches = /^\/(view|edit|multi-edit)\/([^/.]+)/.exec(url);
2019-05-27 13:26:29 -04:00
const cat = (matches && matches.length >= 3) ? matches[2] : defaultCat;
2018-11-30 04:26:28 -05:00
return cat;
}
export { strictParseInt, parseBuildingURL, parseCategoryURL };