diff --git a/index.js b/index.js index dcda631..85c616b 100644 --- a/index.js +++ b/index.js @@ -197,7 +197,10 @@ function hash_assign(result, keys, value) { // If the characters between the brackets is not a number it is an // attribute name and can be assigned directly. - if (isNaN(index)) { + // + // If we process the first record and it is non-zero we assume it's a sparse array + // and use a hash instead of an array to avoid very large sparse arrays + if (isNaN(index) || (result === undefined && index !== 0)) { result = result || {}; result[string] = hash_assign(result[string], keys, value); } diff --git a/package.json b/package.json index 90361c0..dfb366d 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "test": "test" }, "devDependencies": { + "core-assert": "~0.1.3", "domify": "~1.4.0", "zuul": "~3.10.1" }, @@ -27,4 +28,4 @@ "bugs": { "url": "https://github.com/shtylman/form-serialize/issues" } -} \ No newline at end of file +} diff --git a/test/index.js b/test/index.js index ee2a60a..290087a 100644 --- a/test/index.js +++ b/test/index.js @@ -1,10 +1,10 @@ -var assert = require('assert'); +var assert = require('core-assert'); var domify = require('domify'); var serialize = require('../'); var hash_check = function(form, exp) { - assert.deepEqual(serialize(form, { hash: true }), exp); + assert.deepStrictEqual(serialize(form, { hash: true }), exp); }; var str_check = function(form, exp) { @@ -12,15 +12,15 @@ var str_check = function(form, exp) { }; var disabled_check = function(form, exp) { - assert.deepEqual(serialize(form, { hash : false, disabled: true }), exp); + assert.deepStrictEqual(serialize(form, { hash : false, disabled: true }), exp); }; var empty_check = function(form, exp) { - assert.deepEqual(serialize(form, { hash : false, disabled: true, empty: true }), exp); + assert.deepStrictEqual(serialize(form, { hash : false, disabled: true, empty: true }), exp); }; var empty_check_hash = function(form, exp) { - assert.deepEqual(serialize(form, { hash : true, disabled: true, empty: true }), exp); + assert.deepStrictEqual(serialize(form, { hash : true, disabled: true, empty: true }), exp); }; test('null form', function() { @@ -426,6 +426,20 @@ test('bracket notation - non-indexed arrays', function() { }); }); +test('bracket notation - sparse array', function() { + var form = domify('
' + + '' + + '' + + '
'); + + hash_check(form, { + user: { + 1234: "cow", + 4567: "milk", + } + }); +}); + test('bracket notation - nested, non-indexed arrays', function() { var form = domify('
' + '' + @@ -452,19 +466,49 @@ test('bracket notation - indexed arrays', function() { '
'); hash_check(form, { - people: [ - { + people: { + 0: { name: "fred", age: "12" }, - { + 1: { name: "bob", age: "14" }, - { + 2: { name: "bubba", age: "15" }, + 3: { + age: "2" + }, + _values: [ + {name: "frank"} + ] + } + }); +}); + +test('bracket notation - ordered indexed arrays', function() { + var form = domify('
' + + '' + + '' + + '' + + '' + + '' + + '' + + '
'); + + hash_check(form, { + people: [ + { + name: "fred", + age: "12" + }, + { + name: "bob", + age: "14" + }, { name: "frank", age: "2"