Skip to content

Commit

Permalink
Add tests for custom type functions with value and the element being …
Browse files Browse the repository at this point in the history
…passed
  • Loading branch information
marioizquierdo committed Dec 31, 2020
1 parent 703f4c3 commit 4e3c7ff
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
9 changes: 3 additions & 6 deletions jquery.serializejson.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
$.fn.serializeJSON = function (options) {
var f = $.serializeJSON;
var $form = this; // NOTE: the set of matched elements is most likely a form, but it could also be a group of inputs
var opts = f.setupOpts(options); // validate and apply defaults
var opts = f.setupOpts(options); // validate options and apply defaults
var typeFunctions = $.extend({}, opts.defaultTypes, opts.customTypes);

// Make a list with {name, value, el} for each input element.
// Make a list with {name, value, el} for each input element
var serializedArray = f.serializeArray($form, opts);

// Convert the serializedArray into a serializedObject with nested keys
Expand Down Expand Up @@ -169,7 +169,7 @@
if (val == null) {
val = opts.checkboxUncheckedValue;
}
return val
return val;
},

// Parse value with type function
Expand All @@ -193,14 +193,11 @@
} else {
return [name, ""];
}
return typeFunc(valStr, el);
},

// Check if this input should be skipped when it has a falsy value,
// depending on the options to skip values by name or type, and the data-skip-falsy attribute.
shouldSkipFalsy: function(name, nameSansType, type, el, opts) {
var f = $.serializeJSON;

var skipFromDataAttr = $(el).attr("data-skip-falsy");
if (skipFromDataAttr != null) {
return skipFromDataAttr !== "false"; // any value is true, except the string "false"
Expand Down
29 changes: 28 additions & 1 deletion spec/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,8 @@ describe("$.serializeJSON", function() {
describe("customTypes", function() {
it("serializes value according to custom function without disturbing default types", function() {
$form = form([
inputText("foo:alwaysBoo", "0"),
inputText("foo:alwaysBoo", "0"),

inputText("notype", "default type is :string"),
inputText("string:string", ":string type overrides parsing options"),
inputText("excludes:skip", "Use :skip to not include this field in the result"),
Expand Down Expand Up @@ -938,6 +939,7 @@ describe("$.serializeJSON", function() {

expect(obj).toEqual({
"foo": "Boo",

"notype": "default type is :string",
"string": ":string type overrides parsing options",
// :skip type removes the field from the output
Expand Down Expand Up @@ -966,6 +968,31 @@ describe("$.serializeJSON", function() {
});
});

it("type functions receive the value and the DOM element of the field that is being parsed", function() {
$form = form([
inputText("foo1:withXoxo", "luv"),
inputText("foo2:withXoxo", "luv").attr("data-Xoxo", "CustomPassedXoxo"),
inputText("foo3:multiply", "3").attr("data-multiply", "5"),
]);
obj = $form.serializeJSON({
customTypes: {
withXoxo: function(val, el) {
var xoxo = $(el).attr("data-Xoxo") || "Xoxo";
return val + xoxo;
},
multiply: function(val, el) {
var mult = $(el).attr("data-multiply");
return Number(val) * Number(mult);
},
}
});
expect(obj).toEqual({
"foo1": "luvXoxo",
"foo2": "luvCustomPassedXoxo",
"foo3": 15 // 3 * 5
});
});

it("overrides defaultTypes", function() {
$form = form([
inputText("incremented:number", "0"),
Expand Down

0 comments on commit 4e3c7ff

Please sign in to comment.