Permalink
Browse files

computing paths seemingly correctly

  • Loading branch information...
1 parent f498692 commit 522859ef87f9f6963c442ea439482a72b3ff067e @substack committed Jul 19, 2011
Showing with 29 additions and 8 deletions.
  1. +28 −7 index.js
  2. +1 −1 stub.json
View
@@ -2,23 +2,44 @@ var burrito = require('burrito');
module.exports = function (src) {
var self = {};
- var nodes = [];
+ var obj = JSON.parse(src);
+
+ var cursor = [ obj ];
+ var root = [ Object.create(Object.getPrototypeOf(obj)) ];
+
+ var path = [];
+ var prevPathLen = 0;
burrito('[\n' + src + '\n][0]', function (node) {
+ if (this.path.length <= prevPathLen) {
+ path.pop();
+ prevPathLen = path.length;
+ }
+
var p = node.parent();
- if (p && p.name === 'object') {
+ var key = undefined;
+
+ if (!p) return
+ else if (p.name === 'object') {
var ix = this.path[ this.path.length - 2 ];
- var key = p.value[0][ix][0];
- console.log(key);
+ key = p.value[0][ix][0];
+ }
+ else if (p.name === 'array') {
+ key = this.key;
}
- else if (p && p.name === 'array') {
+ else if (p.name === 'sub' && node.value[0] !== 0) {
+ //console.dir(node.value);
}
+ else return;
- nodes.push(node);
+ if (node.name === 'object' || node.name === 'array') {
+ prevPathLen = this.path.length;
+ path.push(key);
+ }
+ console.log(path.join('/'));
});
self.set = function (key, value) {
-
};
self.toString = self.stringify = function () {
View
@@ -1,4 +1,4 @@
{
"a" : [1, 2],
- "b" : [ 3, 4 ]
+ "b" : [ 3, 4, { "c" : [ 5, 6 ] } ]
}

0 comments on commit 522859e

Please sign in to comment.