diff --git a/README.md b/README.md index c165c357..f42a97ac 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/js-yaml/dumper.js b/lib/js-yaml/dumper.js index 67cf6ca0..f73adacb 100644 --- a/lib/js-yaml/dumper.js +++ b/lib/js-yaml/dumper.js @@ -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; @@ -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 && @@ -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: @@ -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; diff --git a/test/20-dumper.js b/test/20-dumper.js index 9ae3b3b0..3c268327 100644 --- a/test/20-dumper.js +++ b/test/20-dumper.js @@ -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; @@ -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'); + }); + });