colouring-montreal/app/src/parse.js

51 lines
1.0 KiB
JavaScript
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) {
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;
}
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) {
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;
}
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) {
const default_cat = 'age';
if (url === "/") {
return default_cat
}
const matches = /^\/(view|edit)\/([^/.]+)/.exec(url);
2019-02-24 14:28:11 -05:00
const cat = (matches && matches.length >= 3) ? matches[2] : default_cat;
2018-11-30 04:26:28 -05:00
return cat;
}
export { strictParseInt, parseBuildingURL, parseCategoryURL };