diff --git a/src/index.js b/src/index.js index fd081b7..8e307c4 100644 --- a/src/index.js +++ b/src/index.js @@ -293,7 +293,7 @@ function updateMap(ast, newJson, json, yaml, offset) { */ function replacePrimitive(node, value, yaml, offset) { return yaml.substr(0, node.start_mark.pointer + offset) + - String(value) + + JSON.stringify(value) + yaml.substring(node.end_mark.pointer + offset); } @@ -417,6 +417,10 @@ function indent(str, depth) { * */ function cleanDump(value) { + if (value === null || isString(value) || isNumber(value)) { + return value; + } + let yaml = dump(value).replace(/\n$/, ''); if (EOL !== '\n') { diff --git a/test/object.js b/test/object.js index 9b12c99..704658d 100644 --- a/test/object.js +++ b/test/object.js @@ -210,7 +210,7 @@ describe('preserves comments and styling in objects when', ()=> { expect(yawn.yaml).to.equal(` # leading comment a: - x: abc + x: "abc" b: y: 'b' z: abc diff --git a/test/string.js b/test/string.js index 229ec82..f81a608 100644 --- a/test/string.js +++ b/test/string.js @@ -18,7 +18,7 @@ describe('preserves comments and styling when', ()=> { expect(yawn.yaml).to.equal(` # leading comment - newValue # inline comment + "newValue" # inline comment # trailing comment`); }); @@ -36,5 +36,20 @@ describe('preserves comments and styling when', ()=> { null # inline comment # trailing comment`); }); + + it('string numbers stay as numbers', ()=> { + let str = ` + # leading comment + foo: "1000" # inline comment + # trailing comment`; + + let yawn = new YAWN(str); + yawn.json = { foo: "1001" }; + + expect(yawn.yaml).to.equal(` + # leading comment + foo: "1001" # inline comment + # trailing comment`); + }); }); });