jquery.serializeObject.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * jQuery serializeObject
  3. * @copyright 2014, macek <paulmacek@gmail.com>
  4. * @link https://github.com/macek/jquery-serialize-object
  5. * @license BSD
  6. * @version 2.5.0
  7. */
  8. (function(root, factory) {
  9. // AMD
  10. if (typeof define === "function" && define.amd) {
  11. define(["exports", "jquery"], function(exports, $) {
  12. return factory(exports, $);
  13. });
  14. }
  15. // CommonJS
  16. else if (typeof exports !== "undefined") {
  17. var $ = require("jquery");
  18. factory(exports, $);
  19. }
  20. // Browser
  21. else {
  22. factory(root, (root.jQuery || root.Zepto || root.ender || root.$));
  23. }
  24. }(this, function(exports, $) {
  25. var patterns = {
  26. validate: /^[a-z_][a-z0-9_]*(?:\[(?:\d*|[a-z0-9_]+)\])*$/i,
  27. key: /[a-z0-9_]+|(?=\[\])/gi,
  28. push: /^$/,
  29. fixed: /^\d+$/,
  30. named: /^[a-z0-9_]+$/i
  31. };
  32. function FormSerializer(helper, $form) {
  33. // private variables
  34. var data = {},
  35. pushes = {};
  36. // private API
  37. function build(base, key, value) {
  38. base[key] = value;
  39. return base;
  40. }
  41. function makeObject(root, value) {
  42. var keys = root.match(patterns.key), k;
  43. // nest, nest, ..., nest
  44. while ((k = keys.pop()) !== undefined) {
  45. // foo[]
  46. if (patterns.push.test(k)) {
  47. var idx = incrementPush(root.replace(/\[\]$/, ''));
  48. value = build([], idx, value);
  49. }
  50. // foo[n]
  51. else if (patterns.fixed.test(k)) {
  52. value = build([], k, value);
  53. }
  54. // foo; foo[bar]
  55. else if (patterns.named.test(k)) {
  56. value = build({}, k, value);
  57. }
  58. }
  59. return value;
  60. }
  61. function incrementPush(key) {
  62. if (pushes[key] === undefined) {
  63. pushes[key] = 0;
  64. }
  65. return pushes[key]++;
  66. }
  67. function encode(pair) {
  68. switch ($('[name="' + pair.name + '"]', $form).attr("type")) {
  69. case "checkbox":
  70. return pair.value === "on" ? true : pair.value;
  71. default:
  72. return pair.value;
  73. }
  74. }
  75. function addPair(pair) {
  76. if (!patterns.validate.test(pair.name)) return this;
  77. var obj = makeObject(pair.name, encode(pair));
  78. data = helper.extend(true, data, obj);
  79. return this;
  80. }
  81. function addPairs(pairs) {
  82. if (!helper.isArray(pairs)) {
  83. throw new Error("formSerializer.addPairs expects an Array");
  84. }
  85. for (var i=0, len=pairs.length; i<len; i++) {
  86. this.addPair(pairs[i]);
  87. }
  88. return this;
  89. }
  90. function serialize() {
  91. return data;
  92. }
  93. function serializeJSON() {
  94. return JSON.stringify(serialize());
  95. }
  96. // public API
  97. this.addPair = addPair;
  98. this.addPairs = addPairs;
  99. this.serialize = serialize;
  100. this.serializeJSON = serializeJSON;
  101. }
  102. FormSerializer.patterns = patterns;
  103. FormSerializer.serializeObject = function serializeObject() {
  104. return new FormSerializer($, this).
  105. addPairs(this.serializeArray()).
  106. serialize();
  107. };
  108. FormSerializer.serializeJSON = function serializeJSON() {
  109. return new FormSerializer($, this).
  110. addPairs(this.serializeArray()).
  111. serializeJSON();
  112. };
  113. if (typeof $.fn !== "undefined") {
  114. $.fn.serializeObject = FormSerializer.serializeObject;
  115. $.fn.serializeJSON = FormSerializer.serializeJSON;
  116. }
  117. exports.FormSerializer = FormSerializer;
  118. return FormSerializer;
  119. }));