b89183931632a1cb2e2e57296e0d37b427ae0941.svn-base
14.3 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
'use strict';
var PlainValue = require('./PlainValue-ec8e588e.js');
var resolveSeq = require('./resolveSeq-4a68b39b.js');
var warnings = require('./warnings-39684f17.js');
function createMap(schema, obj, ctx) {
const map = new resolveSeq.YAMLMap(schema);
if (obj instanceof Map) {
for (const [key, value] of obj) map.items.push(schema.createPair(key, value, ctx));
} else if (obj && typeof obj === 'object') {
for (const key of Object.keys(obj)) map.items.push(schema.createPair(key, obj[key], ctx));
}
if (typeof schema.sortMapEntries === 'function') {
map.items.sort(schema.sortMapEntries);
}
return map;
}
const map = {
createNode: createMap,
default: true,
nodeClass: resolveSeq.YAMLMap,
tag: 'tag:yaml.org,2002:map',
resolve: resolveSeq.resolveMap
};
function createSeq(schema, obj, ctx) {
const seq = new resolveSeq.YAMLSeq(schema);
if (obj && obj[Symbol.iterator]) {
for (const it of obj) {
const v = schema.createNode(it, ctx.wrapScalars, null, ctx);
seq.items.push(v);
}
}
return seq;
}
const seq = {
createNode: createSeq,
default: true,
nodeClass: resolveSeq.YAMLSeq,
tag: 'tag:yaml.org,2002:seq',
resolve: resolveSeq.resolveSeq
};
const string = {
identify: value => typeof value === 'string',
default: true,
tag: 'tag:yaml.org,2002:str',
resolve: resolveSeq.resolveString,
stringify(item, ctx, onComment, onChompKeep) {
ctx = Object.assign({
actualString: true
}, ctx);
return resolveSeq.stringifyString(item, ctx, onComment, onChompKeep);
},
options: resolveSeq.strOptions
};
const failsafe = [map, seq, string];
/* global BigInt */
const intIdentify = value => typeof value === 'bigint' || Number.isInteger(value);
const intResolve = (src, part, radix) => resolveSeq.intOptions.asBigInt ? BigInt(src) : parseInt(part, radix);
function intStringify(node, radix, prefix) {
const {
value
} = node;
if (intIdentify(value) && value >= 0) return prefix + value.toString(radix);
return resolveSeq.stringifyNumber(node);
}
const nullObj = {
identify: value => value == null,
createNode: (schema, value, ctx) => ctx.wrapScalars ? new resolveSeq.Scalar(null) : null,
default: true,
tag: 'tag:yaml.org,2002:null',
test: /^(?:~|[Nn]ull|NULL)?$/,
resolve: () => null,
options: resolveSeq.nullOptions,
stringify: () => resolveSeq.nullOptions.nullStr
};
const boolObj = {
identify: value => typeof value === 'boolean',
default: true,
tag: 'tag:yaml.org,2002:bool',
test: /^(?:[Tt]rue|TRUE|[Ff]alse|FALSE)$/,
resolve: str => str[0] === 't' || str[0] === 'T',
options: resolveSeq.boolOptions,
stringify: ({
value
}) => value ? resolveSeq.boolOptions.trueStr : resolveSeq.boolOptions.falseStr
};
const octObj = {
identify: value => intIdentify(value) && value >= 0,
default: true,
tag: 'tag:yaml.org,2002:int',
format: 'OCT',
test: /^0o([0-7]+)$/,
resolve: (str, oct) => intResolve(str, oct, 8),
options: resolveSeq.intOptions,
stringify: node => intStringify(node, 8, '0o')
};
const intObj = {
identify: intIdentify,
default: true,
tag: 'tag:yaml.org,2002:int',
test: /^[-+]?[0-9]+$/,
resolve: str => intResolve(str, str, 10),
options: resolveSeq.intOptions,
stringify: resolveSeq.stringifyNumber
};
const hexObj = {
identify: value => intIdentify(value) && value >= 0,
default: true,
tag: 'tag:yaml.org,2002:int',
format: 'HEX',
test: /^0x([0-9a-fA-F]+)$/,
resolve: (str, hex) => intResolve(str, hex, 16),
options: resolveSeq.intOptions,
stringify: node => intStringify(node, 16, '0x')
};
const nanObj = {
identify: value => typeof value === 'number',
default: true,
tag: 'tag:yaml.org,2002:float',
test: /^(?:[-+]?\.inf|(\.nan))$/i,
resolve: (str, nan) => nan ? NaN : str[0] === '-' ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY,
stringify: resolveSeq.stringifyNumber
};
const expObj = {
identify: value => typeof value === 'number',
default: true,
tag: 'tag:yaml.org,2002:float',
format: 'EXP',
test: /^[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)[eE][-+]?[0-9]+$/,
resolve: str => parseFloat(str),
stringify: ({
value
}) => Number(value).toExponential()
};
const floatObj = {
identify: value => typeof value === 'number',
default: true,
tag: 'tag:yaml.org,2002:float',
test: /^[-+]?(?:\.([0-9]+)|[0-9]+\.([0-9]*))$/,
resolve(str, frac1, frac2) {
const frac = frac1 || frac2;
const node = new resolveSeq.Scalar(parseFloat(str));
if (frac && frac[frac.length - 1] === '0') node.minFractionDigits = frac.length;
return node;
},
stringify: resolveSeq.stringifyNumber
};
const core = failsafe.concat([nullObj, boolObj, octObj, intObj, hexObj, nanObj, expObj, floatObj]);
/* global BigInt */
const intIdentify$1 = value => typeof value === 'bigint' || Number.isInteger(value);
const stringifyJSON = ({
value
}) => JSON.stringify(value);
const json = [map, seq, {
identify: value => typeof value === 'string',
default: true,
tag: 'tag:yaml.org,2002:str',
resolve: resolveSeq.resolveString,
stringify: stringifyJSON
}, {
identify: value => value == null,
createNode: (schema, value, ctx) => ctx.wrapScalars ? new resolveSeq.Scalar(null) : null,
default: true,
tag: 'tag:yaml.org,2002:null',
test: /^null$/,
resolve: () => null,
stringify: stringifyJSON
}, {
identify: value => typeof value === 'boolean',
default: true,
tag: 'tag:yaml.org,2002:bool',
test: /^true|false$/,
resolve: str => str === 'true',
stringify: stringifyJSON
}, {
identify: intIdentify$1,
default: true,
tag: 'tag:yaml.org,2002:int',
test: /^-?(?:0|[1-9][0-9]*)$/,
resolve: str => resolveSeq.intOptions.asBigInt ? BigInt(str) : parseInt(str, 10),
stringify: ({
value
}) => intIdentify$1(value) ? value.toString() : JSON.stringify(value)
}, {
identify: value => typeof value === 'number',
default: true,
tag: 'tag:yaml.org,2002:float',
test: /^-?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][-+]?[0-9]+)?$/,
resolve: str => parseFloat(str),
stringify: stringifyJSON
}];
json.scalarFallback = str => {
throw new SyntaxError(`Unresolved plain scalar ${JSON.stringify(str)}`);
};
/* global BigInt */
const boolStringify = ({
value
}) => value ? resolveSeq.boolOptions.trueStr : resolveSeq.boolOptions.falseStr;
const intIdentify$2 = value => typeof value === 'bigint' || Number.isInteger(value);
function intResolve$1(sign, src, radix) {
let str = src.replace(/_/g, '');
if (resolveSeq.intOptions.asBigInt) {
switch (radix) {
case 2:
str = `0b${str}`;
break;
case 8:
str = `0o${str}`;
break;
case 16:
str = `0x${str}`;
break;
}
const n = BigInt(str);
return sign === '-' ? BigInt(-1) * n : n;
}
const n = parseInt(str, radix);
return sign === '-' ? -1 * n : n;
}
function intStringify$1(node, radix, prefix) {
const {
value
} = node;
if (intIdentify$2(value)) {
const str = value.toString(radix);
return value < 0 ? '-' + prefix + str.substr(1) : prefix + str;
}
return resolveSeq.stringifyNumber(node);
}
const yaml11 = failsafe.concat([{
identify: value => value == null,
createNode: (schema, value, ctx) => ctx.wrapScalars ? new resolveSeq.Scalar(null) : null,
default: true,
tag: 'tag:yaml.org,2002:null',
test: /^(?:~|[Nn]ull|NULL)?$/,
resolve: () => null,
options: resolveSeq.nullOptions,
stringify: () => resolveSeq.nullOptions.nullStr
}, {
identify: value => typeof value === 'boolean',
default: true,
tag: 'tag:yaml.org,2002:bool',
test: /^(?:Y|y|[Yy]es|YES|[Tt]rue|TRUE|[Oo]n|ON)$/,
resolve: () => true,
options: resolveSeq.boolOptions,
stringify: boolStringify
}, {
identify: value => typeof value === 'boolean',
default: true,
tag: 'tag:yaml.org,2002:bool',
test: /^(?:N|n|[Nn]o|NO|[Ff]alse|FALSE|[Oo]ff|OFF)$/i,
resolve: () => false,
options: resolveSeq.boolOptions,
stringify: boolStringify
}, {
identify: intIdentify$2,
default: true,
tag: 'tag:yaml.org,2002:int',
format: 'BIN',
test: /^([-+]?)0b([0-1_]+)$/,
resolve: (str, sign, bin) => intResolve$1(sign, bin, 2),
stringify: node => intStringify$1(node, 2, '0b')
}, {
identify: intIdentify$2,
default: true,
tag: 'tag:yaml.org,2002:int',
format: 'OCT',
test: /^([-+]?)0([0-7_]+)$/,
resolve: (str, sign, oct) => intResolve$1(sign, oct, 8),
stringify: node => intStringify$1(node, 8, '0')
}, {
identify: intIdentify$2,
default: true,
tag: 'tag:yaml.org,2002:int',
test: /^([-+]?)([0-9][0-9_]*)$/,
resolve: (str, sign, abs) => intResolve$1(sign, abs, 10),
stringify: resolveSeq.stringifyNumber
}, {
identify: intIdentify$2,
default: true,
tag: 'tag:yaml.org,2002:int',
format: 'HEX',
test: /^([-+]?)0x([0-9a-fA-F_]+)$/,
resolve: (str, sign, hex) => intResolve$1(sign, hex, 16),
stringify: node => intStringify$1(node, 16, '0x')
}, {
identify: value => typeof value === 'number',
default: true,
tag: 'tag:yaml.org,2002:float',
test: /^(?:[-+]?\.inf|(\.nan))$/i,
resolve: (str, nan) => nan ? NaN : str[0] === '-' ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY,
stringify: resolveSeq.stringifyNumber
}, {
identify: value => typeof value === 'number',
default: true,
tag: 'tag:yaml.org,2002:float',
format: 'EXP',
test: /^[-+]?([0-9][0-9_]*)?(\.[0-9_]*)?[eE][-+]?[0-9]+$/,
resolve: str => parseFloat(str.replace(/_/g, '')),
stringify: ({
value
}) => Number(value).toExponential()
}, {
identify: value => typeof value === 'number',
default: true,
tag: 'tag:yaml.org,2002:float',
test: /^[-+]?(?:[0-9][0-9_]*)?\.([0-9_]*)$/,
resolve(str, frac) {
const node = new resolveSeq.Scalar(parseFloat(str.replace(/_/g, '')));
if (frac) {
const f = frac.replace(/_/g, '');
if (f[f.length - 1] === '0') node.minFractionDigits = f.length;
}
return node;
},
stringify: resolveSeq.stringifyNumber
}], warnings.binary, warnings.omap, warnings.pairs, warnings.set, warnings.intTime, warnings.floatTime, warnings.timestamp);
const schemas = {
core,
failsafe,
json,
yaml11
};
const tags = {
binary: warnings.binary,
bool: boolObj,
float: floatObj,
floatExp: expObj,
floatNaN: nanObj,
floatTime: warnings.floatTime,
int: intObj,
intHex: hexObj,
intOct: octObj,
intTime: warnings.intTime,
map,
null: nullObj,
omap: warnings.omap,
pairs: warnings.pairs,
seq,
set: warnings.set,
timestamp: warnings.timestamp
};
function findTagObject(value, tagName, tags) {
if (tagName) {
const match = tags.filter(t => t.tag === tagName);
const tagObj = match.find(t => !t.format) || match[0];
if (!tagObj) throw new Error(`Tag ${tagName} not found`);
return tagObj;
} // TODO: deprecate/remove class check
return tags.find(t => (t.identify && t.identify(value) || t.class && value instanceof t.class) && !t.format);
}
function createNode(value, tagName, ctx) {
if (value instanceof resolveSeq.Node) return value;
const {
defaultPrefix,
onTagObj,
prevObjects,
schema,
wrapScalars
} = ctx;
if (tagName && tagName.startsWith('!!')) tagName = defaultPrefix + tagName.slice(2);
let tagObj = findTagObject(value, tagName, schema.tags);
if (!tagObj) {
if (typeof value.toJSON === 'function') value = value.toJSON();
if (typeof value !== 'object') return wrapScalars ? new resolveSeq.Scalar(value) : value;
tagObj = value instanceof Map ? map : value[Symbol.iterator] ? seq : map;
}
if (onTagObj) {
onTagObj(tagObj);
delete ctx.onTagObj;
} // Detect duplicate references to the same object & use Alias nodes for all
// after first. The `obj` wrapper allows for circular references to resolve.
const obj = {};
if (value && typeof value === 'object' && prevObjects) {
const prev = prevObjects.get(value);
if (prev) {
const alias = new resolveSeq.Alias(prev); // leaves source dirty; must be cleaned by caller
ctx.aliasNodes.push(alias); // defined along with prevObjects
return alias;
}
obj.value = value;
prevObjects.set(value, obj);
}
obj.node = tagObj.createNode ? tagObj.createNode(ctx.schema, value, ctx) : wrapScalars ? new resolveSeq.Scalar(value) : value;
if (tagName && obj.node instanceof resolveSeq.Node) obj.node.tag = tagName;
return obj.node;
}
function getSchemaTags(schemas, knownTags, customTags, schemaId) {
let tags = schemas[schemaId.replace(/\W/g, '')]; // 'yaml-1.1' -> 'yaml11'
if (!tags) {
const keys = Object.keys(schemas).map(key => JSON.stringify(key)).join(', ');
throw new Error(`Unknown schema "${schemaId}"; use one of ${keys}`);
}
if (Array.isArray(customTags)) {
for (const tag of customTags) tags = tags.concat(tag);
} else if (typeof customTags === 'function') {
tags = customTags(tags.slice());
}
for (let i = 0; i < tags.length; ++i) {
const tag = tags[i];
if (typeof tag === 'string') {
const tagObj = knownTags[tag];
if (!tagObj) {
const keys = Object.keys(knownTags).map(key => JSON.stringify(key)).join(', ');
throw new Error(`Unknown custom tag "${tag}"; use one of ${keys}`);
}
tags[i] = tagObj;
}
}
return tags;
}
const sortMapEntriesByKey = (a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
class Schema {
// TODO: remove in v2
// TODO: remove in v2
constructor({
customTags,
merge,
schema,
sortMapEntries,
tags: deprecatedCustomTags
}) {
this.merge = !!merge;
this.name = schema;
this.sortMapEntries = sortMapEntries === true ? sortMapEntriesByKey : sortMapEntries || null;
if (!customTags && deprecatedCustomTags) warnings.warnOptionDeprecation('tags', 'customTags');
this.tags = getSchemaTags(schemas, tags, customTags || deprecatedCustomTags, schema);
}
createNode(value, wrapScalars, tagName, ctx) {
const baseCtx = {
defaultPrefix: Schema.defaultPrefix,
schema: this,
wrapScalars
};
const createCtx = ctx ? Object.assign(ctx, baseCtx) : baseCtx;
return createNode(value, tagName, createCtx);
}
createPair(key, value, ctx) {
if (!ctx) ctx = {
wrapScalars: true
};
const k = this.createNode(key, ctx.wrapScalars, null, ctx);
const v = this.createNode(value, ctx.wrapScalars, null, ctx);
return new resolveSeq.Pair(k, v);
}
}
PlainValue._defineProperty(Schema, "defaultPrefix", PlainValue.defaultTagPrefix);
PlainValue._defineProperty(Schema, "defaultTags", PlainValue.defaultTags);
exports.Schema = Schema;