jquery.deparam.js 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. jQuery deparam is an extraction of the deparam method from Ben Alman's jQuery BBQ
  3. http://benalman.com/projects/jquery-bbq-plugin/
  4. */
  5. (function ($) {
  6. $.deparam = function (params, coerce) {
  7. var obj = {},
  8. coerce_types = { 'true': !0, 'false': !1, 'null': null };
  9. // Iterate over all name=value pairs.
  10. $.each(params.replace(/\+/g, ' ').split('&'), function (j,v) {
  11. var param = v.split('='),
  12. key = decodeURIComponent(param[0]),
  13. val,
  14. cur = obj,
  15. i = 0,
  16. // If key is more complex than 'foo', like 'a[]' or 'a[b][c]', split it
  17. // into its component parts.
  18. keys = key.split(']['),
  19. keys_last = keys.length - 1;
  20. // If the first keys part contains [ and the last ends with ], then []
  21. // are correctly balanced.
  22. if (/\[/.test(keys[0]) && /\]$/.test(keys[keys_last])) {
  23. // Remove the trailing ] from the last keys part.
  24. keys[keys_last] = keys[keys_last].replace(/\]$/, '');
  25. // Split first keys part into two parts on the [ and add them back onto
  26. // the beginning of the keys array.
  27. keys = keys.shift().split('[').concat(keys);
  28. keys_last = keys.length - 1;
  29. } else {
  30. // Basic 'foo' style key.
  31. keys_last = 0;
  32. }
  33. // Are we dealing with a name=value pair, or just a name?
  34. if (param.length === 2) {
  35. val = decodeURIComponent(param[1]);
  36. // Coerce values.
  37. if (coerce) {
  38. val = val && !isNaN(val) ? +val // number
  39. : val === 'undefined' ? undefined // undefined
  40. : coerce_types[val] !== undefined ? coerce_types[val] // true, false, null
  41. : val; // string
  42. }
  43. if ( keys_last ) {
  44. // Complex key, build deep object structure based on a few rules:
  45. // * The 'cur' pointer starts at the object top-level.
  46. // * [] = array push (n is set to array length), [n] = array if n is
  47. // numeric, otherwise object.
  48. // * If at the last keys part, set the value.
  49. // * For each keys part, if the current level is undefined create an
  50. // object or array based on the type of the next keys part.
  51. // * Move the 'cur' pointer to the next level.
  52. // * Rinse & repeat.
  53. for (; i <= keys_last; i++) {
  54. key = keys[i] === '' ? cur.length : keys[i];
  55. cur = cur[key] = i < keys_last
  56. ? cur[key] || (keys[i+1] && isNaN(keys[i+1]) ? {} : [])
  57. : val;
  58. }
  59. } else {
  60. // Simple key, even simpler rules, since only scalars and shallow
  61. // arrays are allowed.
  62. if ($.isArray(obj[key])) {
  63. // val is already an array, so push on the next value.
  64. obj[key].push( val );
  65. } else if (obj[key] !== undefined) {
  66. // val isn't an array, but since a second value has been specified,
  67. // convert val into an array.
  68. obj[key] = [obj[key], val];
  69. } else {
  70. // val is a scalar.
  71. obj[key] = val;
  72. }
  73. }
  74. } else if (key) {
  75. // No value was defined, so set something meaningful.
  76. obj[key] = coerce
  77. ? undefined
  78. : '';
  79. }
  80. });
  81. return obj;
  82. };
  83. })(jQuery);