UNPKG

1.63 kBJavaScriptView Raw
1'use strict';
2
3var Scalar = require('../../nodes/Scalar.js');
4var stringifyNumber = require('../../stringify/stringifyNumber.js');
5
6const floatNaN = {
7 identify: value => typeof value === 'number',
8 default: true,
9 tag: 'tag:yaml.org,2002:float',
10 test: /^(?:[-+]?\.(?:inf|Inf|INF)|\.nan|\.NaN|\.NAN)$/,
11 resolve: (str) => str.slice(-3).toLowerCase() === 'nan'
12 ? NaN
13 : str[0] === '-'
14 ? Number.NEGATIVE_INFINITY
15 : Number.POSITIVE_INFINITY,
16 stringify: stringifyNumber.stringifyNumber
17};
18const floatExp = {
19 identify: value => typeof value === 'number',
20 default: true,
21 tag: 'tag:yaml.org,2002:float',
22 format: 'EXP',
23 test: /^[-+]?(?:[0-9][0-9_]*)?(?:\.[0-9_]*)?[eE][-+]?[0-9]+$/,
24 resolve: (str) => parseFloat(str.replace(/_/g, '')),
25 stringify(node) {
26 const num = Number(node.value);
27 return isFinite(num) ? num.toExponential() : stringifyNumber.stringifyNumber(node);
28 }
29};
30const float = {
31 identify: value => typeof value === 'number',
32 default: true,
33 tag: 'tag:yaml.org,2002:float',
34 test: /^[-+]?(?:[0-9][0-9_]*)?\.[0-9_]*$/,
35 resolve(str) {
36 const node = new Scalar.Scalar(parseFloat(str.replace(/_/g, '')));
37 const dot = str.indexOf('.');
38 if (dot !== -1) {
39 const f = str.substring(dot + 1).replace(/_/g, '');
40 if (f[f.length - 1] === '0')
41 node.minFractionDigits = f.length;
42 }
43 return node;
44 },
45 stringify: stringifyNumber.stringifyNumber
46};
47
48exports.float = float;
49exports.floatExp = floatExp;
50exports.floatNaN = floatNaN;