Skip to content

Commit

Permalink
option to use numeric keys
Browse files Browse the repository at this point in the history
  • Loading branch information
evgenyvas committed Mar 21, 2016
1 parent 68ec732 commit df7871c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
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

0 comments on commit df7871c

Please sign in to comment.