Add to jquery unserialise function
First, my English is poor
We can get the corresponding URL via jquery params method,like that:
- 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 ] }'
- $.params(params)
- /*get params
- 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
- */
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.
- 'use strict'
- 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'
- unserialize(uri);
- function unserialize(uri) {
- let params = {};
- let parts = uri.split('&');
- for (let i = 0; i < parts.length; i++) {
- let kv = parts[i].split('=');
- var keys = keyFmt(kv[0]);
- var value = kv[1];
- /*Initialization parameter 'start'*/
- if (typeof params[keys.start] == 'undefined') {
- params[keys.start] = {};
- }
- /*Initialization parameter 'middle'*/
- var ref = params;
- var index = keys.start;
- if (keys.middle.length > 0) {
- keys.middle.forEach(function (key) {
- let cur = ref[index];
- if (typeof cur[key] == 'undefined') {
- cur[key] = {};
- }
- index = key;
- ref = cur;
- })
- }
- /*Initialization parameter 'end',and assignment*/
- if (keys.end != null) {
- if (Object.prototype.toString.call(ref[index]) != "[object Array]") {
- var arr = [];
- for (let key in ref[index]) {
- arr[key] = ref[index][key];
- }
- ref[index] = arr;
- ref[index].push(value);
- } else {
- ref[index].push(value)
- }
- } else {
- ref[index] = value;
- }
- }
- return arrayFmt(params);
- }
- /*
- * format left key of '='
- *
- * like: d1[1][b3][a4][]
- *
- * Formatted:
- * -> {
- * start:d1,
- * middle:[1,b3,a4],
- * end:'array'
- * }
- */
- function keyFmt(keyStr) {
- let key = {start: '', middle: [], end: null};
- var s = /^(\w+)/.exec(keyStr);
- key.start = s[1];
- var mPattern = /\[(\w+)\]/g;
- var m = mPattern.exec(keyStr);
- var i = 0;
- while (m != null && i < 10) {
- key.middle.push(m[1]);
- m = mPattern.exec(keyStr);
- i++;
- }
- var e = /\[\]$/.exec(keyStr);
- if (e != null) {
- key.end = 'array';
- }
- return key;
- }
- /*
- * If the object is the array , converts
- */
- function arrayFmt(params) {
- var keys = [];
- if (params instanceof Object && Object.prototype.toString.call(params) != "[object Array]") {
- for (var key in params) {
- params[key] = arrayFmt(params[key]);
- keys.push(Number(key));
- }
- } else {
- return params;
- }
- if (keys.every((item, index) => item == index)){
- var arr = [];
- for (let key in params) {
- arr.push(params[key]);
- }
- return arr;
- }else{
- return params;
- }
- }