form-serialize
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -1,7 +0,9 @@ | ||
# changelog | ||
# 0.3.0 / 2014-05-01 | ||
## 0.2.0 | ||
* support bracket notation for hash serialization | ||
# 0.2.0 / 2013-12-05 | ||
* add `disabled` option to include disabled fields | ||
## 0.1.1 | ||
# 0.1.1 / 2013-08-26 |
68
index.js
@@ -11,2 +11,6 @@ // get successful control from form and assemble into object | ||
// keys with brackets for hash keys | ||
var brackets_regex = /\[(.+?)\]/g; | ||
var brackeks_prefix_regex = /^(.+?)\[/; | ||
// serializes form fields | ||
@@ -93,3 +97,8 @@ // @param form MUST be an HTMLForm element | ||
else { | ||
result[key] = value; | ||
if (has_brackets(key)) { | ||
extract_from_brackets(result, key, value); | ||
} | ||
else { | ||
result[key] = value; | ||
} | ||
} | ||
@@ -111,2 +120,59 @@ | ||
function has_brackets(string) { | ||
return string.match(brackets_regex); | ||
}; | ||
function matches_between_brackets(string) { | ||
// Make sure to isolate brackets_regex from .exec() calls | ||
var regex = new RegExp(brackets_regex); | ||
var matches = []; | ||
var match; | ||
while (match = regex.exec(string)) { | ||
matches.push(match[1]); | ||
} | ||
return matches; | ||
}; | ||
function extract_from_brackets(result, key, value) { | ||
var prefix = key.match(brackeks_prefix_regex)[1]; | ||
// Set the key if it doesn't exist | ||
if (! result[prefix]) result[prefix] = {}; | ||
var parent = result[prefix]; | ||
var matches_between = matches_between_brackets(key); | ||
var length = matches_between.length; | ||
for (var i = 0; i < length; i++) { | ||
var child = matches_between[i]; | ||
var isLast = (length === i + 1); | ||
if (isLast) { | ||
var existing = parent[child]; | ||
if (existing) { | ||
if (! Array.isArray(existing)) { | ||
parent[child] = [ existing ]; | ||
} | ||
parent[child].push(value); | ||
} | ||
else { | ||
// Finally make the assignment | ||
parent[child] = value; | ||
} | ||
} | ||
else { | ||
// This is a nested key, set it properly for the next iteration | ||
parent[child] = {}; | ||
parent = parent[child]; | ||
} | ||
} | ||
parent = value; | ||
}; | ||
module.exports = serialize; |
{ | ||
"name": "form-serialize", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "serialize html forms", | ||
@@ -11,3 +11,3 @@ "main": "index.js", | ||
"domify": "~1.0.0", | ||
"zuul": "~1.0.4" | ||
"zuul": "~1.6.4" | ||
}, | ||
@@ -14,0 +14,0 @@ "scripts": { |
@@ -61,3 +61,3 @@ # form-serialize [![Build Status](https://travis-ci.org/defunctzombie/form-serialize.png?branch=master)](https://travis-ci.org/defunctzombie/form-serialize) | ||
only [successfull control](http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2) form fields are serialized | ||
only [successfull control](http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2) form fields are serialized (with the exception of disabled fields if disabled option is set) | ||
@@ -64,0 +64,0 @@ multiselect fields with more than one value will result in an array of values in the `hash` output mode using the default hash serializer |
@@ -15,3 +15,3 @@ var assert = require('assert'); | ||
var disabled_check = function(form, exp) { | ||
assert.deepEqual(serialize(form, { hash : false, disabled: true }), exp); | ||
assert.deepEqual(serialize(form, { hash : false, disabled: true }), exp); | ||
}; | ||
@@ -161,1 +161,28 @@ | ||
}); | ||
test('nested hashes with brackets', function() { | ||
var form = domify('<form>' + | ||
'<input type="email" name="account[name]" value="Foo Dude">' + | ||
'<input type="text" name="account[email]" value="foobar@example.org">' + | ||
'<input type="text" name="account[address][city]" value="Qux">' + | ||
'<select name="beer[type]" multiple>' + | ||
' <option value="ipa" selected>IPA</option>' + | ||
' <option value="pale-ale">Pale Ale</option>' + | ||
' <option value="amber-ale" selected>Amber Ale</option>' + | ||
'</select>' + | ||
'</form>'); | ||
hash_check(form, { | ||
account: { | ||
name: 'Foo Dude', | ||
email: 'foobar@example.org', | ||
address: { | ||
city: 'Qux' | ||
} | ||
}, | ||
beer: { | ||
type: [ 'ipa', 'amber-ale' ] | ||
} | ||
}); | ||
str_check(form, 'account%5Bname%5D=Foo+Dude&account%5Bemail%5D=foobar%40example.org&account%5Baddress%5D%5Bcity%5D=Qux&beer%5Btype%5D=ipa&beer%5Btype%5D=amber-ale'); | ||
}); |
Sorry, the diff of this file is not supported yet
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
15564
308