Skip to content

Commit

Permalink
Add ability to override quoting style for scalar values
Browse files Browse the repository at this point in the history
  • Loading branch information
jpb committed Oct 29, 2018
1 parent 2d1fbed commit 7dd8183
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -183,6 +183,7 @@ options:
- `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 `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.
- `scalarQuoteStyle` _(default: `null`)_ set the quote style for scalar values. Valid values are `dumper.STYLE_SINGLE` and `dumper.STYLE_DOUBLE`. Double quoted values will not be changed to single quotes.

The following table show availlable styles (e.g. "canonical",
"binary"...) available for each tag (.e.g. !!null, !!int ...). Yaml
Expand Down
40 changes: 27 additions & 13 deletions lib/js-yaml/dumper.js
Expand Up @@ -105,16 +105,17 @@ function encodeHex(character) {
}

function State(options) {
this.schema = options['schema'] || DEFAULT_FULL_SCHEMA;
this.indent = Math.max(1, (options['indent'] || 2));
this.skipInvalid = options['skipInvalid'] || false;
this.flowLevel = (common.isNothing(options['flowLevel']) ? -1 : options['flowLevel']);
this.styleMap = compileStyleMap(this.schema, options['styles'] || null);
this.sortKeys = options['sortKeys'] || false;
this.lineWidth = options['lineWidth'] || 80;
this.noRefs = options['noRefs'] || false;
this.noCompatMode = options['noCompatMode'] || false;
this.condenseFlow = options['condenseFlow'] || false;
this.schema = options['schema'] || DEFAULT_FULL_SCHEMA;
this.indent = Math.max(1, (options['indent'] || 2));
this.skipInvalid = options['skipInvalid'] || false;
this.flowLevel = (common.isNothing(options['flowLevel']) ? -1 : options['flowLevel']);
this.styleMap = compileStyleMap(this.schema, options['styles'] || null);
this.sortKeys = options['sortKeys'] || false;
this.lineWidth = options['lineWidth'] || 80;
this.noRefs = options['noRefs'] || false;
this.noCompatMode = options['noCompatMode'] || false;
this.condenseFlow = options['condenseFlow'] || false;
this.scalarQuoteStyle = options['scalarQuoteStyle'] || null;

this.implicitTypes = this.schema.compiledImplicit;
this.explicitTypes = this.schema.compiledExplicit;
Expand Down Expand Up @@ -324,6 +325,9 @@ function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, te
function writeScalar(state, string, level, iskey) {
state.dump = (function () {
if (string.length === 0) {
if (state.scalarQuoteStyle === STYLE_DOUBLE) {
return '""';
}
return "''";
}
if (!state.noCompatMode &&
Expand All @@ -350,7 +354,15 @@ function writeScalar(state, string, level, iskey) {
return testImplicitResolving(state, string);
}

switch (chooseScalarStyle(string, singleLineOnly, state.indent, lineWidth, testAmbiguity)) {
var scalarStyle = chooseScalarStyle(string, singleLineOnly, state.indent, lineWidth, testAmbiguity);

if (iskey !== true &&
(scalarStyle === STYLE_PLAIN || scalarStyle === STYLE_SINGLE) &&
(state.scalarQuoteStyle === STYLE_SINGLE || state.scalarQuoteStyle === STYLE_DOUBLE)) {
scalarStyle = state.scalarQuoteStyle;
}

switch (scalarStyle) {
case STYLE_PLAIN:
return string;
case STYLE_SINGLE:
Expand Down Expand Up @@ -821,5 +833,7 @@ function safeDump(input, options) {
return dump(input, common.extend({ schema: DEFAULT_SAFE_SCHEMA }, options));
}

module.exports.dump = dump;
module.exports.safeDump = safeDump;
module.exports.dump = dump;
module.exports.safeDump = safeDump;
module.exports.STYLE_SINGLE = STYLE_SINGLE;
module.exports.STYLE_DOUBLE = STYLE_DOUBLE;
12 changes: 12 additions & 0 deletions test/20-dumper.js
Expand Up @@ -5,6 +5,7 @@ var assert = require('assert');
var path = require('path');
var fs = require('fs');
var yaml = require('../');
var dumper = require('../lib/js-yaml/dumper');

var TEST_SCHEMA = require('./support/schema').TEST_SCHEMA;

Expand All @@ -28,4 +29,15 @@ suite('Dumper', function () {
}
});
});

test('single quote scalars', function () {
var serialized = yaml.dump({ k: 'v' }, { schema: TEST_SCHEMA, scalarQuoteStyle: dumper.STYLE_SINGLE });
assert.equal(serialized, 'k: \'v\'\n');
});

test('double quote scalars', function () {
var serialized = yaml.dump({ k: 'v' }, { schema: TEST_SCHEMA, scalarQuoteStyle: dumper.STYLE_DOUBLE });
assert.equal(serialized, 'k: "v"\n');
});

});

0 comments on commit 7dd8183

Please sign in to comment.