|
| 1 | +// jQuery Deparam - v0.1.0 - 6/14/2011 |
| 2 | +// http://benalman.com/ |
| 3 | +// Copyright (c) 2011 Ben Alman; Licensed MIT, GPL |
| 4 | + |
| 5 | +(function($) { |
| 6 | + // Creating an internal undef value is safer than using undefined, in case it |
| 7 | + // was ever overwritten. |
| 8 | + var undef; |
| 9 | + // A handy reference. |
| 10 | + var decode = decodeURIComponent; |
| 11 | + |
| 12 | + // Document $.deparam. |
| 13 | + var deparam = $.deparam = function(text, reviver) { |
| 14 | + // The object to be returned. |
| 15 | + var result = {}; |
| 16 | + // Iterate over all key=value pairs. |
| 17 | + $.each(text.replace(/\+/g, ' ').split('&'), function(index, pair) { |
| 18 | + // The key=value pair. |
| 19 | + var kv = pair.split('='); |
| 20 | + // The key, URI-decoded. |
| 21 | + var key = decode(kv[0]); |
| 22 | + // Abort if there's no key. |
| 23 | + if ( !key ) { return; } |
| 24 | + // The value, URI-decoded. If value is missing, use empty string. |
| 25 | + var value = decode(kv[1] || ''); |
| 26 | + // If key is more complex than 'foo', like 'a[]' or 'a[b][c]', split it |
| 27 | + // into its component parts. |
| 28 | + var keys = key.split(']['); |
| 29 | + var last = keys.length - 1; |
| 30 | + // Used when key is complex. |
| 31 | + var i = 0; |
| 32 | + var current = result; |
| 33 | + |
| 34 | + // If the first keys part contains [ and the last ends with ], then [] |
| 35 | + // are correctly balanced. |
| 36 | + if ( keys[0].indexOf('[') >= 0 && /\]$/.test(keys[last]) ) { |
| 37 | + // Remove the trailing ] from the last keys part. |
| 38 | + keys[last] = keys[last].replace(/\]$/, ''); |
| 39 | + // Split first keys part into two parts on the [ and add them back onto |
| 40 | + // the beginning of the keys array. |
| 41 | + keys = keys.shift().split('[').concat(keys); |
| 42 | + // Since a key part was added, increment last. |
| 43 | + last++; |
| 44 | + } else { |
| 45 | + // Basic 'foo' style key. |
| 46 | + last = 0; |
| 47 | + } |
| 48 | + |
| 49 | + if ( $.isFunction(reviver) ) { |
| 50 | + // If a reviver function was passed, use that function. |
| 51 | + value = reviver(key, value); |
| 52 | + } else if ( reviver ) { |
| 53 | + // If true was passed, use the built-in $.deparam.reviver function. |
| 54 | + value = deparam.reviver(key, value); |
| 55 | + } |
| 56 | + |
| 57 | + if ( last ) { |
| 58 | + // Complex key, like 'a[]' or 'a[b][c]'. At this point, the keys array |
| 59 | + // might look like ['a', ''] (array) or ['a', 'b', 'c'] (object). |
| 60 | + for ( ; i <= last; i++ ) { |
| 61 | + // If the current key part was specified, use that value as the array |
| 62 | + // index or object key. If omitted, assume an array and use the |
| 63 | + // array's length (effectively an array push). |
| 64 | + key = keys[i] !== '' ? keys[i] : current.length; |
| 65 | + if ( i < last ) { |
| 66 | + // If not the last key part, update the reference to the current |
| 67 | + // object/array, creating it if it doesn't already exist AND there's |
| 68 | + // a next key. If the next key is non-numeric and not empty string, |
| 69 | + // create an object, otherwise create an array. |
| 70 | + current = current[key] = current[key] || (isNaN(keys[i + 1]) ? {} : []); |
| 71 | + } else { |
| 72 | + // If the last key part, set the value. |
| 73 | + current[key] = value; |
| 74 | + } |
| 75 | + } |
| 76 | + } else { |
| 77 | + // Simple key. |
| 78 | + if ( $.isArray(result[key]) ) { |
| 79 | + // If the key already exists, and is an array, push the new value onto |
| 80 | + // the array. |
| 81 | + result[key].push(value); |
| 82 | + } else if ( key in result ) { |
| 83 | + // If the key already exists, and is NOT an array, turn it into an |
| 84 | + // array, pushing the new value onto it. |
| 85 | + result[key] = [result[key], value]; |
| 86 | + } else { |
| 87 | + // Otherwise, just set the value. |
| 88 | + result[key] = value; |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + return result; |
| 94 | + }; |
| 95 | + |
| 96 | + // Default reviver function, used when true is passed as the second argument |
| 97 | + // to $.deparam. Don't like it? Pass your own! |
| 98 | + deparam.reviver = function(key, value) { |
| 99 | + var specials = { |
| 100 | + 'true': true, |
| 101 | + 'false': false, |
| 102 | + 'null': null, |
| 103 | + 'undefined': undef |
| 104 | + }; |
| 105 | + |
| 106 | + return (+value + '') === value ? +value // Number |
| 107 | + : value in specials ? specials[value] // true, false, null, undefined |
| 108 | + : value; // String |
| 109 | + }; |
| 110 | + |
| 111 | +}(jQuery)); |
0 commit comments