diff --git a/README.md b/README.md index c6338fac..ccf76140 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/js-yaml/dumper.js b/lib/js-yaml/dumper.js index c04230ad..6e60bbd0 100644 --- a/lib/js-yaml/dumper.js +++ b/lib/js-yaml/dumper.js @@ -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; @@ -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; } } @@ -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. diff --git a/test/issues/0346.js b/test/issues/0346.js new file mode 100644 index 00000000..7b3ebedd --- /dev/null +++ b/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' + ); +});