Skip to content

Commit

Permalink
Shorthand tags with !! whenever possible
Browse files Browse the repository at this point in the history
fix #258
  • Loading branch information
rlidwka committed Dec 28, 2020
1 parent a0d0caa commit a583097
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
`undefined` in mappings, #571.
- `dump()` with `skipInvalid=true` now serializes invalid items in collections as null.
- Custom tags starting with `!` are now dumped as `!tag` instead of `!<!tag>`, #576.
- Custom tags starting with `tag:yaml.org,2002:` are now shorthanded using `!!`, #258.

### Added
- Added `.mjs` (es modules) support.
Expand Down
2 changes: 2 additions & 0 deletions lib/dumper.js
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,8 @@ function writeNode(state, level, object, block, compact, iskey, isblockseq) {

if (state.tag[0] === '!') {
tagStr = '!' + tagStr;
} else if (tagStr.slice(0, 18) === 'tag:yaml.org,2002:') {
tagStr = '!!' + tagStr.slice(18);
} else {
tagStr = '!<' + tagStr + '>';
}
Expand Down
26 changes: 26 additions & 0 deletions test/issues/0258.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';


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


it('should shorthand tags with !! whenever possible', function () {
let regexp = new yaml.Type('tag:yaml.org,2002:js/regexp', {
kind: 'scalar',
resolve: () => true,
construct: str => new RegExp(str),
instanceOf: RegExp,
represent: object => object.source
});

let schema = yaml.DEFAULT_SCHEMA.extend(regexp);

let source = 're: !!js/regexp .*\n';

let object = yaml.load(source, { schema });
assert(object.re instanceof RegExp);

let str = yaml.dump(object, { schema });
assert.strictEqual(str, source);
});

0 comments on commit a583097

Please # to comment.