Repository: nodeca/js-yaml (js)

Issue:
Option to omit space between array elements
We like to encode objects and arrays to query parameters using YAML. The parser is able to parse this correctly:

![image](https://cloud.githubusercontent.com/assets/10532611/25774286/d1d5f0d8-328c-11e7-9e20-164b8bd8a18e.png)

But unfortunately, the dumper always adds unneeded spaces between array elements:

![image](https://cloud.githubusercontent.com/assets/10532611/25774341/53018a8c-328d-11e7-9469-010f01bb52ac.png)

We don't encode `[]` for prettiness and browsers can handle them fine (PHP has used them forever), but spaces of course do need to be encoded, which is not very pretty.

Here are the options I am currently using to output single line YAML:
```js
yaml.safeDump(value, { flowLevel: 0, indent: 0, lineWidth: Infinity, noCompatMode: true, sortKeys: true })
```
It would be great to have an option for omitting these non-required spaces / emitting "minimized" YAML.


Pull request description:
Add option to condense output in flow sequences
As described in #346, this is useful for using yaml as query parameters in URLs, for example.

Closes #346

Patch:
diff --git a/README.md b/README.md
index c6338fa..ccf7614 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 c04230a..6e60bbd 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.


Test patch:
diff --git a/test/issues/0346.js b/test/issues/0346.js
new file mode 100644
index 0000000..7b3ebed
--- /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'
+  );
+});