Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function(global, factroy) {
- if (typeof define === 'function' && define.amd) {
- define([], factroy);
- } else if (typeof exports === 'object') {
- module.exports = factroy();
- } else {
- global.Qshandler = factroy();
- }
- })(this, function() {
- function queryStringSpliter() {
- const key = '[^=]*';
- const val = '[^&]*';
- const keyVal = '(?:' + key + ')=(?:' + val + ')';
- return new RegExp('(?:\\?|&)(' + keyVal + ')');
- }
- const splitter = queryStringSpliter();
- function arrayify(url) {
- return url.split(splitter).filter(Boolean);
- }
- function objectify(url) {
- const arr = arrayify(url);
- const obj = {};
- for (let i = arr.length; i--;) {
- const str = arr[i];
- const keyVal = str.split('=');
- const key = keyVal[0];
- const val = keyVal[1];
- if (key) {
- obj[key] = val;
- }
- }
- return obj;
- }
- class Qshandler {
- constructor(input = '') {
- if (typeof input !== 'string') {
- throw new Error('argument is required to be typeof string but got' + typeof input);
- }
- this.input = input;
- }
- toArray() {
- return arrayify(this.input);
- }
- toJSON() {
- return objectify(this.input);
- }
- }
- return Qshandler;
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement