source: node_modules/yaml/dist/schema/yaml-1.1/float.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.6 KB
Line 
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;
Note: See TracBrowser for help on using the repository browser.