diff --git a/.bowerrc b/.bowerrc new file mode 100644 index 0000000..ffede22 --- /dev/null +++ b/.bowerrc @@ -0,0 +1,3 @@ +{ + "directory": "components" +} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 5ca0973..da032f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .DS_Store +node_modules/ +components/ diff --git a/.gitmodules b/.gitmodules index 79ddfb5..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +0,0 @@ -[submodule "tools/qunit"] - path = tools/qunit - url = https://github.com/jquery/qunit.git -[submodule "tools/jquery"] - path = tools/jquery - url = https://github.com/jquery/jquery.git diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..3258d05 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,67 @@ +{ + "bitwise" : true, + "curly" : true, + "eqeqeq" : false, + "forin" : true, + "immed" : true, + "latedef" : true, + "newcap" : true, + "noarg" : true, + "noempty" : true, + "nonew" : true, + "plusplus" : false, + "regexp" : true, + "undef" : true, + "strict" : false, + "trailing" : true, + + "asi" : false, + "boss" : false, + "debug" : false, + "eqnull" : false, + "es5" : false, + "esnext" : false, + "evil" : false, + "expr" : false, + "funcscope" : false, + "globalstrict" : false, + "iterator" : false, + "lastsemic" : false, + "laxbreak" : false, + "laxcomma" : false, + "loopfunc" : false, + "multistr" : false, + "onecase" : false, + "proto" : false, + "regexdash" : false, + "scripturl" : false, + "smarttabs" : false, + "shadow" : false, + "sub" : false, + "supernew" : false, + "validthis" : false, + + "browser" : true, + "couch" : false, + "devel" : true, + "dojo" : false, + "jquery" : true, + "mootools" : false, + "node" : true, + "nonstandard" : false, + "prototypejs" : false, + "rhino" : false, + "wsh" : false, + + "nomen" : false, + "onevar" : false, + "passfail" : false, + "white" : false, + + "maxerr" : 100, + "predef" : [ + "test", + "deepEqual" + ], + "indent" : 4 +} \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..d30ba63 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: + - 0.11 +before_script: + - npm install -g grunt-cli bower diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..2ff9623 --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1,43 @@ + +module.exports = function (grunt) { + +// Project configuration + + grunt.initConfig({ + pkg: '', + + files: { + all: ['jquery.serializeObject.js'], + tests: ['test/**/*.js'] + }, + + docs: { + all: ['README.markdown'] + }, + + uglify: { + dist: { + 'dist/jquery.serializeObject.min.js': '' + } + }, + + jshint: { + options: '', + all: '' + }, + + qunit: { + all: ['test/*.html'] + } + + }); + + grunt.loadNpmTasks('grunt-contrib-jshint'); + grunt.loadNpmTasks('grunt-contrib-uglify'); + grunt.loadNpmTasks('grunt-contrib-qunit'); + +// Default grunt task + + grunt.registerTask('default', ['jshint', 'uglify', 'qunit']); + +}; \ No newline at end of file diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..f4b72d7 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2013 David Hong + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.markdown b/README.markdown index 3fcee5d..7407517 100644 --- a/README.markdown +++ b/README.markdown @@ -1,9 +1,10 @@ +[![Build Status](https://travis-ci.org/hongymagic/jQuery.serializeObject.png)](https://travis-ci.org/hongymagic/jQuery.serializeObject) # What is it? `$.serializeObject` is a variant of existing `$.serialize` method which, instead of encoding form elements to string, converts form elements to a valid JSON -object. +object which can be used in your JavaScript application. # Why? @@ -13,29 +14,61 @@ JSON much easier to work with than DOM or string manipulation. # How do I use it? -If you want to see the code and demo first: http://jsfiddle.net/davidhong/gP9bh/ +If you want to see the code and demo first: http://jsfiddle.net/davidhong/PRpJT/ -Simple include the `jQuery.serializeObject.js` along with any `jQuery` instance +Simply include the `jQuery.serializeObject.js` along with any `jQuery` instance and use it like `$.serialize`. If you have a `form` like the following: -
- - - - ... -
+
+ + + + + + +
and wish to convert them to a JSON object: - var minutes = $('form#minutes').serializeObject(); + var minutes = $('form#minutes').serializeObject(); will return: - { - subject: '', - minuteTaker: '', - attendees: '' - } + { + "subject": "", + "minute-taker": "", + "attendees": [ + "David", + "Daniel", + "Darwin" + ] + } +## Change log + +### 2.0.3 + +- Add MIT License + +### 2.0.2 + +- Add support for $.noConflict mode + +### 2.0.0 + +*Major version change: Camel casing of names have been removed. Please use +version 1.0.4 if you require camel casing of names.* + +- Remove `$.data` like camelCasing on names + +### 1.0.4 + +- Fix an issue (#2) where arrays longer than 2 resulted in incorrect values + +## Known issues + +- In rare cases, this won't work with old checkbox/hidden hack. See [this issue for more details](https://github.com/hongymagic/jQuery.serializeObject/issues/9). + +[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/hongymagic/jquery.serializeobject/trend.png)](https://bitdeli.com/free "Bitdeli Badge") diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..2612443 --- /dev/null +++ b/bower.json @@ -0,0 +1,16 @@ +{ + "name": "jQuery.serializeObject", + "version": "2.0.3", + "main": "jquery.serializeObject.js", + "ignore": [ + "**/.*", + "node_modules", + "components" + ], + "dependencies": { + "jquery": "1.9.1" + }, + "devDependencies": { + "qunit": "~1.14.0" + } +} diff --git a/dist/jquery.serializeObject.min.js b/dist/jquery.serializeObject.min.js new file mode 100644 index 0000000..8aac8a4 --- /dev/null +++ b/dist/jquery.serializeObject.min.js @@ -0,0 +1 @@ +$.fn.serializeObject=function(){"use strict";var a={},b=function(b,c){var d=a[c.name];"undefined"!=typeof d&&d!==null?$.isArray(d)?d.push(c.value):a[c.name]=[d,c.value]:a[c.name]=c.value};return $.each(this.serializeArray(),b),a}; \ No newline at end of file diff --git a/jQuery.serializeObject.js b/jQuery.serializeObject.js deleted file mode 100644 index 6bf29ec..0000000 --- a/jQuery.serializeObject.js +++ /dev/null @@ -1,45 +0,0 @@ -// -// Use internal $.serializeArray to get list of form elements which is consistent with $.serialize -// -// And to avoid names such as -// => object["favorite-color"] -// -// We camelcase the name part, so the notation becomes -// => object["favoriteColor"] -// -// Conveniently, this allows period notation to be used. -// => object.favoriteColor -// -// This behaviour is similar to $(element).data() -// -// $('
').data() -// => { favoriteColor: 'yellow' } -// -$.fn.serializeObject = function () { - var - - result = Object.create(null), - mapper = function (element) { - element.name = $.camelCase(element.name); - return element; - }, - extend = function (i, element) { - var node = result[element.name]; - -// If node with same name exists already, need to convert it to an array as it -// is a multi-value field (i.e., checkboxes) - - if ('undefined' !== typeof node && node !== null) { - result[element.name] = node.push ? node.push(element.value) : [node, element.value]; - } else { - result[element.name] = element.value; - } - }; - -// For each serialzable element, convert element names to camelCasing and -// extend each of them to a JSON object - - $.each($.map(this.serializeArray(), mapper), extend); - return result; -}; - diff --git a/jquery.serializeObject.d.ts b/jquery.serializeObject.d.ts new file mode 100644 index 0000000..3aea456 --- /dev/null +++ b/jquery.serializeObject.d.ts @@ -0,0 +1,4 @@ + +interface JQuery { + serializeObject(): { [index: string]: string|string[] } +} diff --git a/jquery.serializeObject.js b/jquery.serializeObject.js new file mode 100644 index 0000000..f6285ac --- /dev/null +++ b/jquery.serializeObject.js @@ -0,0 +1,40 @@ +// +// Use internal $.serializeArray to get list of form elements which is +// consistent with $.serialize +// +// From version 2.0.0, $.serializeObject will stop converting [name] values +// to camelCase format. This is *consistent* with other serialize methods: +// +// - $.serialize +// - $.serializeArray +// +// If you require camel casing, you can either download version 1.0.4 or map +// them yourself. +// + +(function($){ + $.fn.serializeObject = function () { + "use strict"; + + var result = {}; + var extend = function (i, element) { + var node = result[element.name]; + + // If node with same name exists already, need to convert it to an array as it + // is a multi-value field (i.e., checkboxes) + + if ('undefined' !== typeof node && node !== null) { + if ($.isArray(node)) { + node.push(element.value); + } else { + result[element.name] = [node, element.value]; + } + } else { + result[element.name] = element.value; + } + }; + + $.each(this.serializeArray(), extend); + return result; + }; +})(jQuery); diff --git a/package.json b/package.json index 87e2df9..b07a0b7 100644 --- a/package.json +++ b/package.json @@ -1,39 +1,36 @@ { - "name": "serializeObject", - "version": "1.0.0", - "title": "jQuery.serializeObject()", - "author": { - "name": "David G. Hong", - "url": "https://github.com/hongymagic" - }, - "licenses": [ - { - "type": "MIT", - "url": "MIT-LICENSE.txt" - }, - { - "type": "GPLv2", - "url": "GPL-LICENSE.txt" - } - ], - "dependencies": { - "jquery": "1" - }, - "description": "Convert
parameters to JSON (JavaScript Object Notation) format", - "keywords": [ - "serializeObject", - "serialize", - "serialise", - "JSON" - ], - "homepage": "https://github.com/hongymagic/jQuery.serializeObject", - "maintainers": [ - { - "name": "David G. Hong", - "url": "https://github.com/hongymagic" - } - ], - "files": [ - "jQuery.serializeObject.js" - ] -} \ No newline at end of file + "name": "jQuery.serializeObject", + "version": "2.0.4", + "description": "A jQuery plugin to turn form data into JSON", + "main": "jquery.serializeObject.js", + "types": "jquery.serializeObject.d.ts", + "directories": { + "test": "test" + }, + "scripts": { + "pretest": "bower install", + "test": "grunt" + }, + "repository": { + "type": "git", + "url": "git://github.com/hongymagic/jQuery.serializeObject.git" + }, + "keywords": [ + "jquery", + "serialize", + "object", + "json", + "serializeJSON", + "serializeObject" + ], + "author": "David G. Hong", + "license": "MIT", + "gitHead": "7789084425b353624fc95487024459f364d11df6", + "readmeFilename": "README.markdown", + "devDependencies": { + "grunt": "~0.4.1", + "grunt-contrib-uglify": "~0.2.0", + "grunt-contrib-jshint": "~0.4.3", + "grunt-contrib-qunit": "~0.2.1" + } +} diff --git a/serializeObject.jquery.json b/serializeObject.jquery.json new file mode 100644 index 0000000..bbc4022 --- /dev/null +++ b/serializeObject.jquery.json @@ -0,0 +1,35 @@ +{ + "name": "serializeObject", + "version": "2.0.3", + "title": "jQuery serializeObject", + "author": { + "name": "David G. Hong", + "email": "davidhong.code@gmail.com", + "url": "https://github.com/hongymagic" + }, + "licenses": [ + { + "type": "MIT", + "url": "MIT-LICENSE.txt" + } + ], + "dependencies": { + "jquery": ">=1.4" + }, + "description": "Convert your form data to into JSON (JavaScript Object Notation) format, so you can manipulate them easily. See Github project page for information.", + "tags": [ + "form", + "JSON", + "serializeObject", + "serialize", + "serialise" + ], + "homepage": "https://github.com/hongymagic/jQuery.serializeObject", + "bugs": "https://github.com/hongymagic/jQuery.serializeObject/issues", + "maintainers": [ + { + "name": "David G. Hong", + "url": "https://github.com/hongymagic" + } + ] +} \ No newline at end of file diff --git a/test/serialization-test.js b/test/serialization-test.js index f677bec..8f43b66 100644 --- a/test/serialization-test.js +++ b/test/serialization-test.js @@ -2,16 +2,31 @@ module('serialization test'); test('simple form test', function () { - var form = $('form#simple-form'), - data = form.serializeObject(), - expected = { - name: 'John Apple', - age: '21', - email: 'john.apple@apple.com', - password: '', - legalAge: 'yes' - }; + var form = $('form#simple-form'); + var data = form.serializeObject(); + var expected = { + name: 'John Apple', + age: '21', + email: 'john.apple@apple.com', + password: '', + 'legal-age': 'yes' + }; - deepEqual(data, expected, 'Key/value pairs should be identical'); + deepEqual(data, expected, 'Key/value pairs should be identical'); }); +test('multi value inputs', function () { + var form = $('form#multi-value-form'); + var data = form.serializeObject(); + var expected = { + food: ['Banana', 'Melon'], + drink: [ + 'Water', + 'Milk', + 'Beer', + 'Cocktail' + ] + }; + + deepEqual(data, expected, 'Multiple values should be an array'); +}); diff --git a/test/tests.html b/test/tests.html index 0d6af36..efc21ff 100644 --- a/test/tests.html +++ b/test/tests.html @@ -1,36 +1,48 @@ - - - $.serializeObject tests + + + $.serializeObject tests - - - - - + + + + + - - - -

QUnit Test Suite - Step Addon

-

-
-

-
    + + + +

    QUnit Test Suite - Step Addon

    +

    +
    +

    +
      -
      - - - - - +
      + + + + + - - - -
      - + + + + +
      + + + + + + + + + +
      +
      + diff --git a/tools/jquery b/tools/jquery deleted file mode 160000 index d511613..0000000 --- a/tools/jquery +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d511613d748a92af04a3f07943f34f9baadc4153 diff --git a/tools/qunit b/tools/qunit deleted file mode 160000 index c319c35..0000000 --- a/tools/qunit +++ /dev/null @@ -1 +0,0 @@ -Subproject commit c319c356cb4942a16ea62e5b9020044e7d97c1b9