Skip to content

Commit

Permalink
[dumper] Prevent adding unnecessary spaces.
Browse files Browse the repository at this point in the history
Related to issue nodeca#68.
dervus committed Dec 22, 2013
1 parent a7f330e commit ea3d954
Showing 2 changed files with 24 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/js-yaml/dumper.js
Original file line number Diff line number Diff line change
@@ -337,7 +337,11 @@ function writeBlockMapping(state, level, object, compact) {
(state.dump && state.dump.length > 1024);

if (explicitPair) {
pairBuffer += '? ';
if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) {
pairBuffer += '?';
} else {
pairBuffer += '? ';
}
}

pairBuffer += state.dump;
@@ -350,7 +354,13 @@ function writeBlockMapping(state, level, object, compact) {
continue; // Skip this pair because of invalid value.
}

pairBuffer += ': ' + state.dump;
if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) {
pairBuffer += ':';
} else {
pairBuffer += ': ';
}

pairBuffer += state.dump;

// Both key and value are valid.
_result += pairBuffer;
12 changes: 12 additions & 0 deletions test/issues/issue-68.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
/*global it */


var assert = require('assert');
var yaml = require('../../lib/js-yaml');


it('Prevent adding unnecessary space character to end of a line within block collections', function () {
assert.strictEqual(yaml.dump({ data: [ 'foo', 'bar', 'baz' ] }), 'data:\n - foo\n - bar\n - baz\n');
assert.strictEqual(yaml.dump({ foo: { bar: [ 'baz' ] } }), 'foo:\n bar:\n - baz\n');
});

0 comments on commit ea3d954

Please sign in to comment.