Skip to content

Commit

Permalink
Add condenseFlow option, close #346
Browse files Browse the repository at this point in the history
  • Loading branch information
eseliger authored and Vitaly Puzrin committed Jul 7, 2017
1 parent 3de650d commit bd32506
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -182,6 +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.

The following table show availlable styles (e.g. "canonical",
"binary"...) available for each tag (.e.g. !!null, !!int ...). Yaml
Expand Down
5 changes: 3 additions & 2 deletions lib/js-yaml/dumper.js
Expand Up @@ -114,6 +114,7 @@ function State(options) {
this.lineWidth = options['lineWidth'] || 80;
this.noRefs = options['noRefs'] || false;
this.noCompatMode = options['noCompatMode'] || false;
this.condenseFlow = options['condenseFlow'] || false;

this.implicitTypes = this.schema.compiledImplicit;
this.explicitTypes = this.schema.compiledExplicit;
Expand Down Expand Up @@ -483,7 +484,7 @@ function writeFlowSequence(state, level, object) {
for (index = 0, length = object.length; index < length; index += 1) {
// Write only valid elements.
if (writeNode(state, level, object[index], false, false)) {
if (index !== 0) _result += ', ';
if (index !== 0) _result += ',' + (!state.condenseFlow ? ' ' : '');
_result += state.dump;
}
}
Expand Down Expand Up @@ -543,7 +544,7 @@ function writeFlowMapping(state, level, object) {

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

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

if (!writeNode(state, level, objectValue, false, false)) {
continue; // Skip this pair because of invalid value.
Expand Down
19 changes: 19 additions & 0 deletions test/issues/0346.js
@@ -0,0 +1,19 @@
'use strict';

var assert = require('assert');
var yaml = require('../../');


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

test('should not emit spaces between key: value in objects in flow sequence using condenseFlow: true', function () {
assert.equal(
yaml.dump({ a: { b: 'c' } }, { flowLevel: 0, indent: 0, condenseFlow: true }),
'{a:{b:c}}\n'
);
});

0 comments on commit bd32506

Please sign in to comment.