Initial commit
[yaffs-website] / node_modules / js-yaml / lib / js-yaml / type / float.js
1 'use strict';
2
3 var common = require('../common');
4 var Type   = require('../type');
5
6 var YAML_FLOAT_PATTERN = new RegExp(
7   // 2.5e4, 2.5 and integers
8   '^(?:[-+]?(?:0|[1-9][0-9_]*)(?:\\.[0-9_]*)?(?:[eE][-+]?[0-9]+)?' +
9   // .2e4, .2
10   // special case, seems not from spec
11   '|\\.[0-9_]+(?:[eE][-+]?[0-9]+)?' +
12   // 20:59
13   '|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]*' +
14   // .inf
15   '|[-+]?\\.(?:inf|Inf|INF)' +
16   // .nan
17   '|\\.(?:nan|NaN|NAN))$');
18
19 function resolveYamlFloat(data) {
20   if (data === null) return false;
21
22   if (!YAML_FLOAT_PATTERN.test(data)) return false;
23
24   return true;
25 }
26
27 function constructYamlFloat(data) {
28   var value, sign, base, digits;
29
30   value  = data.replace(/_/g, '').toLowerCase();
31   sign   = value[0] === '-' ? -1 : 1;
32   digits = [];
33
34   if ('+-'.indexOf(value[0]) >= 0) {
35     value = value.slice(1);
36   }
37
38   if (value === '.inf') {
39     return (sign === 1) ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
40
41   } else if (value === '.nan') {
42     return NaN;
43
44   } else if (value.indexOf(':') >= 0) {
45     value.split(':').forEach(function (v) {
46       digits.unshift(parseFloat(v, 10));
47     });
48
49     value = 0.0;
50     base = 1;
51
52     digits.forEach(function (d) {
53       value += d * base;
54       base *= 60;
55     });
56
57     return sign * value;
58
59   }
60   return sign * parseFloat(value, 10);
61 }
62
63
64 var SCIENTIFIC_WITHOUT_DOT = /^[-+]?[0-9]+e/;
65
66 function representYamlFloat(object, style) {
67   var res;
68
69   if (isNaN(object)) {
70     switch (style) {
71       case 'lowercase': return '.nan';
72       case 'uppercase': return '.NAN';
73       case 'camelcase': return '.NaN';
74     }
75   } else if (Number.POSITIVE_INFINITY === object) {
76     switch (style) {
77       case 'lowercase': return '.inf';
78       case 'uppercase': return '.INF';
79       case 'camelcase': return '.Inf';
80     }
81   } else if (Number.NEGATIVE_INFINITY === object) {
82     switch (style) {
83       case 'lowercase': return '-.inf';
84       case 'uppercase': return '-.INF';
85       case 'camelcase': return '-.Inf';
86     }
87   } else if (common.isNegativeZero(object)) {
88     return '-0.0';
89   }
90
91   res = object.toString(10);
92
93   // JS stringifier can build scientific format without dots: 5e-100,
94   // while YAML requres dot: 5.e-100. Fix it with simple hack
95
96   return SCIENTIFIC_WITHOUT_DOT.test(res) ? res.replace('e', '.e') : res;
97 }
98
99 function isFloat(object) {
100   return (Object.prototype.toString.call(object) === '[object Number]') &&
101          (object % 1 !== 0 || common.isNegativeZero(object));
102 }
103
104 module.exports = new Type('tag:yaml.org,2002:float', {
105   kind: 'scalar',
106   resolve: resolveYamlFloat,
107   construct: constructYamlFloat,
108   predicate: isFloat,
109   represent: representYamlFloat,
110   defaultStyle: 'lowercase'
111 });