Skip to content

Commit

Permalink
Recurse for map objects
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsen1 committed Oct 2, 2015
1 parent 640b4c5 commit a66a6c0
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 75 deletions.
1 change: 0 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
"no-new-wrappers": 2, // disallows creating new instances of String,Number, and Boolean
"no-octal": 2, // disallow use of octal literals
"no-octal-escape": 2, // disallow use of octal escape sequences in string literals, such as var foo = "Copyright \251";
"no-param-reassign": 2, // disallow reassignment of function parameters (off by default)
"no-process-env": 2, // disallow use of process.env (off by default)
"no-proto": 2, // disallow usage of __proto__ property
"no-redeclare": 2, // disallow declaring the same variable more then once
Expand Down
106 changes: 67 additions & 39 deletions dist/yawn.js

Large diffs are not rendered by default.

96 changes: 63 additions & 33 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,39 +96,7 @@ export default class YAWN {
if (ast.tag === MAP_TAG) {
let json = this.json;

each(ast.value, pair => {
let [keyNode, valNode] = pair;

// node is deleted
if (isUndefined(newJson[keyNode.value])) {
this.yaml = this.yaml.substr(0, keyNode.start_mark.pointer) +
this.yaml.substring(getNodeEndMark(valNode).pointer);
return;
}

let value = json[keyNode.value];
let newValue = newJson[keyNode.value];

// only primitive value
if (newValue !== value && !isArray(valNode.value)) {
this.yaml = replacePrimitive(valNode, newValue, this.yaml);
}
});

// look for new items to add
each(newJson, (value, key)=> {

// item is new
if (isUndefined(this.json[key])) {
let newValue = cleanDump({[key]: value});
this.yaml = insertAfterNode(ast, newValue, this.yaml);
}

// item value has changed
if (!isEqual(this.json[key], newJson[key])) {
// TODO: recurse
}
});
this.yaml = updateMap(ast, newJson, json, this.yaml);
}

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -196,6 +164,68 @@ function getTag(json) {
return tag;
}

/*
* update a map structure with new values
*
* @param {any} - json
* @returns {boolean}
* @throws {YAWNError} - if json has weird type
*/
function updateMap(ast, newJson, json, yaml) {

// look for changes
each(ast.value, pair => {
let [keyNode, valNode] = pair;

// node is deleted
if (isUndefined(newJson[keyNode.value])) {

// TODO: can we use of the methods below?
yaml = yaml.substr(0, keyNode.start_mark.pointer) +
yaml.substring(getNodeEndMark(valNode).pointer);
return;
}

let value = json[keyNode.value];
let newValue = newJson[keyNode.value];

// primitive value has changed
if (newValue !== value && !isArray(valNode.value)) {

// replace the value node
yaml = replacePrimitive(valNode, newValue, yaml);

// remove the key/value from newJson so it's not detected as new pair in
// later code
delete newJson[keyNode.value];
}

// non primitive value had changed
if (!isEqual(newValue, value) && isArray(valNode.value)) {

// recurse
yaml = updateMap(valNode, newValue, valNode.value, yaml);

// remove the key/value from newJson so it's not detected as new pair in
// later code
delete newJson[keyNode.value];
}
});

// look for new items to add
each(newJson, (value, key)=> {

// item is new
if (isUndefined(json[key])) {
let newValue = cleanDump({[key]: value});

yaml = insertAfterNode(ast, newValue, yaml);
}
});

return yaml;
}

/*
* Place value in node range in yaml string
*
Expand Down
26 changes: 24 additions & 2 deletions test/deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,30 @@ describe('preserves comments and styling in objects when', ()=> {
expect(yawn.yaml).to.equal(`
# leading comment
obj:
val: 1 # inline comment
newVal: 2
val: 1
newVal: 2 # inline comment
# trailing comment`);
});

it('a primitive value is changed in third level', ()=> {

let str = `
# leading comment
obj:
deep: # inline_comment
val: 1 # inline comment
# trailing comment`;

let yawn = new YAWN(str);
let json = yawn.json;
json.obj.deep.val = 2;
yawn.json = json;

expect(yawn.yaml).to.equal(`
# leading comment
obj:
deep: # inline_comment
val: 2 # inline comment
# trailing comment`);
});
});
Expand Down

0 comments on commit a66a6c0

Please sign in to comment.