-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery-handler.min.js
1 lines (1 loc) · 2.27 KB
/
query-handler.min.js
1
!function(root,factory){"function"==typeof define&&define.amd?define(["exports"],factory):factory("undefined"!=typeof exports?exports:root)}(this,function(exports){"use strict";function QueryHandler(q){return new Query(q)}var _createClass=function(){function defineProperties(target,props){for(var key in props){var prop=props[key];prop.configurable=!0,prop.value&&(prop.writable=!0)}Object.defineProperties(target,props)}return function(Constructor,protoProps,staticProps){return protoProps&&defineProperties(Constructor.prototype,protoProps),staticProps&&defineProperties(Constructor,staticProps),Constructor}}(),_classCallCheck=function(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")};exports.QueryHandler=QueryHandler,Object.defineProperty(exports,"__esModule",{value:!0});var Query=function(){function Query(string){_classCallCheck(this,Query),this.initialString=string,this.generateQueryObject(string)}return _createClass(Query,{generateQueryObject:{value:function(str){this.query={};for(var arr=str.substr(1).split("&"),i=0,il=arr.length;il>i;++i){var current=arr[i].split("="),key=decodeURIComponent(current[0]),value=this.smartParse(current[1]);"undefined"==typeof this.query[key]?this.query[key]=value:this.query[key]instanceof Array?this.query[key].push(value):this.query[key]=[this.query[key],value]}}},smartParse:{value:function(val){return"undefined"==typeof val?null:/^(true|false|null)$/i.test(val)?JSON.parse(val):isNaN(+val)?decodeURIComponent(val):+val}},getQueryString:{value:function(){var self=this,q=this.query,result=Object.keys(q).map(function(key,_){var val=q[key];return val instanceof Array?val.map(function(v,i){return self.pairToString(key,v)}).join("&"):self.pairToString(key,val)});return"?"+result.join("&")}},string:{get:function(){return this.getQueryString()}},initial:{get:function(){return this.initialString}},has:{value:function(key){return this.query.hasOwnProperty(key)}},get:{value:function(key,def){return this.has(key)?this.query[key]:"undefined"==typeof def?null:def}},set:{value:function(key,val){this.query[key]=val}},remove:{value:function(key){delete this.query[key]}},pairToString:{value:function(key,val){return key=encodeURIComponent(key),null===val?key:key+"="+encodeURIComponent(val)}}}),Query}()});