diff --git a/CHANGELOG.md b/CHANGELOG.md index 02136ea1..8becad8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added `.mjs` (es modules) support. - Added `quotingType` and `forceQuotes` options for dumper to configure string literal style, #290, #529. +- Added `styles: { '!!null': 'empty' }` option for dumper + (serializes `{ foo: null }` as "`foo: `"), #570. ### Fixed - Astral characters are no longer encoded by dump/safeDump, #587. diff --git a/lib/type/null.js b/lib/type/null.js index 6874daa6..315ca4e2 100644 --- a/lib/type/null.js +++ b/lib/type/null.js @@ -28,7 +28,8 @@ module.exports = new Type('tag:yaml.org,2002:null', { canonical: function () { return '~'; }, lowercase: function () { return 'null'; }, uppercase: function () { return 'NULL'; }, - camelcase: function () { return 'Null'; } + camelcase: function () { return 'Null'; }, + empty: function () { return ''; } }, defaultStyle: 'lowercase' }); diff --git a/test/issues/0570.js b/test/issues/0570.js new file mode 100644 index 00000000..86c45125 --- /dev/null +++ b/test/issues/0570.js @@ -0,0 +1,24 @@ +'use strict'; + + +const assert = require('assert'); +const yaml = require('../../'); + + +it('should dump null in different styles', function () { + let dump, src = { foo: null, bar: 1 }; + + let tests = { + lowercase: 'null', + uppercase: 'NULL', + camelcase: 'Null', + canonical: '~', + empty: '' + }; + + for (let [ name, value ] of Object.entries(tests)) { + dump = yaml.dump(src, { styles: { '!!null': name } }); + assert.strictEqual(dump, 'foo: ' + value + '\nbar: 1\n'); + assert.deepStrictEqual(yaml.load(dump), src); + } +});