Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. (function(global, factroy) {
  2. if (typeof define === 'function' && define.amd) {
  3. define([], factroy);
  4. } else if (typeof exports === 'object') {
  5. module.exports = factroy();
  6. } else {
  7. global.Qshandler = factroy();
  8. }
  9. })(this, function() {
  10. function queryStringSpliter() {
  11. const key = '[^=]*';
  12. const val = '[^&]*';
  13. const keyVal = '(?:' + key + ')=(?:' + val + ')';
  14.  
  15. return new RegExp('(?:\\?|&)(' + keyVal + ')');
  16. }
  17.  
  18. const splitter = queryStringSpliter();
  19.  
  20. function arrayify(url) {
  21. return url.split(splitter).filter(Boolean);
  22. }
  23.  
  24. function objectify(url) {
  25. const arr = arrayify(url);
  26. const obj = {};
  27. for (let i = arr.length; i--;) {
  28. const str = arr[i];
  29. const keyVal = str.split('=');
  30. const key = keyVal[0];
  31. const val = keyVal[1];
  32. if (key) {
  33. obj[key] = val;
  34. }
  35. }
  36. return obj;
  37. }
  38.  
  39. class Qshandler {
  40. constructor(input = '') {
  41. if (typeof input !== 'string') {
  42. throw new Error('argument is required to be typeof string but got' + typeof input);
  43. }
  44. this.input = input;
  45. }
  46. toArray() {
  47. return arrayify(this.input);
  48. }
  49. toJSON() {
  50. return objectify(this.input);
  51. }
  52. }
  53.  
  54. return Qshandler;
  55. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement