Permalink
Browse files

Add readme/full test, fix jshint recommendations and update code style

  • Loading branch information...
1 parent c558560 commit 2f03335166b1546c26cb2031252dec2417664f1a @danheberden committed Aug 5, 2012
Showing with 89 additions and 63 deletions.
  1. +32 −32 dist/serializeObject.js
  2. +1 −1 dist/serializeObject.min.js
  3. +29 −29 src/serializeObject.js
  4. +9 −1 test/serializeObject.html
  5. +18 −0 test/serializeObject_test.js
@@ -2,41 +2,41 @@
* https://github.com/danheberden/serializeObject
* Copyright (c) 2012 Dan Heberden; Licensed MIT, GPL */
-(function($){
+(function( $ ){
$.fn.serializeObject = function() {
- if ( !this.length ) { return false; }
-
- var $el = this,
- data = {},
- lookup = data; //current reference of data
-
- $el.find(':input[type!="checkbox"][type!="radio"], input:checked').each(function() {
- // data[a][b] becomes [ data, a, b ]
- var named = this.name.replace(/\[([^\]]+)?\]/g, ',$1').split(','),
- cap = named.length - 1,
- i = 0;
-
- // Ensure that only elements with valid `name` properties will be serialized
- if ( named[ 0 ] ) {
- for ( ; i < cap; i++ ) {
- // move down the tree - create objects or array if necessary
- lookup = lookup[ named[i] ] = lookup[ named[i] ] ||
- ( named[i+1] == "" ? [] : {} );
- }
-
- // at the end, psuh or assign the value
- if ( lookup.length != undefined ) {
- lookup.push( $(this).val() );
- }else {
- lookup[ named[ cap ] ] = $(this).val();
- }
-
- // assign the reference back to root
- lookup = data;
+ if ( this.length < 1) {
+ return false;
+ }
+
+ var $el = this;
+ var data = {};
+ var lookup = data; //current reference of data
+
+ $el.find( ':input[type!="checkbox"][type!="radio"], input:checked' ).each( function() {
+ // data[a][b] becomes [ data, a, b ]
+ var named = this.name.replace(/\[([^\]]+)?\]/g, ',$1').split(',');
+ var cap = named.length - 1;
+
+ // Ensure that only elements with valid `name` properties will be serialized
+ if ( named[ 0 ] ) {
+ for ( var i = 0; i < cap; i++ ) {
+ // move down the tree - create objects or array if necessary
+ lookup = lookup[ named[i] ] = lookup[ named[i] ] ||
+ ( named[ i + 1 ] === "" ? [] : {} );
+ }
+ // at the end, push or assign the value
+ if ( lookup.length !== undefined ) {
+ lookup.push( $( this ).val() );
+ }else {
+ lookup[ named[ cap ] ] = $( this ).val();
}
- });
+
+ // assign the reference back to root
+ lookup = data;
+ }
+ });
return data;
};
-})(jQuery);
+}( jQuery ));
Oops, something went wrong.
@@ -5,41 +5,41 @@
* Copyright (c) 2012 Dan Heberden
* Licensed under the MIT, GPL licenses.
*/
-(function($){
+(function( $ ){
$.fn.serializeObject = function() {
- if ( !this.length ) { return false; }
+ if ( this.length < 1) {
+ return false;
+ }
- var $el = this,
- data = {},
- lookup = data; //current reference of data
+ var $el = this;
+ var data = {};
+ var lookup = data; //current reference of data
- $el.find(':input[type!="checkbox"][type!="radio"], input:checked').each(function() {
- // data[a][b] becomes [ data, a, b ]
- var named = this.name.replace(/\[([^\]]+)?\]/g, ',$1').split(','),
- cap = named.length - 1,
- i = 0;
+ $el.find( ':input[type!="checkbox"][type!="radio"], input:checked' ).each( function() {
+ // data[a][b] becomes [ data, a, b ]
+ var named = this.name.replace(/\[([^\]]+)?\]/g, ',$1').split(',');
+ var cap = named.length - 1;
- // Ensure that only elements with valid `name` properties will be serialized
- if ( named[ 0 ] ) {
- for ( ; i < cap; i++ ) {
- // move down the tree - create objects or array if necessary
- lookup = lookup[ named[i] ] = lookup[ named[i] ] ||
- ( named[i+1] == "" ? [] : {} );
- }
-
- // at the end, psuh or assign the value
- if ( lookup.length != undefined ) {
- lookup.push( $(this).val() );
- }else {
- lookup[ named[ cap ] ] = $(this).val();
- }
-
- // assign the reference back to root
- lookup = data;
+ // Ensure that only elements with valid `name` properties will be serialized
+ if ( named[ 0 ] ) {
+ for ( var i = 0; i < cap; i++ ) {
+ // move down the tree - create objects or array if necessary
+ lookup = lookup[ named[i] ] = lookup[ named[i] ] ||
+ ( named[ i + 1 ] === "" ? [] : {} );
+ }
+ // at the end, push or assign the value
+ if ( lookup.length !== undefined ) {
+ lookup.push( $( this ).val() );
+ }else {
+ lookup[ named[ cap ] ] = $( this ).val();
}
- });
+
+ // assign the reference back to root
+ lookup = data;
+ }
+ });
return data;
};
-})(jQuery);
+}( jQuery ));
@@ -24,11 +24,19 @@ <h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">
<form id="test-form" name="test-form">
-
<input type="text" placeholder="Email" name="name" id="name" value="dude" />
<input type="password" placeholder="Password" name="password" id="password" value="seriously" />
<input type="submit" value="Sign In">
+ </form>
+ <form id="test-form-2">
+ <input name="text1" value="txt-one" />
+ <input type="checkbox" name="top[child][]" value="1" checked="checked" />
+ <input type="checkbox" name="top[child][]" value="2" checked="checked" />
+ <input type="checkbox" name="top[child][]" value="3" checked="checked" />
+ <select name="another[select]">
+ <option value="opt"></option>
+ </select>
</form>
</div>
</body>
@@ -42,4 +42,22 @@
deepEqual( $("#test-form").serializeObject(), looksLike, "jQuery.fn.serializeObject() only serializes elements with `name` property" );
});
+ test( "Object matches form values", function() {
+ expect(1);
+
+ var result = $( "#test-form-2" ).serializeObject();
+ var looksLike = { text1: "txt-one",
+ top: {
+ child: [ "1", "2", "3" ]
+ },
+ another: {
+ select: "opt"
+ }
+ };
+
+ deepEqual( result, looksLike, "jQuery.fn.serizlieObject() correctly generates form object" );
+ });
+
+
+
}(jQuery));

0 comments on commit 2f03335

Please sign in to comment.