Javascript: (Page) ranges from string

Date: 2016-09-01
var pdfPages = '1,3-5,8,9-15,4,2,1';

var getPages = function(pageRangeStr) {
	var pages = [];
	var rangeStr = pageRangeStr.replace(/\s/g, ''); // remove all whitespace
	rangeStr.split(',').forEach(function(r) {
		var s = r.split('-');
		var f, t;		
		if (s.length == 1) {
			f = parseInt(s[0]);
			pages.push(f);
		} else if (s.length == 2) {
			f = parseInt(s[0]);
			t = parseInt(s[1]);
			var x = f;
			while(x <= t) {
				pages.push(x);
				x += 1;
			}
		}
	});
	return pages;
};

getPages(pdfPages);
// results in [1, 3, 4, 5, 8, 9, 10, 4, 2, 1]
4630cookie-checkJavascript: (Page) ranges from string