|
48 | 48 | } |
49 | 49 |
|
50 | 50 | function makeObject(root, value) { |
51 | | - |
52 | | - var keys = root.match(patterns.key), k; |
| 51 | + var keys = root.match(patterns.key), k, key_pushes = ''; |
53 | 52 |
|
54 | 53 | // nest, nest, ..., nest |
55 | 54 | while ((k = keys.pop()) !== undefined) { |
56 | 55 | // foo[] |
57 | 56 | if (patterns.push.test(k)) { |
58 | | - var idx = incrementPush(root.replace(/\[\]$/, '')); |
| 57 | + var idx = incrementPush(keys.join('.'), key_pushes); |
59 | 58 | value = build([], idx, value); |
| 59 | + key_pushes += '[' + idx +']'; |
60 | 60 | } |
61 | 61 |
|
62 | 62 | // foo[n] |
63 | 63 | else if (patterns.fixed.test(k)) { |
64 | 64 | value = build([], k, value); |
| 65 | + key_pushes += '[' + k + ']'; |
65 | 66 | } |
66 | 67 |
|
67 | 68 | // foo; foo[bar] |
68 | 69 | else if (patterns.named.test(k)) { |
69 | 70 | value = build({}, k, value); |
| 71 | + key_pushes += k; |
70 | 72 | } |
71 | 73 | } |
72 | 74 |
|
73 | 75 | return value; |
74 | 76 | } |
75 | 77 |
|
76 | | - function incrementPush(key) { |
77 | | - if (pushes[key] === undefined) { |
78 | | - pushes[key] = 0; |
| 78 | + function incrementPush(path, key) { |
| 79 | + pushes[path] = pushes[path] || { |
| 80 | + size: 0, |
| 81 | + keys_set: {} |
| 82 | + }; |
| 83 | + if (pushes[path].keys_set[key]) { |
| 84 | + pushes[path].size ++; |
| 85 | + pushes[path].keys_set = {} |
| 86 | + for (var sub_path in pushes){ |
| 87 | + if (sub_path != path && 0 == sub_path.indexOf(path)){ |
| 88 | + pushes[sub_path] = undefined; |
| 89 | + } |
| 90 | + } |
79 | 91 | } |
80 | | - return pushes[key]++; |
| 92 | + pushes[path].keys_set[key] = true |
| 93 | + return pushes[path].size; |
81 | 94 | } |
82 | 95 |
|
83 | 96 | function encode(pair) { |
|
0 commit comments