Skip to content

Commit

Permalink
fix deep array bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rhalff committed Nov 17, 2015
1 parent da9b362 commit e46da6f
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ DotObject.prototype._fill = function(a, obj, v, mod) {

if (a.length > 0) {
obj[k] = obj[k] ||
(a.length === 1 && this.useArray && isIndex(a[0]) ? [] : {});
(this.useArray && isIndex(a[0]) ? [] : {});

if (obj[k] !== Object(obj[k])) {
if (this.override) {
Expand Down
2 changes: 1 addition & 1 deletion src/dot-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ DotObject.prototype._fill = function(a, obj, v, mod) {

if (a.length > 0) {
obj[k] = obj[k] ||
(a.length === 1 && this.useArray && isIndex(a[0]) ? [] : {});
(this.useArray && isIndex(a[0]) ? [] : {});

if (obj[k] !== Object(obj[k])) {
if (this.override) {
Expand Down
30 changes: 30 additions & 0 deletions test/data/array_deep_bug.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "array deep bug #10",
"options": {
"useArray": true
},
"input": {
"a[0]": "A0",
"a[1]": "A1",
"a[2]": "A2",
"a[3].b[0].c[0]": "A3B0C0",
"a[3].b[0].c[1]": "A3B0C1"
},
"expected": {
"a": [
"A0",
"A1",
"A2",
{
"b": [
{
"c": [
"A3B0C0",
"A3B0C1"
]
}
]
}
]
}
}
19 changes: 19 additions & 0 deletions test/data/array_deep_bug2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "array deep bug2 #10",
"options": {
"useArray": true
},
"input": {
"b.0.c.1": "b0c1",
"b.0.c.2": "b0c2"
},
"expected": {
"b" : [ {
"c" : [
null,
"b0c1",
"b0c2"
]
}]
}
}
30 changes: 30 additions & 0 deletions test/data/object_deep_numeric_keys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "object deep numeric keys",
"options": {
"useArray": false
},
"input": {
"a[0]": "A0",
"a[1]": "A1",
"a[2]": "A2",
"a[3].b[0].c[0]": "A3B0C0",
"a[3].b[0].c[1]": "A3B0C1"
},
"expected": {
"a": {
"0": "A0",
"1": "A1",
"2": "A2",
"3": {
"b": {
"0": {
"c": {
"0": "A3B0C0",
"1": "A3B0C1"
}
}
}
}
}
}
}
20 changes: 20 additions & 0 deletions test/data/object_deep_numeric_keys2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "object deep numeric keys 2 #10",
"options": {
"useArray": false
},
"input": {
"b.0.c.1": "b0c1",
"b.0.c.2": "b0c2"
},
"expected": {
"b": {
"0": {
"c": {
"1": "b0c1",
"2": "b0c2"
}
}
}
}
}
31 changes: 31 additions & 0 deletions test/test_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

require('should');
var Dot = require('../index');

var testData = [
require('./data/array_deep_bug'),
require('./data/array_deep_bug2'),
require('./data/object_deep_numeric_keys'),
require('./data/object_deep_numeric_keys2')
];

describe('Test Data:', function() {

var dot = new Dot();
function testIt(test) {
it(test.name, function() {
if (test.options) {
Object.keys(test.options).forEach(function(name) {
dot[name] = test.options[name];
});
}
dot.object(test.input);
JSON.stringify(test.input).should.eql(JSON.stringify(test.expected));
});
}

// note with object() it is possible to cleanup, with del it is not.

testData.forEach(testIt);
});

0 comments on commit e46da6f

Please sign in to comment.