{"id":463,"date":"2016-09-01T10:23:27","date_gmt":"2016-09-01T09:23:27","guid":{"rendered":"https:\/\/solidt.eu\/blog\/?p=463"},"modified":"2022-07-25T08:35:45","modified_gmt":"2022-07-25T07:35:45","slug":"javascript-page-ranges-from-string","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/javascript-page-ranges-from-string\/","title":{"rendered":"Javascript: (Page) ranges from string"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var pdfPages = '1,3-5,8,9-15,4,2,1';\n\nvar getPages = function(pageRangeStr) {\n\tvar pages = [];\n\tvar rangeStr = pageRangeStr.replace(\/\\s\/g, ''); \/\/ remove all whitespace\n\trangeStr.split(',').forEach(function(r) {\n\t\tvar s = r.split('-');\n\t\tvar f, t;\t\t\n\t\tif (s.length == 1) {\n\t\t\tf = parseInt(s[0]);\n\t\t\tpages.push(f);\n\t\t} else if (s.length == 2) {\n\t\t\tf = parseInt(s[0]);\n\t\t\tt = parseInt(s[1]);\n\t\t\tvar x = f;\n\t\t\twhile(x &lt;= t) {\n\t\t\t\tpages.push(x);\n\t\t\t\tx += 1;\n\t\t\t}\n\t\t}\n\t});\n\treturn pages;\n};\n\ngetPages(pdfPages);\n\/\/ results in [1, 3, 4, 5, 8, 9, 10, 4, 2, 1]\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5,4],"tags":[],"class_list":["post-463","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/463","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=463"}],"version-history":[{"count":2,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/463\/revisions"}],"predecessor-version":[{"id":1912,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/463\/revisions\/1912"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}