react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / lib / js-yaml / type / float.js
81149 views'use strict';12var common = require('../common');3var Type = require('../type');45var YAML_FLOAT_PATTERN = new RegExp(6'^(?:[-+]?(?:[0-9][0-9_]*)\\.[0-9_]*(?:[eE][-+][0-9]+)?' +7'|\\.[0-9_]+(?:[eE][-+][0-9]+)?' +8'|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]*' +9'|[-+]?\\.(?:inf|Inf|INF)' +10'|\\.(?:nan|NaN|NAN))$');1112function resolveYamlFloat(data) {13if (null === data) {14return false;15}1617var value, sign, base, digits;1819if (!YAML_FLOAT_PATTERN.test(data)) {20return false;21}22return true;23}2425function constructYamlFloat(data) {26var value, sign, base, digits;2728value = data.replace(/_/g, '').toLowerCase();29sign = '-' === value[0] ? -1 : 1;30digits = [];3132if (0 <= '+-'.indexOf(value[0])) {33value = value.slice(1);34}3536if ('.inf' === value) {37return (1 === sign) ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;3839} else if ('.nan' === value) {40return NaN;4142} else if (0 <= value.indexOf(':')) {43value.split(':').forEach(function (v) {44digits.unshift(parseFloat(v, 10));45});4647value = 0.0;48base = 1;4950digits.forEach(function (d) {51value += d * base;52base *= 60;53});5455return sign * value;5657}58return sign * parseFloat(value, 10);59}6061function representYamlFloat(object, style) {62if (isNaN(object)) {63switch (style) {64case 'lowercase':65return '.nan';66case 'uppercase':67return '.NAN';68case 'camelcase':69return '.NaN';70}71} else if (Number.POSITIVE_INFINITY === object) {72switch (style) {73case 'lowercase':74return '.inf';75case 'uppercase':76return '.INF';77case 'camelcase':78return '.Inf';79}80} else if (Number.NEGATIVE_INFINITY === object) {81switch (style) {82case 'lowercase':83return '-.inf';84case 'uppercase':85return '-.INF';86case 'camelcase':87return '-.Inf';88}89} else if (common.isNegativeZero(object)) {90return '-0.0';91}92return object.toString(10);93}9495function isFloat(object) {96return ('[object Number]' === Object.prototype.toString.call(object)) &&97(0 !== object % 1 || common.isNegativeZero(object));98}99100module.exports = new Type('tag:yaml.org,2002:float', {101kind: 'scalar',102resolve: resolveYamlFloat,103construct: constructYamlFloat,104predicate: isFloat,105represent: representYamlFloat,106defaultStyle: 'lowercase'107});108109110