Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix condenseFlow for objects #371

Merged
merged 4 commits into from Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});