-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
51 lines (46 loc) · 1.27 KB
/
index.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
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(['module', 'exports'], factory);
} else if (typeof exports !== "undefined") {
factory(module, exports);
} else {
var mod = {
exports: {}
};
factory(mod, mod.exports);
global.formToObject = mod.exports;
}
})(this, function (module, exports) {
'use strict';
exports.default = function (form) {
var obj = Object.create(null);
function assign(name, value) {
if (obj[name] === undefined) {
obj[name] = value;
} else if (Array.isArray(obj[name])) {
obj[name].push(value);
} else {
obj[name] = [obj[name], value];
}
}
Array.prototype.slice.call(form.elements).forEach(function (field) {
if (field.name && !field.disabled && (['file', 'reset', 'button'].indexOf(field.type) === -1)) {
if (field.type === 'select-multiple') {
Array.prototype.slice.call(field.options).forEach(function (option) {
if (option.selected) {
assign(field.name, option.value);
}
})
} else if (field.type === 'checkbox' || field.type === 'radio') {
if (field.checked) {
assign(field.name, field.value);
}
} else {
assign(field.name, field.value);
}
}
})
return obj;
};
module.exports = exports['default'];
});