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