Skip to content

Instantly share code, notes, and snippets.

@sujeetkv
Last active February 19, 2019 07:29
Show Gist options
  • Save sujeetkv/290dd2f417fe08d19e7dd634f972d22b to your computer and use it in GitHub Desktop.
Save sujeetkv/290dd2f417fe08d19e7dd634f972d22b to your computer and use it in GitHub Desktop.

Revisions

  1. sujeetkv revised this gist Feb 19, 2019. 1 changed file with 34 additions and 5 deletions.
    39 changes: 34 additions & 5 deletions url-state.js
    Original file line number Diff line number Diff line change
    @@ -2,11 +2,12 @@
    * URL State manipulation utility
    *
    * Usage Example:
    * //urlState.strictMode = (function() { return !this; })();
    * var queryString = urlState.search();
    * queryString.name = 'sujeet';
    * urlState.pathname('/result-page').search(queryString).update();
    */
    (function (win) {
    (function (win) {
    'use strict';

    var toType = function (val) {
    @@ -92,10 +93,36 @@
    init_loc.href = win.location.href;
    wloc.href = win.location.href;
    return {
    protocol: wloc.protocol,
    host: wloc.host,
    hostname: wloc.hostname,
    port: wloc.port,
    strictMode: true,

    get protocol() { return wloc.protocol; },
    set protocol(value) {
    if (this.strictMode) {
    throw new TypeError('"protocol" is read-only');
    }
    },

    get host() { return wloc.host; },
    set host(value) {
    if (this.strictMode) {
    throw new TypeError('"host" is read-only');
    }
    },

    get hostname() { return wloc.hostname; },
    set hostname(value) {
    if (this.strictMode) {
    throw new TypeError('"hostname" is read-only');
    }
    },

    get port() { return wloc.port; },
    set port(value) {
    if (this.strictMode) {
    throw new TypeError('"port" is read-only');
    }
    },

    pathname: function (pathname) {
    if (pathname === undefined) {
    return wloc.pathname;
    @@ -120,6 +147,7 @@
    return this;
    }
    },

    update: function (title, replace) {
    win.history[replace?'replaceState':'pushState'](init_state, '', this.toString());
    win.document.title = title || init_title;
    @@ -131,6 +159,7 @@
    win.document.title = init_title;
    return this;
    },

    toString: function () {
    return wloc.toString();
    },
  2. sujeetkv revised this gist Feb 12, 2019. 1 changed file with 55 additions and 55 deletions.
    110 changes: 55 additions & 55 deletions url-state.js
    Original file line number Diff line number Diff line change
    @@ -14,60 +14,7 @@
    return type.replace(/\[(\w+)\s(\w+)\]/, '$2').toLowerCase();
    };

    win.urlState = (function () {
    var init_state = win.history.state,
    init_title = win.document.title,
    init_loc = win.document.createElement('a'),
    wloc = win.document.createElement('a');
    init_loc.href = win.location.href;
    wloc.href = win.location.href;
    return {
    protocol: wloc.protocol,
    host: wloc.host,
    hostname: wloc.hostname,
    port: wloc.port,
    pathname: function (pathname) {
    if (pathname === undefined) {
    return wloc.pathname;
    } else {
    wloc.pathname = pathname;
    return this;
    }
    },
    search: function (search) {
    if (search === undefined) {
    return win.parseQstr(wloc.search);
    } else {
    wloc.search = win.createQstr(search);
    return this;
    }
    },
    hash: function (hash) {
    if (hash === undefined) {
    return wloc.hash;
    } else {
    wloc.hash = hash;
    return this;
    }
    },
    update: function (title, replace) {
    win.history[replace?'replaceState':'pushState'](init_state, '', this.toString());
    win.document.title = title || init_title;
    return this;
    },
    reset: function () {
    wloc = init_loc;
    win.history.replaceState(init_state, '', this.toString());
    win.document.title = init_title;
    return this;
    },
    toString: function () {
    return wloc.toString();
    },
    };
    })();

    win.parseQstr = function (queryString, coerce) {
    var parseQstr = function (queryString, coerce) {
    queryString = ('' + queryString).replace(/^\?/, '');
    var re = /([^&=]+)=?([^&]*)/g,
    m,
    @@ -110,7 +57,7 @@
    return params;
    };

    win.createQstr = function (obj) {
    var createQstr = function (obj) {
    var key, s = [];
    var add = function (k, v) {
    v = typeof v === 'function' ? v() : v;
    @@ -136,4 +83,57 @@
    }
    return s.join('&');
    };

    win.urlState = (function () {
    var init_state = win.history.state,
    init_title = win.document.title,
    init_loc = win.document.createElement('a'),
    wloc = win.document.createElement('a');
    init_loc.href = win.location.href;
    wloc.href = win.location.href;
    return {
    protocol: wloc.protocol,
    host: wloc.host,
    hostname: wloc.hostname,
    port: wloc.port,
    pathname: function (pathname) {
    if (pathname === undefined) {
    return wloc.pathname;
    } else {
    wloc.pathname = pathname;
    return this;
    }
    },
    search: function (search) {
    if (search === undefined) {
    return parseQstr(wloc.search);
    } else {
    wloc.search = createQstr(search);
    return this;
    }
    },
    hash: function (hash) {
    if (hash === undefined) {
    return wloc.hash;
    } else {
    wloc.hash = hash;
    return this;
    }
    },
    update: function (title, replace) {
    win.history[replace?'replaceState':'pushState'](init_state, '', this.toString());
    win.document.title = title || init_title;
    return this;
    },
    reset: function () {
    wloc = init_loc;
    win.history.replaceState(init_state, '', this.toString());
    win.document.title = init_title;
    return this;
    },
    toString: function () {
    return wloc.toString();
    },
    };
    })();
    })(this);
  3. sujeetkv revised this gist Jan 28, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion url-state.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    * Usage Example:
    * var queryString = urlState.search();
    * queryString.name = 'sujeet';
    * urlState.search(queryString).update();
    * urlState.pathname('/result-page').search(queryString).update();
    */
    (function (win) {
    'use strict';
  4. sujeetkv revised this gist Jan 28, 2019. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion url-state.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,9 @@
    * URL State manipulation utility
    *
    * Usage Example:
    * urlState.pathname('/hello-demo').search({q: 'sujeet'}).update();
    * var queryString = urlState.search();
    * queryString.name = 'sujeet';
    * urlState.search(queryString).update();
    */
    (function (win) {
    'use strict';
  5. sujeetkv revised this gist Jan 28, 2019. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion url-state.js
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,8 @@
    * urlState.pathname('/hello-demo').search({q: 'sujeet'}).update();
    */
    (function (win) {
    'use strict';

    var toType = function (val) {
    var type = Object.prototype.toString.call(val);
    return type.replace(/\[(\w+)\s(\w+)\]/, '$2').toLowerCase();
    @@ -107,7 +109,7 @@
    };

    win.createQstr = function (obj) {
    var s = [];
    var key, s = [];
    var add = function (k, v) {
    v = typeof v === 'function' ? v() : v;
    v = (v === null || v === undefined) ? '' : v === undefined ? '' : v;
  6. sujeetkv created this gist Jan 28, 2019.
    135 changes: 135 additions & 0 deletions url-state.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,135 @@
    /**
    * URL State manipulation utility
    *
    * Usage Example:
    * urlState.pathname('/hello-demo').search({q: 'sujeet'}).update();
    */
    (function (win) {
    var toType = function (val) {
    var type = Object.prototype.toString.call(val);
    return type.replace(/\[(\w+)\s(\w+)\]/, '$2').toLowerCase();
    };

    win.urlState = (function () {
    var init_state = win.history.state,
    init_title = win.document.title,
    init_loc = win.document.createElement('a'),
    wloc = win.document.createElement('a');
    init_loc.href = win.location.href;
    wloc.href = win.location.href;
    return {
    protocol: wloc.protocol,
    host: wloc.host,
    hostname: wloc.hostname,
    port: wloc.port,
    pathname: function (pathname) {
    if (pathname === undefined) {
    return wloc.pathname;
    } else {
    wloc.pathname = pathname;
    return this;
    }
    },
    search: function (search) {
    if (search === undefined) {
    return win.parseQstr(wloc.search);
    } else {
    wloc.search = win.createQstr(search);
    return this;
    }
    },
    hash: function (hash) {
    if (hash === undefined) {
    return wloc.hash;
    } else {
    wloc.hash = hash;
    return this;
    }
    },
    update: function (title, replace) {
    win.history[replace?'replaceState':'pushState'](init_state, '', this.toString());
    win.document.title = title || init_title;
    return this;
    },
    reset: function () {
    wloc = init_loc;
    win.history.replaceState(init_state, '', this.toString());
    win.document.title = init_title;
    return this;
    },
    toString: function () {
    return wloc.toString();
    },
    };
    })();

    win.parseQstr = function (queryString, coerce) {
    queryString = ('' + queryString).replace(/^\?/, '');
    var re = /([^&=]+)=?([^&]*)/g,
    m,
    params = {},
    coerceTypes = {'true': !0, 'false': !1, 'null': null},
    decode = function (str) {
    return decodeURIComponent(str.replace(/\+/g, '%20'));
    };
    if (queryString) {
    while (m = re.exec(queryString)) {
    var k = decode(m[1]),
    v = decode(m[2]),
    curr = params;
    if (coerce) {
    v = !isNaN(v) ? +v : (v === 'undefined' ? undefined : (coerceTypes[v] ? coerceTypes[v] : v));
    }
    var keys = k.split('][');
    var keysLast = keys.length - 1;
    if (/\[/.test(keys[0]) && /\]$/.test(keys[keysLast])) {
    keys[keysLast] = keys[keysLast].replace(/\]$/, '');
    keys = keys.shift().split('[').concat(keys);
    keysLast = keys.length - 1;
    }
    if (keysLast) {
    for (var i = 0; i <= keysLast; i++) {
    k = (keys[i] === '') ? curr.length : keys[i];
    curr = curr[k] = (i < keysLast) ? curr[k] || (keys[i+1] && isNaN(keys[i+1]) ? {} : []) : v;
    }
    } else if (params[k]) {
    if (toType(params[k]) === 'array') {
    params[k].push(v);
    } else {
    params[k] = [params[k], v];
    }
    } else {
    params[k] = v;
    }
    }
    }
    return params;
    };

    win.createQstr = function (obj) {
    var s = [];
    var add = function (k, v) {
    v = typeof v === 'function' ? v() : v;
    v = (v === null || v === undefined) ? '' : v === undefined ? '' : v;
    s[s.length] = encodeURIComponent(k) + '=' + encodeURIComponent(v);
    };
    var buildParams = function (prefix, obj) {
    var i, len, key, objectType = toType(obj);
    if (objectType === 'array') {
    for (i = 0, len = obj.length; i < len; i++) {
    buildParams(prefix + '[' + (typeof obj[i] === 'object' && obj[i] ? i : '') + ']', obj[i]);
    }
    } else if (objectType === 'object') {
    for (key in obj) {
    buildParams(prefix + '[' + key + ']', obj[key]);
    }
    } else {
    add(prefix, obj);
    }
    };
    for (key in obj) {
    buildParams(key, obj[key]);
    }
    return s.join('&');
    };
    })(this);