1 | 'use strict';
|
---|
2 |
|
---|
3 | var Scalar = require('../../nodes/Scalar.js');
|
---|
4 | var map = require('../common/map.js');
|
---|
5 | var seq = require('../common/seq.js');
|
---|
6 |
|
---|
7 | function intIdentify(value) {
|
---|
8 | return typeof value === 'bigint' || Number.isInteger(value);
|
---|
9 | }
|
---|
10 | const stringifyJSON = ({ value }) => JSON.stringify(value);
|
---|
11 | const jsonScalars = [
|
---|
12 | {
|
---|
13 | identify: value => typeof value === 'string',
|
---|
14 | default: true,
|
---|
15 | tag: 'tag:yaml.org,2002:str',
|
---|
16 | resolve: str => str,
|
---|
17 | stringify: stringifyJSON
|
---|
18 | },
|
---|
19 | {
|
---|
20 | identify: value => value == null,
|
---|
21 | createNode: () => new Scalar.Scalar(null),
|
---|
22 | default: true,
|
---|
23 | tag: 'tag:yaml.org,2002:null',
|
---|
24 | test: /^null$/,
|
---|
25 | resolve: () => null,
|
---|
26 | stringify: stringifyJSON
|
---|
27 | },
|
---|
28 | {
|
---|
29 | identify: value => typeof value === 'boolean',
|
---|
30 | default: true,
|
---|
31 | tag: 'tag:yaml.org,2002:bool',
|
---|
32 | test: /^true|false$/,
|
---|
33 | resolve: str => str === 'true',
|
---|
34 | stringify: stringifyJSON
|
---|
35 | },
|
---|
36 | {
|
---|
37 | identify: intIdentify,
|
---|
38 | default: true,
|
---|
39 | tag: 'tag:yaml.org,2002:int',
|
---|
40 | test: /^-?(?:0|[1-9][0-9]*)$/,
|
---|
41 | resolve: (str, _onError, { intAsBigInt }) => intAsBigInt ? BigInt(str) : parseInt(str, 10),
|
---|
42 | stringify: ({ value }) => intIdentify(value) ? value.toString() : JSON.stringify(value)
|
---|
43 | },
|
---|
44 | {
|
---|
45 | identify: value => typeof value === 'number',
|
---|
46 | default: true,
|
---|
47 | tag: 'tag:yaml.org,2002:float',
|
---|
48 | test: /^-?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][-+]?[0-9]+)?$/,
|
---|
49 | resolve: str => parseFloat(str),
|
---|
50 | stringify: stringifyJSON
|
---|
51 | }
|
---|
52 | ];
|
---|
53 | const jsonError = {
|
---|
54 | default: true,
|
---|
55 | tag: '',
|
---|
56 | test: /^/,
|
---|
57 | resolve(str, onError) {
|
---|
58 | onError(`Unresolved plain scalar ${JSON.stringify(str)}`);
|
---|
59 | return str;
|
---|
60 | }
|
---|
61 | };
|
---|
62 | const schema = [map.map, seq.seq].concat(jsonScalars, jsonError);
|
---|
63 |
|
---|
64 | exports.schema = schema;
|
---|