Skip to content

Commit

Permalink
feat: Add YAML 1.1 tags: !!binary, !!omap, !!pairs, !!set, !!timestamp (
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli authored Jul 7, 2024
1 parent 79abc64 commit 2e450f0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,22 @@ const re = parse('!re /fo./g', { customTags: [regexp] })
a function must be provided as `customTags` in order to prepend the `bigint` tag,
or else the built-in `!!int` tags will take priority.
See [bigint.test.ts](./src/bigint.test.ts) for examples.
- `binary` (`!!binary`) - JavaScript [Uint8Array], one of the YAML 1.1 tags
- `classTag` (`!class`) - JavaScript [Class] values
- `error` (`!error`) - JavaScript [Error] objects
- `functionTag` (`!function`) - JavaScript [Function] values
(will also be used to stringify Class values,
unless the `classTag` tag is loaded ahead of `functionTag`)
- `nullobject` (`!nullobject`) - Object with a `null` prototype
- `omap` (`!!omap`) - JavaScript [Map], one of the YAML 1.1 tags
- `pairs` (`!!pairs`) - Ordered sequence of key-value [pairs], one of the YAML 1.1 tags
- `regexp` (`!re`) - [RegExp] values,
using their default `/foo/flags` string representation.
- `set` (`!!set`) - JavaScript [Set], one of the YAML 1.1 tags
- `sharedSymbol` (`!symbol/shared`) - [Shared Symbols],
i.e. ones created with `Symbol.for()`
- `symbol` (`!symbol`) - [Unique Symbols]
- `timestamp` (`!!timestamp`) - JavaScript [Date], one of the YAML 1.1 tags

The function and class values created by parsing `!function` and
`!class` tags will not actually replicate running code, but
Expand All @@ -45,11 +50,16 @@ rather no-op function/class values with matching name and

[BigInt]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
[Class]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
[Date]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
[Error]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
[Function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions
[Map]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
[RegExp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions
[Set]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
[Shared Symbols]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol#shared_symbols_in_the_global_symbol_registry
[Uint8Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
[Unique Symbols]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol
[pairs]: https://yaml.org/type/pairs.html

## Customising Tag Names

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { functionTag } from './function.js'
export { nullobject } from './null-object.js'
export { regexp } from './regexp.js'
export { sharedSymbol, symbol } from './symbol.js'
export { binary, omap, pairs, set, timestamp } from './yaml11.js'
11 changes: 11 additions & 0 deletions src/yaml11.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { test } from 'tap'

import { binary, omap, pairs, set, timestamp } from '.'

test('tag defined', t => {
for (const tag of [binary, omap, pairs, set, timestamp]) {
t.type(tag, 'object', `${tag.tag} is an object`)
t.equal(tag.default, false, `${tag.tag} is not default`)
}
t.end()
})
20 changes: 20 additions & 0 deletions src/yaml11.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { type CollectionTag, type ScalarTag, Schema } from 'yaml'

const tags = new Schema({ resolveKnownTags: true }).knownTags as {
'tag:yaml.org,2002:binary': ScalarTag
'tag:yaml.org,2002:omap': CollectionTag
'tag:yaml.org,2002:pairs': CollectionTag
'tag:yaml.org,2002:set': CollectionTag
'tag:yaml.org,2002:timestamp': ScalarTag
}

export const {
'tag:yaml.org,2002:binary': binary,
'tag:yaml.org,2002:omap': omap,
'tag:yaml.org,2002:pairs': pairs,
'tag:yaml.org,2002:set': set
} = tags
export const timestamp: ScalarTag = {
...tags['tag:yaml.org,2002:timestamp'],
default: false
}

0 comments on commit 2e450f0

Please sign in to comment.