Skip to content

Commit a583097

Browse files
committed
Shorthand tags with !! whenever possible
fix #258
1 parent a0d0caa commit a583097

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
`undefined` in mappings, #571.
2828
- `dump()` with `skipInvalid=true` now serializes invalid items in collections as null.
2929
- Custom tags starting with `!` are now dumped as `!tag` instead of `!<!tag>`, #576.
30+
- Custom tags starting with `tag:yaml.org,2002:` are now shorthanded using `!!`, #258.
3031

3132
### Added
3233
- Added `.mjs` (es modules) support.

lib/dumper.js

+2
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,8 @@ function writeNode(state, level, object, block, compact, iskey, isblockseq) {
888888

889889
if (state.tag[0] === '!') {
890890
tagStr = '!' + tagStr;
891+
} else if (tagStr.slice(0, 18) === 'tag:yaml.org,2002:') {
892+
tagStr = '!!' + tagStr.slice(18);
891893
} else {
892894
tagStr = '!<' + tagStr + '>';
893895
}

test/issues/0258.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
4+
const assert = require('assert');
5+
const yaml = require('../../');
6+
7+
8+
it('should shorthand tags with !! whenever possible', function () {
9+
let regexp = new yaml.Type('tag:yaml.org,2002:js/regexp', {
10+
kind: 'scalar',
11+
resolve: () => true,
12+
construct: str => new RegExp(str),
13+
instanceOf: RegExp,
14+
represent: object => object.source
15+
});
16+
17+
let schema = yaml.DEFAULT_SCHEMA.extend(regexp);
18+
19+
let source = 're: !!js/regexp .*\n';
20+
21+
let object = yaml.load(source, { schema });
22+
assert(object.re instanceof RegExp);
23+
24+
let str = yaml.dump(object, { schema });
25+
assert.strictEqual(str, source);
26+
});

0 commit comments

Comments
 (0)