Files
2026-04-28 15:13:50 +02:00

344 lines
7.9 KiB
JavaScript

/*!
SerializeJSON jQuery plugin.
https://github.com/marioizquierdo/jquery.serializeJSON
version 2.9.0 (Jan, 2018)
Copyright (c) 2012-2018 Mario Izquierdo
Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*/
(function (a) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], a);
} else {
if (typeof exports === 'object') {
var b = require('jquery');
module.exports = a(b);
} else {
a(window.jQuery || window.Zepto || window.$);
}
}
})(function (a) {
a.fn.serializeJSON = function (o) {
var h, p, b, i, k, c, m, d, j, e, l, n, g;
h = a.serializeJSON;
p = this;
b = h.setupOpts(o);
i = p.serializeArray();
h.readCheckboxUncheckedValues(i, b, p);
k = {};
a.each(i, function (f, q) {
c = q.name;
m = q.value;
j = h.extractTypeAndNameWithNoType(c);
e = j.nameWithNoType;
l = j.type;
if (!l) {
l = h.attrFromInputWithName(p, c, 'data-value-type');
}
h.validateType(c, l, b);
if (l !== 'skip') {
n = h.splitInputNameIntoKeysArray(e);
d = h.parseValue(m, c, l, b);
g = !d && h.shouldSkipFalsy(p, c, e, l, b);
if (!g) {
h.deepSet(k, n, d, b);
}
}
});
return k;
};
a.serializeJSON = {
defaultOptions: {
checkboxUncheckedValue: undefined,
parseNumbers: false,
parseBooleans: false,
parseNulls: false,
parseAll: false,
parseWithFunction: null,
skipFalsyValuesForTypes: [],
skipFalsyValuesForFields: [],
customTypes: {},
defaultTypes: {
string: function (b) {
return String(b);
},
number: function (b) {
return Number(b);
},
boolean: function (c) {
var b = ['false', 'null', 'undefined', '', '0'];
return b.indexOf(c) === -1;
},
null: function (c) {
var b = ['false', 'null', 'undefined', '', '0'];
return b.indexOf(c) === -1 ? c : null;
},
array: function (b) {
return JSON.parse(b);
},
object: function (b) {
return JSON.parse(b);
},
auto: function (b) {
return a.serializeJSON.parseValue(b, null, null, {
parseNumbers: true,
parseBooleans: true,
parseNulls: true,
});
},
skip: null,
},
useIntKeysAsArrayIndex: false,
},
setupOpts: function (d) {
var e, g, b, i, c, h;
h = a.serializeJSON;
if (d == null) {
d = {};
}
b = h.defaultOptions || {};
g = [
'checkboxUncheckedValue',
'parseNumbers',
'parseBooleans',
'parseNulls',
'parseAll',
'parseWithFunction',
'skipFalsyValuesForTypes',
'skipFalsyValuesForFields',
'customTypes',
'defaultTypes',
'useIntKeysAsArrayIndex',
];
for (e in d) {
if (g.indexOf(e) === -1) {
throw new Error(
"serializeJSON ERROR: invalid option '" +
e +
"'. Please use one of " +
g.join(', ')
);
}
}
i = function (f) {
return d[f] !== false && d[f] !== '' && (d[f] || b[f]);
};
c = i('parseAll');
return {
checkboxUncheckedValue: i('checkboxUncheckedValue'),
parseNumbers: c || i('parseNumbers'),
parseBooleans: c || i('parseBooleans'),
parseNulls: c || i('parseNulls'),
parseWithFunction: i('parseWithFunction'),
skipFalsyValuesForTypes: i('skipFalsyValuesForTypes'),
skipFalsyValuesForFields: i('skipFalsyValuesForFields'),
typeFunctions: a.extend(
{},
i('defaultTypes'),
i('customTypes')
),
useIntKeysAsArrayIndex: i('useIntKeysAsArrayIndex'),
};
},
parseValue: function (d, c, e, g) {
var h, b;
h = a.serializeJSON;
b = d;
if (g.typeFunctions && e && g.typeFunctions[e]) {
b = g.typeFunctions[e](d);
} else {
if (g.parseNumbers && h.isNumeric(d)) {
b = Number(d);
} else {
if (g.parseBooleans && (d === 'true' || d === 'false')) {
b = d === 'true';
} else {
if (g.parseNulls && d == 'null') {
b = null;
} else {
if (g.typeFunctions && g.typeFunctions.string) {
b = g.typeFunctions.string(d);
}
}
}
}
}
if (g.parseWithFunction && !e) {
b = g.parseWithFunction(b, c);
}
return b;
},
isObject: function (b) {
return b === Object(b);
},
isUndefined: function (b) {
return b === void 0;
},
isValidArrayIndex: function (b) {
return /^[0-9]+$/.test(String(b));
},
isNumeric: function (b) {
return b - parseFloat(b) >= 0;
},
optionKeys: function (d) {
if (Object.keys) {
return Object.keys(d);
} else {
var b,
c = [];
for (b in d) {
c.push(b);
}
return c;
}
},
readCheckboxUncheckedValues: function (i, b, k) {
var d, g, j, h, e, c;
if (b == null) {
b = {};
}
e = a.serializeJSON;
d = 'input[type=checkbox][name]:not(:checked):not([disabled])';
g = k.find(d).add(k.filter(d));
g.each(function (f, l) {
j = a(l);
h = j.attr('data-unchecked-value');
if (h == null) {
h = b.checkboxUncheckedValue;
}
if (h != null) {
if (l.name && l.name.indexOf('[][') !== -1) {
throw new Error(
"serializeJSON ERROR: checkbox unchecked values are not supported on nested arrays of objects like '" +
l.name +
"'. See https://github.com/marioizquierdo/jquery.serializeJSON/issues/67"
);
}
i.push({ name: l.name, value: h });
}
});
},
extractTypeAndNameWithNoType: function (c) {
var b;
if ((b = c.match(/(.*):([^:]+)$/))) {
return { nameWithNoType: b[1], type: b[2] };
} else {
return { nameWithNoType: c, type: null };
}
},
shouldSkipFalsy: function (k, c, e, i, b) {
var h = a.serializeJSON;
var j = h.attrFromInputWithName(k, c, 'data-skip-falsy');
if (j != null) {
return j !== 'false';
}
var g = b.skipFalsyValuesForFields;
if (g && (g.indexOf(e) !== -1 || g.indexOf(c) !== -1)) {
return true;
}
var d = b.skipFalsyValuesForTypes;
if (i == null) {
i = 'string';
}
if (d && d.indexOf(i) !== -1) {
return true;
}
return false;
},
attrFromInputWithName: function (d, e, f) {
var c, b, h, g;
c = e.replace(/(:|\.|\[|\]|\s)/g, '\\$1');
b = '[name="' + c + '"]';
h = d.find(b).add(d.filter(b));
return h.attr(f);
},
validateType: function (b, c, d) {
var g, e;
e = a.serializeJSON;
g = e.optionKeys(
d ? d.typeFunctions : e.defaultOptions.defaultTypes
);
if (!c || g.indexOf(c) !== -1) {
return true;
} else {
throw new Error(
'serializeJSON ERROR: Invalid type ' +
c +
" found in input name '" +
b +
"', please use one of " +
g.join(', ')
);
}
},
splitInputNameIntoKeysArray: function (b) {
var c, d;
d = a.serializeJSON;
c = b.split('[');
c = a.map(c, function (e) {
return e.replace(/\]/g, '');
});
if (c[0] === '') {
c.shift();
}
return c;
},
deepSet: function (c, l, j, b) {
var k, h, g, i, d, e;
if (b == null) {
b = {};
}
e = a.serializeJSON;
if (e.isUndefined(c)) {
throw new Error(
"ArgumentError: param 'o' expected to be an object or array, found undefined"
);
}
if (!l || l.length === 0) {
throw new Error(
"ArgumentError: param 'keys' expected to be an array with least one element"
);
}
k = l[0];
if (l.length === 1) {
if (k === '') {
c.push(j);
} else {
c[k] = j;
}
} else {
h = l[1];
if (k === '') {
i = c.length - 1;
d = c[i];
if (
e.isObject(d) &&
(e.isUndefined(d[h]) || l.length > 2)
) {
k = i;
} else {
k = i + 1;
}
}
if (h === '') {
if (e.isUndefined(c[k]) || !a.isArray(c[k])) {
c[k] = [];
}
} else {
if (b.useIntKeysAsArrayIndex && e.isValidArrayIndex(h)) {
if (e.isUndefined(c[k]) || !a.isArray(c[k])) {
c[k] = [];
}
} else {
if (e.isUndefined(c[k]) || !e.isObject(c[k])) {
c[k] = {};
}
}
}
g = l.slice(1);
e.deepSet(c[k], g, j, b);
}
},
};
});