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 996dc4f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 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 `"single"` and `"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
39 changes: 28 additions & 11 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 @@ -246,6 +247,9 @@ var STYLE_PLAIN = 1,
STYLE_FOLDED = 4,
STYLE_DOUBLE = 5;

var SCALAR_QUOTE_STYLE_SINGLE = 'single',
SCALAR_QUOTE_STYLE_DOUBLE = 'double';

// Determines which scalar styles are possible and returns the preferred style.
// lineWidth = -1 => no limit.
// Pre-conditions: str.length > 0.
Expand Down Expand Up @@ -324,6 +328,9 @@ function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, te
function writeScalar(state, string, level, iskey) {
state.dump = (function () {
if (string.length === 0) {
if (state.scalarQuoteStyle === SCALAR_QUOTE_STYLE_DOUBLE) {
return '""';
}
return "''";
}
if (!state.noCompatMode &&
Expand All @@ -350,7 +357,17 @@ 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)) {
if (state.scalarQuoteStyle === SCALAR_QUOTE_STYLE_SINGLE) {
scalarStyle = STYLE_SINGLE;
} else if (state.scalarQuoteStyle === SCALAR_QUOTE_STYLE_DOUBLE) {
scalarStyle = STYLE_DOUBLE;
}
}

switch (scalarStyle) {
case STYLE_PLAIN:
return string;
case STYLE_SINGLE:
Expand Down
11 changes: 11 additions & 0 deletions test/20-dumper.js
Expand Up @@ -28,4 +28,15 @@ suite('Dumper', function () {
}
});
});

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

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

});

0 comments on commit 996dc4f

Please sign in to comment.