Skip to content

Commit

Permalink
Fix condenseFlow for objects (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
eseliger authored and Vitaly Puzrin committed Sep 11, 2017
1 parent 685e6fb commit 0ec1037
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -182,7 +182,7 @@ options:
- `noRefs` _(default: `false`)_ - if `true`, don't convert duplicate objects into references
- `noCompatMode` _(default: `false`)_ - if `true` don't try to be compatible with older
yaml versions. Currently: don't quote "yes", "no" and so on, as required for YAML 1.1
- `condenseFlow` _(default: `false`)_ - if `true` flow sequences will be condensed, omitting the space between `key: value` or `a, b`. Eg. `'[a,b]'` or `{a:{b:c}}`. Can be useful when using yaml for pretty URL query params as spaces are %-encoded.
- `condenseFlow` _(default: `false`)_ - if `true` flow sequences will be condensed, omitting the space between `a, b`. Eg. `'[a,b]'`, and omitting the space between `key: value` and quoting the key. Eg. `'{"a":b}'` Can be useful when using yaml for pretty URL query params as spaces are %-encoded.

The following table show availlable styles (e.g. "canonical",
"binary"...) available for each tag (.e.g. !!null, !!int ...). Yaml
Expand Down
4 changes: 2 additions & 2 deletions lib/js-yaml/dumper.js
Expand Up @@ -531,7 +531,7 @@ function writeFlowMapping(state, level, object) {
pairBuffer;

for (index = 0, length = objectKeyList.length; index < length; index += 1) {
pairBuffer = '';
pairBuffer = state.condenseFlow ? '"' : '';

if (index !== 0) pairBuffer += ', ';

Expand All @@ -544,7 +544,7 @@ function writeFlowMapping(state, level, object) {

if (state.dump.length > 1024) pairBuffer += '? ';

pairBuffer += state.dump + ':' + (state.condenseFlow ? '' : ' ');
pairBuffer += state.dump + (state.condenseFlow ? '"' : '') + ':' + (state.condenseFlow ? '' : ' ');

if (!writeNode(state, level, objectValue, false, false)) {
continue; // Skip this pair because of invalid value.
Expand Down
14 changes: 10 additions & 4 deletions test/issues/0346.js
Expand Up @@ -5,15 +5,21 @@ var yaml = require('../../');


test('should not emit spaces in arrays in flow mode between entries using condenseFlow: true', function () {
var array = [ 'a', 'b' ];
var dumpedArray = yaml.dump(array, { flowLevel: 0, indent: 0, condenseFlow: true });
assert.equal(
yaml.dump([ 'a', 'b' ], { flowLevel: 0, indent: 0, condenseFlow: true }),
dumpedArray,
'[a,b]\n'
);
assert.deepEqual(yaml.load(dumpedArray), array);
});

test('should not emit spaces between key: value in objects in flow sequence using condenseFlow: true', function () {
test('should not emit spaces between key: value and quote keys using condenseFlow: true', function () {
var object = { a: { b: 'c' } };
var objectDump = yaml.dump(object, { flowLevel: 0, indent: 0, condenseFlow: true });
assert.equal(
yaml.dump({ a: { b: 'c' } }, { flowLevel: 0, indent: 0, condenseFlow: true }),
'{a:{b:c}}\n'
objectDump,
'{"a":{"b":c}}\n'
);
assert.deepEqual(yaml.load(objectDump), object);
});

0 comments on commit 0ec1037

Please sign in to comment.