form-serialize
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -0,1 +1,6 @@ | ||
# 0.4.0 (2014-12-16) | ||
* consistently serialize [] params into arrays | ||
* fix multi-select field support | ||
# 0.3.0 / 2014-05-01 | ||
@@ -2,0 +7,0 @@ |
30
index.js
@@ -12,3 +12,4 @@ // get successful control from form and assemble into object | ||
// keys with brackets for hash keys | ||
var brackets_regex = /\[(.+?)\]/g; | ||
var object_brackets_regex = /\[(.+?)\]/g; | ||
var array_brackets_regex = /\[\]$/; | ||
var brackeks_prefix_regex = /^(.+?)\[/; | ||
@@ -70,5 +71,5 @@ | ||
var options = element.options; | ||
for (var i=0 ; i<options.length ; ++i) { | ||
var option = options[i]; | ||
var selectOptions = element.options; | ||
for (var j=0 ; j<selectOptions.length ; ++j) { | ||
var option = selectOptions[j]; | ||
if (option.selected) { | ||
@@ -90,2 +91,7 @@ result = serializer(result, key, option.value); | ||
function hash_serializer(result, key, value) { | ||
var is_array_key = has_array_brackets(key); | ||
if (is_array_key) { | ||
key = key.replace(array_brackets_regex, ''); | ||
} | ||
if (key in result) { | ||
@@ -99,7 +105,7 @@ var existing = result[key]; | ||
else { | ||
if (has_brackets(key)) { | ||
if (has_object_brackets(key)) { | ||
extract_from_brackets(result, key, value); | ||
} | ||
else { | ||
result[key] = value; | ||
result[key] = is_array_key ? [value] : value; | ||
} | ||
@@ -122,9 +128,13 @@ } | ||
function has_brackets(string) { | ||
return string.match(brackets_regex); | ||
function has_object_brackets(string) { | ||
return string.match(object_brackets_regex); | ||
}; | ||
function has_array_brackets(string) { | ||
return string.match(array_brackets_regex); | ||
} | ||
function matches_between_brackets(string) { | ||
// Make sure to isolate brackets_regex from .exec() calls | ||
var regex = new RegExp(brackets_regex); | ||
// Make sure to isolate object_brackets_regex from .exec() calls | ||
var regex = new RegExp(object_brackets_regex); | ||
var matches = []; | ||
@@ -131,0 +141,0 @@ var match; |
{ | ||
"name": "form-serialize", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "serialize html forms", | ||
@@ -11,3 +11,3 @@ "main": "index.js", | ||
"domify": "~1.0.0", | ||
"zuul": "~1.6.4" | ||
"zuul": "~1.15.1" | ||
}, | ||
@@ -30,2 +30,2 @@ "scripts": { | ||
} | ||
} | ||
} |
@@ -65,2 +65,29 @@ # form-serialize [![Build Status](https://travis-ci.org/defunctzombie/form-serialize.png?branch=master)](https://travis-ci.org/defunctzombie/form-serialize) | ||
### explicit array fields | ||
Fields who's name ends with `[]` are **always** serialized as an array field in `hash` output mode using the default hash serializer. | ||
The field name also gets the brackets removed from its name. | ||
This does not affect `url-encoding` mode output in any way. | ||
```html | ||
<form id="example-form"> | ||
<input type="checkbox" name="foo[]" value="bar" checked /> | ||
<input type="checkbox" name="foo[]" value="baz" /> | ||
<input type="submit" value="do it!"/> | ||
</form> | ||
``` | ||
```js | ||
var serialize = require('form-serialize'); | ||
var form = document.querySelector('#example-form'); | ||
var obj = serialize(form, { hash: true }); | ||
// obj -> { foo: ['bar'] } | ||
var str = serialize(form); | ||
// str -> "foo[]=bar" | ||
``` | ||
## references | ||
@@ -67,0 +94,0 @@ |
@@ -87,2 +87,23 @@ var assert = require('assert'); | ||
test('checkboxes - array', function() { | ||
var form = domify('<form>' + | ||
'<input type="checkbox" name="foo[]" value="bar" checked/>' + | ||
'<input type="checkbox" name="foo[]" value="baz" checked/>' + | ||
'</form>'); | ||
hash_check(form, { | ||
'foo': ['bar', 'baz'] | ||
}); | ||
str_check(form, 'foo%5B%5D=bar&foo%5B%5D=baz'); | ||
}); | ||
test('checkboxes - array with single item', function() { | ||
var form = domify('<form>' + | ||
'<input type="checkbox" name="foo[]" value="bar" checked/>' + | ||
'</form>'); | ||
hash_check(form, { | ||
'foo': ['bar'] | ||
}); | ||
str_check(form, 'foo%5B%5D=bar'); | ||
}); | ||
test('select - single', function() { | ||
@@ -89,0 +110,0 @@ var form = domify('<form>' + |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17345
335
99