Skip to content

option to use numeric keys #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ $.extend(FormSerializer.patterns, {
//=> {foo: {bar: "a", bof: "b"}, hello: "world"}
```

*Parameters*

* `numkeys` — allow to use number keys instead of fixed numeric array indexes. False by default.

```js
$("form").serializeObject({'numkeys':true});
```

Tests
-----

Expand Down Expand Up @@ -203,4 +211,4 @@ $ npm run-script build
[legacy]: https://github.com/macek/jquery-serialize-object/releases/tag/1.0.0
[dash-notation]: https://github.com/macek/jquery-serialize-object/issues/6
[dot-notation]: https://github.com/macek/jquery-serialize-object/issues/4
[boolean]: https://github.com/macek/jquery-serialize-object/blob/master/test/integration/encode-test.js
[boolean]: https://github.com/macek/jquery-serialize-object/blob/master/test/integration/encode-test.js
2 changes: 1 addition & 1 deletion dist/jquery.serialize-object.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 19 additions & 3 deletions jquery.serialize-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
named: /^[a-z0-9_]+$/i
};

// allow to use number keys instead of fixed numeric array indexes
var numkeys = false;

function FormSerializer(helper, $form) {

// private variables
Expand All @@ -61,7 +64,11 @@

// foo[n]
else if (patterns.fixed.test(k)) {
value = build([], k, value);
var base = [];
if (numkeys) {
base = {};
}
value = build(base, k, value);
}

// foo; foo[bar]
Expand Down Expand Up @@ -121,15 +128,24 @@
this.serializeJSON = serializeJSON;
}

function parseOpt(opt) {
if (!opt) return true;
if (opt.numkeys && opt.numkeys === true) {
numkeys = true;
}
}

FormSerializer.patterns = patterns;

FormSerializer.serializeObject = function serializeObject() {
FormSerializer.serializeObject = function serializeObject(opt) {
parseOpt(opt);
return new FormSerializer($, this).
addPairs(this.serializeArray()).
serialize();
};

FormSerializer.serializeJSON = function serializeJSON() {
FormSerializer.serializeJSON = function serializeJSON(opt) {
parseOpt(opt);
return new FormSerializer($, this).
addPairs(this.serializeArray()).
serializeJSON();
Expand Down