Skip to content

Commit

Permalink
Merge pull request #343 from onebytegone/fix_342_array_eol_space
Browse files Browse the repository at this point in the history
[writer] Prevent space after dash for arrays that wrap
  • Loading branch information
Vitaly Puzrin committed May 7, 2017
2 parents db20313 + 66e07b2 commit 9e26400
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/js-yaml/dumper.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,14 @@ function writeBlockSequence(state, level, object, compact) {
if (!compact || index !== 0) {
_result += generateNextLine(state, level);
}
_result += '- ' + state.dump;

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

_result += state.dump;
}
}

Expand Down
61 changes: 61 additions & 0 deletions test/issues/0342.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';


var assert = require('assert');
var yaml = require('../../');
var simpleArray = [ 'a', 'b' ];
var arrayOfSimpleObj = [ { a: 1 }, { b: 2 } ];
var arrayOfObj = [ { a: 1, b: 'abc' }, { c: 'def', d: 2 } ];


test('space should be added for array, regardless of indent', function () {
assert.deepEqual(
yaml.dump(simpleArray, { indent: 1 }),
'- a\n- b\n'
);
assert.deepEqual(
yaml.dump(simpleArray, { indent: 2 }),
'- a\n- b\n'
);
assert.deepEqual(
yaml.dump(simpleArray, { indent: 3 }),
'- a\n- b\n'
);
assert.deepEqual(
yaml.dump(simpleArray, { indent: 4 }),
'- a\n- b\n'
);
});

test('array of objects should not wrap at indentation of 2', function () {
assert.deepEqual(
yaml.dump(arrayOfSimpleObj, { indent: 2 }),
'- a: 1\n- b: 2\n'
);
assert.deepEqual(
yaml.dump(arrayOfObj, { indent: 2 }),
'- a: 1\n b: abc\n- c: def\n d: 2\n'
);
});

test('EOL space should not be added on array of objects at indentation of 3', function () {
assert.deepEqual(
yaml.dump(arrayOfSimpleObj, { indent: 3 }),
'-\n a: 1\n-\n b: 2\n'
);
assert.deepEqual(
yaml.dump(arrayOfObj, { indent: 3 }),
'-\n a: 1\n b: abc\n-\n c: def\n d: 2\n'
);
});

test('EOL space should not be added on array of objects at indentation of 4', function () {
assert.deepEqual(
yaml.dump(arrayOfSimpleObj, { indent: 4 }),
'-\n a: 1\n-\n b: 2\n'
);
assert.deepEqual(
yaml.dump(arrayOfObj, { indent: 4 }),
'-\n a: 1\n b: abc\n-\n c: def\n d: 2\n'
);
});

0 comments on commit 9e26400

Please sign in to comment.