Skip to content

Commit

Permalink
Add dumper noCompatMode option which bypass DEPRECATED_BOOLEANS_SYNTA…
Browse files Browse the repository at this point in the history
…X handling
  • Loading branch information
jeromew committed Mar 9, 2016
1 parent 4a9e0a2 commit 3912658
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ options:
function, use the function to sort the keys.
- `lineWidth` _(default: `80`)_ - set max line width.
- `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

styles:

Expand Down
20 changes: 11 additions & 9 deletions lib/js-yaml/dumper.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,15 @@ 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.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.implicitTypes = this.schema.compiledImplicit;
this.explicitTypes = this.schema.compiledExplicit;
Expand Down Expand Up @@ -219,7 +220,8 @@ function writeScalar(state, object, level, iskey) {
return;
}

if (DEPRECATED_BOOLEANS_SYNTAX.indexOf(object) !== -1) {
if (!state.noCompatMode &&
DEPRECATED_BOOLEANS_SYNTAX.indexOf(object) !== -1) {
state.dump = "'" + object + "'";
return;
}
Expand Down
1 change: 1 addition & 0 deletions test/issues/0085.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ test('Dumper should take into account booleans syntax from YAML 1.0/1.1', functi
('"' + string + '" string is dumped without quoting; actual dump: ' + dump));
});
});

21 changes: 21 additions & 0 deletions test/issues/0266.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';


var assert = require('assert');
var yaml = require('../../');


var DEPRECATED_BOOLEANS_SYNTAX = [
'y', 'Y', 'yes', 'Yes', 'YES', 'on', 'On', 'ON',
'n', 'N', 'no', 'No', 'NO', 'off', 'Off', 'OFF'
];


test('Dumper should not take into account booleans syntax from YAML 1.0/1.1 in noCompatMode', function () {
DEPRECATED_BOOLEANS_SYNTAX.forEach(function (string) {
var dump = yaml.dump(string, { noCompatMode: true }).trim();

assert((dump === string),
('"' + string + '" string is not dumped as-is; actual dump: ' + dump));
});
});

0 comments on commit 3912658

Please sign in to comment.