Skip to content

Commit e46da6f

Browse files
committed
fix deep array bug
1 parent da9b362 commit e46da6f

File tree

7 files changed

+132
-2
lines changed

7 files changed

+132
-2
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ DotObject.prototype._fill = function(a, obj, v, mod) {
7171

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

7676
if (obj[k] !== Object(obj[k])) {
7777
if (this.override) {

src/dot-object.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ DotObject.prototype._fill = function(a, obj, v, mod) {
7171

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

7676
if (obj[k] !== Object(obj[k])) {
7777
if (this.override) {

test/data/array_deep_bug.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "array deep bug #10",
3+
"options": {
4+
"useArray": true
5+
},
6+
"input": {
7+
"a[0]": "A0",
8+
"a[1]": "A1",
9+
"a[2]": "A2",
10+
"a[3].b[0].c[0]": "A3B0C0",
11+
"a[3].b[0].c[1]": "A3B0C1"
12+
},
13+
"expected": {
14+
"a": [
15+
"A0",
16+
"A1",
17+
"A2",
18+
{
19+
"b": [
20+
{
21+
"c": [
22+
"A3B0C0",
23+
"A3B0C1"
24+
]
25+
}
26+
]
27+
}
28+
]
29+
}
30+
}

test/data/array_deep_bug2.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "array deep bug2 #10",
3+
"options": {
4+
"useArray": true
5+
},
6+
"input": {
7+
"b.0.c.1": "b0c1",
8+
"b.0.c.2": "b0c2"
9+
},
10+
"expected": {
11+
"b" : [ {
12+
"c" : [
13+
null,
14+
"b0c1",
15+
"b0c2"
16+
]
17+
}]
18+
}
19+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "object deep numeric keys",
3+
"options": {
4+
"useArray": false
5+
},
6+
"input": {
7+
"a[0]": "A0",
8+
"a[1]": "A1",
9+
"a[2]": "A2",
10+
"a[3].b[0].c[0]": "A3B0C0",
11+
"a[3].b[0].c[1]": "A3B0C1"
12+
},
13+
"expected": {
14+
"a": {
15+
"0": "A0",
16+
"1": "A1",
17+
"2": "A2",
18+
"3": {
19+
"b": {
20+
"0": {
21+
"c": {
22+
"0": "A3B0C0",
23+
"1": "A3B0C1"
24+
}
25+
}
26+
}
27+
}
28+
}
29+
}
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "object deep numeric keys 2 #10",
3+
"options": {
4+
"useArray": false
5+
},
6+
"input": {
7+
"b.0.c.1": "b0c1",
8+
"b.0.c.2": "b0c2"
9+
},
10+
"expected": {
11+
"b": {
12+
"0": {
13+
"c": {
14+
"1": "b0c1",
15+
"2": "b0c2"
16+
}
17+
}
18+
}
19+
}
20+
}

test/test_data.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
require('should');
4+
var Dot = require('../index');
5+
6+
var testData = [
7+
require('./data/array_deep_bug'),
8+
require('./data/array_deep_bug2'),
9+
require('./data/object_deep_numeric_keys'),
10+
require('./data/object_deep_numeric_keys2')
11+
];
12+
13+
describe('Test Data:', function() {
14+
15+
var dot = new Dot();
16+
function testIt(test) {
17+
it(test.name, function() {
18+
if (test.options) {
19+
Object.keys(test.options).forEach(function(name) {
20+
dot[name] = test.options[name];
21+
});
22+
}
23+
dot.object(test.input);
24+
JSON.stringify(test.input).should.eql(JSON.stringify(test.expected));
25+
});
26+
}
27+
28+
// note with object() it is possible to cleanup, with del it is not.
29+
30+
testData.forEach(testIt);
31+
});

0 commit comments

Comments
 (0)