3 var common = require('../common');
4 var Type = require('../type');
6 function isHexCode(c) {
7 return ((0x30/* 0 */ <= c) && (c <= 0x39/* 9 */)) ||
8 ((0x41/* A */ <= c) && (c <= 0x46/* F */)) ||
9 ((0x61/* a */ <= c) && (c <= 0x66/* f */));
12 function isOctCode(c) {
13 return ((0x30/* 0 */ <= c) && (c <= 0x37/* 7 */));
16 function isDecCode(c) {
17 return ((0x30/* 0 */ <= c) && (c <= 0x39/* 9 */));
20 function resolveYamlInteger(data) {
21 if (data === null) return false;
23 var max = data.length,
28 if (!max) return false;
33 if (ch === '-' || ch === '+') {
39 if (index + 1 === max) return true;
42 // base 2, base 8, base 16
48 for (; index < max; index++) {
50 if (ch === '_') continue;
51 if (ch !== '0' && ch !== '1') return false;
54 return hasDigits && ch !== '_';
62 for (; index < max; index++) {
64 if (ch === '_') continue;
65 if (!isHexCode(data.charCodeAt(index))) return false;
68 return hasDigits && ch !== '_';
72 for (; index < max; index++) {
74 if (ch === '_') continue;
75 if (!isOctCode(data.charCodeAt(index))) return false;
78 return hasDigits && ch !== '_';
81 // base 10 (except 0) or base 60
83 // value should not start with `_`;
84 if (ch === '_') return false;
86 for (; index < max; index++) {
88 if (ch === '_') continue;
89 if (ch === ':') break;
90 if (!isDecCode(data.charCodeAt(index))) {
96 // Should have digits and should not end with `_`
97 if (!hasDigits || ch === '_') return false;
100 if (ch !== ':') return true;
102 // base60 almost not used, no needs to optimize
103 return /^(:[0-5]?[0-9])+$/.test(data.slice(index));
106 function constructYamlInteger(data) {
107 var value = data, sign = 1, ch, base, digits = [];
109 if (value.indexOf('_') !== -1) {
110 value = value.replace(/_/g, '');
115 if (ch === '-' || ch === '+') {
116 if (ch === '-') sign = -1;
117 value = value.slice(1);
121 if (value === '0') return 0;
124 if (value[1] === 'b') return sign * parseInt(value.slice(2), 2);
125 if (value[1] === 'x') return sign * parseInt(value, 16);
126 return sign * parseInt(value, 8);
129 if (value.indexOf(':') !== -1) {
130 value.split(':').forEach(function (v) {
131 digits.unshift(parseInt(v, 10));
137 digits.forEach(function (d) {
146 return sign * parseInt(value, 10);
149 function isInteger(object) {
150 return (Object.prototype.toString.call(object)) === '[object Number]' &&
151 (object % 1 === 0 && !common.isNegativeZero(object));
154 module.exports = new Type('tag:yaml.org,2002:int', {
156 resolve: resolveYamlInteger,
157 construct: constructYamlInteger,
158 predicate: isInteger,
160 binary: function (obj) { return obj >= 0 ? '0b' + obj.toString(2) : '-0b' + obj.toString(2).slice(1); },
161 octal: function (obj) { return obj >= 0 ? '0' + obj.toString(8) : '-0' + obj.toString(8).slice(1); },
162 decimal: function (obj) { return obj.toString(10); },
163 /* eslint-disable max-len */
164 hexadecimal: function (obj) { return obj >= 0 ? '0x' + obj.toString(16).toUpperCase() : '-0x' + obj.toString(16).toUpperCase().slice(1); }
166 defaultStyle: 'decimal',
168 binary: [ 2, 'bin' ],
170 decimal: [ 10, 'dec' ],
171 hexadecimal: [ 16, 'hex' ]