-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
set.ts
123 lines (113 loc) · 3.53 KB
/
set.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { isMap, isPair, isScalar } from '../../nodes/identity.ts'
import { Pair } from '../../nodes/Pair.ts'
import type { Scalar } from '../../nodes/Scalar.ts'
import type { ToJSContext } from '../../nodes/toJS.ts'
import { findPair, YAMLMap } from '../../nodes/YAMLMap.ts'
import type { Schema } from '../../schema/Schema.ts'
import type { StringifyContext } from '../../stringify/stringify.ts'
import type { CreateNodeContext } from '../../util.ts'
import { createPair } from '../../util.ts'
import type { CollectionTag } from '../types.ts'
export class YAMLSet<T = unknown> extends YAMLMap<T, Scalar<null> | null> {
static tag = 'tag:yaml.org,2002:set'
constructor(schema?: Schema) {
super(schema)
this.tag = YAMLSet.tag
}
add(
key:
| T
| Pair<T, Scalar<null> | null>
| { key: T; value: Scalar<null> | null }
) {
let pair: Pair<T, Scalar<null> | null>
if (isPair(key)) pair = key
else if (
key &&
typeof key === 'object' &&
'key' in key &&
'value' in key &&
key.value === null
)
pair = new Pair(key.key, null)
else pair = new Pair(key as T, null)
const prev = findPair(this.items, pair.key)
if (!prev) this.items.push(pair)
}
/**
* If `keepPair` is `true`, returns the Pair matching `key`.
* Otherwise, returns the value of that Pair's key.
*/
get(key: unknown, keepPair?: boolean): any {
const pair = findPair(this.items, key)
return !keepPair && isPair(pair)
? isScalar(pair.key)
? pair.key.value
: pair.key
: pair
}
set(key: T, value: boolean): void
/** @deprecated Will throw; `value` must be boolean */
set(key: T, value: null): void
set(key: T, value: boolean | null) {
if (typeof value !== 'boolean')
throw new Error(
`Expected boolean value for set(key, value) in a YAML set, not ${typeof value}`
)
const prev = findPair(this.items, key)
if (prev && !value) {
this.items.splice(this.items.indexOf(prev), 1)
} else if (!prev && value) {
this.items.push(new Pair(key))
}
}
toJSON(_?: unknown, ctx?: ToJSContext): any {
return super.toJSON(_, ctx, Set)
}
toString(
ctx?: StringifyContext,
onComment?: () => void,
onChompKeep?: () => void
): string {
if (!ctx) return JSON.stringify(this)
if (this.hasAllNullValues(true))
return super.toString(
Object.assign({}, ctx, { allNullValues: true }),
onComment,
onChompKeep
)
else throw new Error('Set items must all have null values')
}
static from(
schema: Schema,
iterable: unknown,
ctx: CreateNodeContext
): YAMLSet {
const { replacer } = ctx
const set = new this(schema)
if (iterable && Symbol.iterator in Object(iterable))
for (let value of iterable as Iterable<unknown>) {
if (typeof replacer === 'function')
value = replacer.call(iterable, value, value)
set.items.push(
createPair(value, null, ctx) as Pair<unknown, Scalar<null>>
)
}
return set
}
}
export const set: CollectionTag = {
collection: 'map',
identify: value => value instanceof Set,
nodeClass: YAMLSet,
default: false,
tag: 'tag:yaml.org,2002:set',
createNode: (schema, iterable, ctx) => YAMLSet.from(schema, iterable, ctx),
resolve(map, onError) {
if (isMap(map)) {
if (map.hasAllNullValues(true)) return Object.assign(new YAMLSet(), map)
else onError('Set items must all have null values')
} else onError('Expected a mapping for this tag')
return map
}
}