[d24f17c] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var identity = require('../../nodes/identity.js');
|
---|
| 4 | var Pair = require('../../nodes/Pair.js');
|
---|
| 5 | var YAMLMap = require('../../nodes/YAMLMap.js');
|
---|
| 6 |
|
---|
| 7 | class YAMLSet extends YAMLMap.YAMLMap {
|
---|
| 8 | constructor(schema) {
|
---|
| 9 | super(schema);
|
---|
| 10 | this.tag = YAMLSet.tag;
|
---|
| 11 | }
|
---|
| 12 | add(key) {
|
---|
| 13 | let pair;
|
---|
| 14 | if (identity.isPair(key))
|
---|
| 15 | pair = key;
|
---|
| 16 | else if (key &&
|
---|
| 17 | typeof key === 'object' &&
|
---|
| 18 | 'key' in key &&
|
---|
| 19 | 'value' in key &&
|
---|
| 20 | key.value === null)
|
---|
| 21 | pair = new Pair.Pair(key.key, null);
|
---|
| 22 | else
|
---|
| 23 | pair = new Pair.Pair(key, null);
|
---|
| 24 | const prev = YAMLMap.findPair(this.items, pair.key);
|
---|
| 25 | if (!prev)
|
---|
| 26 | this.items.push(pair);
|
---|
| 27 | }
|
---|
| 28 | /**
|
---|
| 29 | * If `keepPair` is `true`, returns the Pair matching `key`.
|
---|
| 30 | * Otherwise, returns the value of that Pair's key.
|
---|
| 31 | */
|
---|
| 32 | get(key, keepPair) {
|
---|
| 33 | const pair = YAMLMap.findPair(this.items, key);
|
---|
| 34 | return !keepPair && identity.isPair(pair)
|
---|
| 35 | ? identity.isScalar(pair.key)
|
---|
| 36 | ? pair.key.value
|
---|
| 37 | : pair.key
|
---|
| 38 | : pair;
|
---|
| 39 | }
|
---|
| 40 | set(key, value) {
|
---|
| 41 | if (typeof value !== 'boolean')
|
---|
| 42 | throw new Error(`Expected boolean value for set(key, value) in a YAML set, not ${typeof value}`);
|
---|
| 43 | const prev = YAMLMap.findPair(this.items, key);
|
---|
| 44 | if (prev && !value) {
|
---|
| 45 | this.items.splice(this.items.indexOf(prev), 1);
|
---|
| 46 | }
|
---|
| 47 | else if (!prev && value) {
|
---|
| 48 | this.items.push(new Pair.Pair(key));
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 | toJSON(_, ctx) {
|
---|
| 52 | return super.toJSON(_, ctx, Set);
|
---|
| 53 | }
|
---|
| 54 | toString(ctx, onComment, onChompKeep) {
|
---|
| 55 | if (!ctx)
|
---|
| 56 | return JSON.stringify(this);
|
---|
| 57 | if (this.hasAllNullValues(true))
|
---|
| 58 | return super.toString(Object.assign({}, ctx, { allNullValues: true }), onComment, onChompKeep);
|
---|
| 59 | else
|
---|
| 60 | throw new Error('Set items must all have null values');
|
---|
| 61 | }
|
---|
| 62 | static from(schema, iterable, ctx) {
|
---|
| 63 | const { replacer } = ctx;
|
---|
| 64 | const set = new this(schema);
|
---|
| 65 | if (iterable && Symbol.iterator in Object(iterable))
|
---|
| 66 | for (let value of iterable) {
|
---|
| 67 | if (typeof replacer === 'function')
|
---|
| 68 | value = replacer.call(iterable, value, value);
|
---|
| 69 | set.items.push(Pair.createPair(value, null, ctx));
|
---|
| 70 | }
|
---|
| 71 | return set;
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 | YAMLSet.tag = 'tag:yaml.org,2002:set';
|
---|
| 75 | const set = {
|
---|
| 76 | collection: 'map',
|
---|
| 77 | identify: value => value instanceof Set,
|
---|
| 78 | nodeClass: YAMLSet,
|
---|
| 79 | default: false,
|
---|
| 80 | tag: 'tag:yaml.org,2002:set',
|
---|
| 81 | createNode: (schema, iterable, ctx) => YAMLSet.from(schema, iterable, ctx),
|
---|
| 82 | resolve(map, onError) {
|
---|
| 83 | if (identity.isMap(map)) {
|
---|
| 84 | if (map.hasAllNullValues(true))
|
---|
| 85 | return Object.assign(new YAMLSet(), map);
|
---|
| 86 | else
|
---|
| 87 | onError('Set items must all have null values');
|
---|
| 88 | }
|
---|
| 89 | else
|
---|
| 90 | onError('Expected a mapping for this tag');
|
---|
| 91 | return map;
|
---|
| 92 | }
|
---|
| 93 | };
|
---|
| 94 |
|
---|
| 95 | exports.YAMLSet = YAMLSet;
|
---|
| 96 | exports.set = set;
|
---|