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

Add ability to override quoting style for scalar values #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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');
});

});