3 var common = require('../common');
4 var Type = require('../type');
6 var YAML_FLOAT_PATTERN = new RegExp(
7 '^(?:[-+]?(?:[0-9][0-9_]*)\\.[0-9_]*(?:[eE][-+][0-9]+)?' +
8 '|\\.[0-9_]+(?:[eE][-+][0-9]+)?' +
9 '|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]*' +
10 '|[-+]?\\.(?:inf|Inf|INF)' +
11 '|\\.(?:nan|NaN|NAN))$');
13 function resolveYamlFloat(data) {
18 var value, sign, base, digits;
20 if (!YAML_FLOAT_PATTERN.test(data)) {
26 function constructYamlFloat(data) {
27 var value, sign, base, digits;
29 value = data.replace(/_/g, '').toLowerCase();
30 sign = '-' === value[0] ? -1 : 1;
33 if (0 <= '+-'.indexOf(value[0])) {
34 value = value.slice(1);
37 if ('.inf' === value) {
38 return (1 === sign) ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
40 } else if ('.nan' === value) {
43 } else if (0 <= value.indexOf(':')) {
44 value.split(':').forEach(function (v) {
45 digits.unshift(parseFloat(v, 10));
51 digits.forEach(function (d) {
59 return sign * parseFloat(value, 10);
63 function representYamlFloat(object, style) {
73 } else if (Number.POSITIVE_INFINITY === object) {
82 } else if (Number.NEGATIVE_INFINITY === object) {
91 } else if (common.isNegativeZero(object)) {
94 return object.toString(10);
98 function isFloat(object) {
99 return ('[object Number]' === Object.prototype.toString.call(object)) &&
100 (0 !== object % 1 || common.isNegativeZero(object));
103 module.exports = new Type('tag:yaml.org,2002:float', {
105 resolve: resolveYamlFloat,
106 construct: constructYamlFloat,
108 represent: representYamlFloat,
109 defaultStyle: 'lowercase'