Repository: nodeca/js-yaml (js)

Issue:
condenseFlow results in invalid object keys
```js
jsYaml.safeDump({key: 'value'}, {
    flowLevel: 0,
    indent: 0,
    lineWidth: Infinity,
    noCompatMode: true,
    sortKeys: true,
    noRefs: true,
    condenseFlow: true
})
```
results in actual YAML:
```yaml
{key:value}
```
but parsing that with `safeLoad()` results in
```js
{ 'key:value': null }
```

Expected YAML:
```yaml
{"key":value}
# or
{key: value}
```
which results in
```js
{ key: 'value' }
```

it would be best if the quoting of the key would be controlled by a `quoteKeys` setting.

cc @eseliger 


Pull request description:
Fix condenseFlow for objects
Closes #370 

Patch:
diff --git a/README.md b/README.md
index cf4831a..cf73ebd 100644
--- a/README.md
+++ b/README.md
@@ -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
diff --git a/lib/js-yaml/dumper.js b/lib/js-yaml/dumper.js
index 6e60bbd..66a4568 100644
--- a/lib/js-yaml/dumper.js
+++ b/lib/js-yaml/dumper.js
@@ -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 += ', ';
 
@@ -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.


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