Skip to content

Instantly share code, notes, and snippets.

@L00Cyph3r
Last active October 5, 2018 08:21
Show Gist options
  • Select an option

  • Save L00Cyph3r/e794c01c2aafa7680a31e7fc4d414a49 to your computer and use it in GitHub Desktop.

Select an option

Save L00Cyph3r/e794c01c2aafa7680a31e7fc4d414a49 to your computer and use it in GitHub Desktop.

Revisions

  1. L00Cyph3r revised this gist Oct 5, 2018. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions rangeToString.js
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,9 @@ function rangeToString(items) {
    var range = false;
    var string = '';
    for (var i = 0; i < items.length; i++) {
    if (point === items[i]) {
    continue;
    }
    if (point === null) {
    string = string + items[i];
    } else if ((point + 1) === items[i]) {
  2. L00Cyph3r created this gist Oct 5, 2018.
    36 changes: 36 additions & 0 deletions rangeToString.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    /**
    * Originally from: https://stackoverflow.com/a/24104156
    * Rewritten in JavaScript
    */
    var pagenumbers = [
    10,12,13,45,46,47,48,49,50,3,4,5
    ];

    function rangeToString(items) {
    items.sort(function (a, b) {
    return a - b;
    });
    var point = null;
    var range = false;
    var string = '';
    for (var i = 0; i < items.length; i++) {
    if (point === null) {
    string = string + items[i];
    } else if ((point + 1) === items[i]) {
    range = true;
    } else {
    if (range === true) {
    string = string + '-' + point;
    }
    string = string + ',' + items[i];
    }
    point = items[i];
    }
    if (range === true) {
    string = string + '-' + point;
    }

    return string;
    }

    var string = rangeToString(pagenumbers);