forked from macek/jquery-serialize-object
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.serialize-object.js
129 lines (105 loc) · 3.57 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
/*!
* jQuery serializeObject
* http://github.com/macek/jquery-serialize-object
*
* Copyright 2013 Paul Macek <paulmacek@gmail.com>
* Released under the BSD license
*/
;(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var FormSerializer = module.exports = function FormSerializer(helper) {
this._helper = helper;
this._object = {};
this._pushes = {};
this._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
};
};
FormSerializer.prototype._build = function _build(base, key, value) {
base[key] = value;
return base;
};
FormSerializer.prototype._makeObject = function _nest(root, value) {
var keys = root.match(this._patterns.key), k;
// nest, nest, ..., nest
while ((k = keys.pop()) !== undefined) {
// foo[]
if (this._patterns.push.test(k)) {
var idx = this._incrementPush(root.replace(/\[\]$/, ''));
value = this._build([], idx, value);
}
// foo[n]
else if (this._patterns.fixed.test(k)) {
value = this._build([], k, value);
}
// foo; foo[bar]
else if (this._patterns.named.test(k)) {
value = this._build({}, k, value);
}
}
return value;
};
FormSerializer.prototype._incrementPush = function _incrementPush(key) {
if (this._pushes[key] === undefined) {
this._pushes[key] = 0;
}
return this._pushes[key]++;
};
FormSerializer.prototype.addPair = function addPair(pair) {
if (!this._patterns.validate.test(pair.name)) return this;
var obj = this._makeObject(pair.name, pair.value);
this._object = this._helper.extend(true, this._object, obj);
return this;
};
FormSerializer.prototype.addPairs = function addPairs(pairs) {
if (!this._helper.isArray(pairs)) {
throw new Error("formSerializer.addPairs expects an Array");
}
pairs.forEach(this.addPair.bind(this));
return this;
};
FormSerializer.prototype.serialize = function serialize() {
return this._object
};
FormSerializer.prototype.serializeJSON = function serializeJSON() {
return JSON.stringify(this.serialize());
};
},{}],2:[function(require,module,exports){
var Helper = module.exports = function Helper(jQuery) {
// jQuery.extend requirement
if (typeof jQuery.extend === 'function') {
this.extend = jQuery.extend;
}
else {
throw new Error("jQuery is required to use jquery-serialize-object");
}
// Array.isArray polyfill
if(typeof Array.isArray === 'function') {
this.isArray = Array.isArray;
}
else {
this.isArray = function isArray(input) {
return Object.prototype.toString.call(input) === "[object Array]";
};
}
};
},{}],3:[function(require,module,exports){
var FormSerializer = require("./form-serializer"),
Helper = require("./helper");
(function($) {
var helper = new Helper($ || {});
$.fn.serializeObject = function() {
var form = $(this);
if (form.length > 1) {
return new Error("jquery-serialize-object can only serialize one form at a time");
}
return new FormSerializer(helper).
addPairs(form.serializeArray()).
serialize();
};
})(jQuery);
},{"./form-serializer":1,"./helper":2}]},{},[3])
;