Add to jquery unserialise function

Add to jquery unserialise function


First, my English is poor
We can get the corresponding URL via jquery params method,like that:
  1. var params= '{ "a": [ 0, [ 1, 2 ], [ 3, [ 4, 5 ], [ 6 ] ], { "b": [ 7, [ 8, 9 ], [ { "c": 10, "d": 11 } ], [ [ 12 ] ], [ [ [ 13 ] ] ], { "e": { "f": { "g": [ 14, [ 15 ] ] } } }, 16 ] }, 17 ] }'
  2. $.params(params)
  3. /*get params 
  4. a[]=0&a[1][]=1&a[1][]=2&a[2][]=3&a[2][1][]=4&a[2][1][]=5&a[2][2][]=6&a[3][b][]=7&a[3][b][1][]=8&a[3][b][1][]=9&a[3][b][2][0][c]=10&a[3][b][2][0][d]=11&a[3][b][3][0][]=12&a[3][b][4][0][0][]=13&a[3][b][5][e][f][g][]=14&a[3][b][5][e][f][g][1][]=15&a[3][b][]=16&a[]=17
  5. */
But sometimes we need to turn,For example, from one page to another page , and we need to bring a lot of arguments,No back-end support. So deserialization need to do our own.

I have developed a unserialise function,and i want to add to jquery.
  1. 'use strict'

  2. let uri = 'a1=123&b1[a2]=123&c1=dsss&d1[0][a3]=123&d1[0][b3][a4]=123&d1[1][a3]=123&d1[2][]=1&d1[2][]=2&d1[2][]=1&d1[2][]=3&d1[2][]=4'

  3. unserialize(uri);

  4. function unserialize(uri) {
  5.     let params = {};
  6.     let parts = uri.split('&');

  7.     for (let i = 0; i < parts.length; i++) {

  8.         let kv = parts[i].split('=');
  9.         var keys = keyFmt(kv[0]);
  10.         var value = kv[1];

  11.         /*Initialization parameter 'start'*/
  12.         if (typeof params[keys.start] == 'undefined') {
  13.             params[keys.start] = {};
  14.         }

  15.         /*Initialization parameter 'middle'*/
  16.         var ref = params;
  17.         var index = keys.start;

  18.         if (keys.middle.length > 0) {
  19.             keys.middle.forEach(function (key) {
  20.                 let cur = ref[index];
  21.                 if (typeof cur[key] == 'undefined') {
  22.                     cur[key] = {};
  23.                 }
  24.                 index = key;
  25.                 ref = cur;
  26.             })
  27.         }

  28.         /*Initialization parameter 'end',and assignment*/
  29.         if (keys.end != null) {
  30.             if (Object.prototype.toString.call(ref[index]) != "[object Array]") {
  31.                 var arr = [];

  32.                 for (let key in ref[index]) {
  33.                     arr[key] = ref[index][key];
  34.                 }
  35.                 ref[index] = arr;
  36.                 ref[index].push(value);

  37.             } else {
  38.                 ref[index].push(value)
  39.             }
  40.         } else {
  41.             ref[index] = value;
  42.         }
  43.     }
  44.     return arrayFmt(params);
  45. }

  46. /*
  47.  * format left key of '='
  48.  *
  49.  * like: d1[1][b3][a4][]
  50.  *
  51.  * Formatted:
  52.  * -> {
  53.  * start:d1,
  54.  * middle:[1,b3,a4],
  55.  * end:'array'
  56.  * }
  57.  */
  58. function keyFmt(keyStr) {
  59.     let key = {start: '', middle: [], end: null};

  60.     var s = /^(\w+)/.exec(keyStr);
  61.     key.start = s[1];

  62.     var mPattern = /\[(\w+)\]/g;
  63.     var m = mPattern.exec(keyStr);

  64.     var i = 0;
  65.     while (m != null && i < 10) {
  66.         key.middle.push(m[1]);
  67.         m = mPattern.exec(keyStr);
  68.         i++;
  69.     }

  70.     var e = /\[\]$/.exec(keyStr);
  71.     if (e != null) {
  72.         key.end = 'array';
  73.     }
  74.     return key;
  75. }

  76. /*
  77.  * If the object is the array , converts
  78.  */
  79. function arrayFmt(params) {
  80.     var keys = [];
  81.     if (params instanceof Object && Object.prototype.toString.call(params) != "[object Array]") {
  82.         for (var key in params) {
  83.             params[key] = arrayFmt(params[key]);
  84.             keys.push(Number(key));
  85.         }
  86.     } else {
  87.         return params;
  88.     }

  89.     if (keys.every((item, index) => item == index)){
  90.         var arr = [];
  91.         for (let key in params) {
  92.             arr.push(params[key]);
  93.         }
  94.         return arr;
  95.     }else{
  96.         return params;
  97.     }
  98. }