Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing YAML 1.1 collections: omap, pairs, set #65

Merged
merged 13 commits into from
Jan 1, 2019
Merged

Add missing YAML 1.1 collections: omap, pairs, set #65

merged 13 commits into from
Jan 1, 2019

Conversation

eemeli
Copy link
Owner

@eemeli eemeli commented Dec 30, 2018

This adds a new instance method createNode(item, wrapScalars, tag) to the document schema, along with adding the following tags to the yaml-1.1 schema:

!!omap is an ordered key:value mapping, represented in JS as a Map and in YAML as a sequence of pairs:

const src = '!!omap [ a: 1, b: 2, c: 3 ]'
const map = YAML.parse(src, { version: '1.1' }) // Map { 'a' => 1, 'b' => 2, 'c' => 3 }

YAML.stringify(map, { version: '1.1' }) // '!!omap\n- a: 1\n- b: 2\n- c: 3\n'
YAML.stringify(map, { version: '1.2' }) // 'a: 1\nb: 2\nc: 3\n'

!!pairs is very similar to !!omap, but allows duplicate keys. It does not have an exactly corresponding JS equivalent, and so is represented as an array of { key: value } objects:

const src = '!!pairs [ a: 1, b: 2, a: 3 ]'
const pairs = YAML.parse(src, { version: '1.1' }) // [ { a: 1 }, { b: 2 }, { a: 3 } ]

// Array of pairs is by default parsed as !!seq
YAML.stringify(pairs, { version: '1.1' }) // '- a: 1\n- b: 2\n- a: 3\n'

// To create a new !!pairs, you'll need a document with a schema
const doc = new YAML.Document({ version: '1.1' })
doc.setSchema()
doc.contents = doc.schema.createNode(pairs, false, '!!pairs')
String(doc) // '!!pairs\n- a: 1\n- b: 2\n- a: 3\n'

Both !!omap and !!pairs support construction from either a Map, an array of [ key, value ] tuples, or an array of { key: value } objects.

!!set is an unordered set of unique values. In JS it's a Set and in YAML a mapping with null values:

const src = '!!set { a, b, c }'
const set = YAML.parse(src, { version: '1.1' }) // Set { 'a', 'b', 'c' }

YAML.stringify(set, { version: '1.1' }) // '!!set\n? a\n? b\n? c\n'
YAML.stringify(set, { version: '1.2' }) // '- a\n- b\n- c\n'

To use these collections with e.g. the core schema, they may be included as custom tags:

import omap from 'yaml/types/omap'
import pairs from 'yaml/types/pairs'
import set from 'yaml/types/set'

YAML.defaultOptions.tags = [ omap, pairs, set ]
YAML.parse('!!set { a, b, c }') // Set { 'a', 'b', 'c' }

With these additions, yaml should now directly support all YAML 1.1 tags, except for !!value and !!yaml, which are unlikely to get implemented unless someone comes up with a really convincing usage scenario.

eemeli added a commit to eemeli/yaml-docs that referenced this pull request Jan 1, 2019
@eemeli eemeli merged commit 0b0b163 into master Jan 1, 2019
@eemeli eemeli deleted the sets branch January 1, 2019 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant