From c0e11b9bf7ee22b5578f0e8e5d942a36dfb1d90b Mon Sep 17 00:00:00 2001 From: Erik Seliger Date: Tue, 5 Sep 2017 20:55:19 -0700 Subject: [PATCH 1/4] Add quote keys option and fix condenseFlow for objects --- README.md | 1 + lib/js-yaml/dumper.js | 12 ++++++++++-- test/issues/0346.js | 14 ++++++++++---- test/issues/0370.js | 24 ++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 test/issues/0370.js diff --git a/README.md b/README.md index cf4831a6..9ee63539 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 `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. +- `quoteKeys` _(default: `false`)_ - if `true`, all keys will be quoted in doublequotes (`"`). Eg. `'{"a": b}'`. In combination with `condenseFlow`, this generates `'{"a":b}'`. Useful if no spaces are wanted in the dump (eg. URL). 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 6e60bbd0..eb5b84e4 100644 --- a/lib/js-yaml/dumper.js +++ b/lib/js-yaml/dumper.js @@ -115,6 +115,7 @@ function State(options) { this.noRefs = options['noRefs'] || false; this.noCompatMode = options['noCompatMode'] || false; this.condenseFlow = options['condenseFlow'] || false; + this.quoteKeys = options['quoteKeys'] || false; this.implicitTypes = this.schema.compiledImplicit; this.explicitTypes = this.schema.compiledExplicit; @@ -531,7 +532,7 @@ function writeFlowMapping(state, level, object) { pairBuffer; for (index = 0, length = objectKeyList.length; index < length; index += 1) { - pairBuffer = ''; + pairBuffer = state.quoteKeys ? '"' : ''; if (index !== 0) pairBuffer += ', '; @@ -544,7 +545,14 @@ function writeFlowMapping(state, level, object) { if (state.dump.length > 1024) pairBuffer += '? '; - pairBuffer += state.dump + ':' + (state.condenseFlow ? '' : ' '); + pairBuffer += state.dump; + if (state.quoteKeys) { + pairBuffer += '"'; + } + pairBuffer += ':'; + if (!(state.condenseFlow && state.quoteKeys)) { + pairBuffer += ' '; + } if (!writeNode(state, level, objectValue, false, false)) { continue; // Skip this pair because of invalid value. diff --git a/test/issues/0346.js b/test/issues/0346.js index 7b3ebedd..5eb1e411 100644 --- a/test/issues/0346.js +++ b/test/issues/0346.js @@ -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 emit spaces between key: value in objects in flow sequence 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); }); diff --git a/test/issues/0370.js b/test/issues/0370.js new file mode 100644 index 00000000..fe834052 --- /dev/null +++ b/test/issues/0370.js @@ -0,0 +1,24 @@ +'use strict'; + +var assert = require('assert'); +var yaml = require('../../'); + +test('should quote keys when using qouteKeys', function () { + var object = { a: { b: 'c' } }; + var objectDump = yaml.dump(object, { flowLevel: 0, indent: 0, quoteKeys: true }); + assert.equal( + objectDump, + '{"a": {"b": c}}\n' + ); + assert.deepEqual(yaml.load(objectDump), object); +}); + +test('should correctly emit objects with condenseFlow and quoteKeys set to true', function () { + var object = { a: { b: 'c' } }; + var objectDump = yaml.dump(object, { flowLevel: 0, indent: 0, condenseFlow: true, quoteKeys: true }); + assert.equal( + objectDump, + '{"a":{"b":c}}\n' + ); + assert.deepEqual(yaml.load(objectDump), object); +}); From ce787fb591244bf5b45f347d9093d2b5e6df8a86 Mon Sep 17 00:00:00 2001 From: Erik Seliger Date: Tue, 5 Sep 2017 20:59:53 -0700 Subject: [PATCH 2/4] Improve README for condenseFlow --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9ee63539..f33047c5 100644 --- a/README.md +++ b/README.md @@ -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]'`. Can be useful when using yaml for pretty URL query params as spaces are %-encoded. See also `quoteKeys`. - `quoteKeys` _(default: `false`)_ - if `true`, all keys will be quoted in doublequotes (`"`). Eg. `'{"a": b}'`. In combination with `condenseFlow`, this generates `'{"a":b}'`. Useful if no spaces are wanted in the dump (eg. URL). The following table show availlable styles (e.g. "canonical", From 4a2b61d6ca83942174e59ccc915a731358f2a80e Mon Sep 17 00:00:00 2001 From: Erik Seliger Date: Tue, 5 Sep 2017 21:22:21 -0700 Subject: [PATCH 3/4] Improve option to choose between single and double quotes --- README.md | 2 +- lib/js-yaml/dumper.js | 13 +++++++++---- test/issues/0370.js | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f33047c5..f307732d 100644 --- a/README.md +++ b/README.md @@ -183,7 +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]'`. Can be useful when using yaml for pretty URL query params as spaces are %-encoded. See also `quoteKeys`. -- `quoteKeys` _(default: `false`)_ - if `true`, all keys will be quoted in doublequotes (`"`). Eg. `'{"a": b}'`. In combination with `condenseFlow`, this generates `'{"a":b}'`. Useful if no spaces are wanted in the dump (eg. URL). +- `quoteKeys` _(default: `false`)_ - if `true`, all keys will be quoted in doublequotes (`"`). Eg. `'{"a": b}'`. Other valid options are `'` and `"` to specify the type of quotes. In combination with `condenseFlow`, this generates `'{"a":b}'`. Useful if no spaces are wanted in the dump (eg. URL). 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 eb5b84e4..e11aeb03 100644 --- a/lib/js-yaml/dumper.js +++ b/lib/js-yaml/dumper.js @@ -116,6 +116,13 @@ function State(options) { this.noCompatMode = options['noCompatMode'] || false; this.condenseFlow = options['condenseFlow'] || false; this.quoteKeys = options['quoteKeys'] || false; + if (this.quoteKeys) { + if (this.quoteKeys === true) { + this.quoteKeys = '"'; + } else if (this.quoteKeys !== '\'' && this.quoteKeys !== '"') { + this.quoteKeys = false; + } + } this.implicitTypes = this.schema.compiledImplicit; this.explicitTypes = this.schema.compiledExplicit; @@ -532,7 +539,7 @@ function writeFlowMapping(state, level, object) { pairBuffer; for (index = 0, length = objectKeyList.length; index < length; index += 1) { - pairBuffer = state.quoteKeys ? '"' : ''; + pairBuffer = state.quoteKeys || ''; if (index !== 0) pairBuffer += ', '; @@ -546,9 +553,7 @@ function writeFlowMapping(state, level, object) { if (state.dump.length > 1024) pairBuffer += '? '; pairBuffer += state.dump; - if (state.quoteKeys) { - pairBuffer += '"'; - } + pairBuffer += state.quoteKeys || ''; pairBuffer += ':'; if (!(state.condenseFlow && state.quoteKeys)) { pairBuffer += ' '; diff --git a/test/issues/0370.js b/test/issues/0370.js index fe834052..0b56552e 100644 --- a/test/issues/0370.js +++ b/test/issues/0370.js @@ -22,3 +22,23 @@ test('should correctly emit objects with condenseFlow and quoteKeys set to true' ); assert.deepEqual(yaml.load(objectDump), object); }); + +test('should correctly emit objects with quoteKeys set to "', function () { + var object = { a: { b: 'c' } }; + var objectDump = yaml.dump(object, { flowLevel: 0, indent: 0, quoteKeys: '"' }); + assert.equal( + objectDump, + '{"a": {"b": c}}\n' + ); + assert.deepEqual(yaml.load(objectDump), object); +}); + +test('should correctly emit objects with quoteKeys set to \'', function () { + var object = { a: { b: 'c' } }; + var objectDump = yaml.dump(object, { flowLevel: 0, indent: 0, quoteKeys: '\'' }); + assert.equal( + objectDump, + '{\'a\': {\'b\': c}}\n' + ); + assert.deepEqual(yaml.load(objectDump), object); +}); From 3f7c59bc2943f8d7a4394b6cf6f3a0cc08a76f98 Mon Sep 17 00:00:00 2001 From: Erik Seliger Date: Wed, 6 Sep 2017 12:03:40 -0700 Subject: [PATCH 4/4] Remove quoteKeys option --- README.md | 3 +-- lib/js-yaml/dumper.js | 17 ++--------------- test/issues/0346.js | 4 ++-- test/issues/0370.js | 44 ------------------------------------------- 4 files changed, 5 insertions(+), 63 deletions(-) delete mode 100644 test/issues/0370.js diff --git a/README.md b/README.md index f307732d..cf73ebdf 100644 --- a/README.md +++ b/README.md @@ -182,8 +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 `a, b`. Eg. `'[a,b]'`. Can be useful when using yaml for pretty URL query params as spaces are %-encoded. See also `quoteKeys`. -- `quoteKeys` _(default: `false`)_ - if `true`, all keys will be quoted in doublequotes (`"`). Eg. `'{"a": b}'`. Other valid options are `'` and `"` to specify the type of quotes. In combination with `condenseFlow`, this generates `'{"a":b}'`. Useful if no spaces are wanted in the dump (eg. URL). +- `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 diff --git a/lib/js-yaml/dumper.js b/lib/js-yaml/dumper.js index e11aeb03..66a45687 100644 --- a/lib/js-yaml/dumper.js +++ b/lib/js-yaml/dumper.js @@ -115,14 +115,6 @@ function State(options) { this.noRefs = options['noRefs'] || false; this.noCompatMode = options['noCompatMode'] || false; this.condenseFlow = options['condenseFlow'] || false; - this.quoteKeys = options['quoteKeys'] || false; - if (this.quoteKeys) { - if (this.quoteKeys === true) { - this.quoteKeys = '"'; - } else if (this.quoteKeys !== '\'' && this.quoteKeys !== '"') { - this.quoteKeys = false; - } - } this.implicitTypes = this.schema.compiledImplicit; this.explicitTypes = this.schema.compiledExplicit; @@ -539,7 +531,7 @@ function writeFlowMapping(state, level, object) { pairBuffer; for (index = 0, length = objectKeyList.length; index < length; index += 1) { - pairBuffer = state.quoteKeys || ''; + pairBuffer = state.condenseFlow ? '"' : ''; if (index !== 0) pairBuffer += ', '; @@ -552,12 +544,7 @@ function writeFlowMapping(state, level, object) { if (state.dump.length > 1024) pairBuffer += '? '; - pairBuffer += state.dump; - pairBuffer += state.quoteKeys || ''; - pairBuffer += ':'; - if (!(state.condenseFlow && state.quoteKeys)) { - pairBuffer += ' '; - } + pairBuffer += state.dump + (state.condenseFlow ? '"' : '') + ':' + (state.condenseFlow ? '' : ' '); if (!writeNode(state, level, objectValue, false, false)) { continue; // Skip this pair because of invalid value. diff --git a/test/issues/0346.js b/test/issues/0346.js index 5eb1e411..ccd75d5b 100644 --- a/test/issues/0346.js +++ b/test/issues/0346.js @@ -14,12 +14,12 @@ test('should not emit spaces in arrays in flow mode between entries using conden assert.deepEqual(yaml.load(dumpedArray), array); }); -test('should 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( objectDump, - '{a: {b: c}}\n' + '{"a":{"b":c}}\n' ); assert.deepEqual(yaml.load(objectDump), object); }); diff --git a/test/issues/0370.js b/test/issues/0370.js deleted file mode 100644 index 0b56552e..00000000 --- a/test/issues/0370.js +++ /dev/null @@ -1,44 +0,0 @@ -'use strict'; - -var assert = require('assert'); -var yaml = require('../../'); - -test('should quote keys when using qouteKeys', function () { - var object = { a: { b: 'c' } }; - var objectDump = yaml.dump(object, { flowLevel: 0, indent: 0, quoteKeys: true }); - assert.equal( - objectDump, - '{"a": {"b": c}}\n' - ); - assert.deepEqual(yaml.load(objectDump), object); -}); - -test('should correctly emit objects with condenseFlow and quoteKeys set to true', function () { - var object = { a: { b: 'c' } }; - var objectDump = yaml.dump(object, { flowLevel: 0, indent: 0, condenseFlow: true, quoteKeys: true }); - assert.equal( - objectDump, - '{"a":{"b":c}}\n' - ); - assert.deepEqual(yaml.load(objectDump), object); -}); - -test('should correctly emit objects with quoteKeys set to "', function () { - var object = { a: { b: 'c' } }; - var objectDump = yaml.dump(object, { flowLevel: 0, indent: 0, quoteKeys: '"' }); - assert.equal( - objectDump, - '{"a": {"b": c}}\n' - ); - assert.deepEqual(yaml.load(objectDump), object); -}); - -test('should correctly emit objects with quoteKeys set to \'', function () { - var object = { a: { b: 'c' } }; - var objectDump = yaml.dump(object, { flowLevel: 0, indent: 0, quoteKeys: '\'' }); - assert.equal( - objectDump, - '{\'a\': {\'b\': c}}\n' - ); - assert.deepEqual(yaml.load(objectDump), object); -});