1 | 'use strict';
|
---|
2 |
|
---|
3 | var stringifyNumber = require('../../stringify/stringifyNumber.js');
|
---|
4 |
|
---|
5 | const intIdentify = (value) => typeof value === 'bigint' || Number.isInteger(value);
|
---|
6 | const intResolve = (str, offset, radix, { intAsBigInt }) => (intAsBigInt ? BigInt(str) : parseInt(str.substring(offset), radix));
|
---|
7 | function intStringify(node, radix, prefix) {
|
---|
8 | const { value } = node;
|
---|
9 | if (intIdentify(value) && value >= 0)
|
---|
10 | return prefix + value.toString(radix);
|
---|
11 | return stringifyNumber.stringifyNumber(node);
|
---|
12 | }
|
---|
13 | const intOct = {
|
---|
14 | identify: value => intIdentify(value) && value >= 0,
|
---|
15 | default: true,
|
---|
16 | tag: 'tag:yaml.org,2002:int',
|
---|
17 | format: 'OCT',
|
---|
18 | test: /^0o[0-7]+$/,
|
---|
19 | resolve: (str, _onError, opt) => intResolve(str, 2, 8, opt),
|
---|
20 | stringify: node => intStringify(node, 8, '0o')
|
---|
21 | };
|
---|
22 | const int = {
|
---|
23 | identify: intIdentify,
|
---|
24 | default: true,
|
---|
25 | tag: 'tag:yaml.org,2002:int',
|
---|
26 | test: /^[-+]?[0-9]+$/,
|
---|
27 | resolve: (str, _onError, opt) => intResolve(str, 0, 10, opt),
|
---|
28 | stringify: stringifyNumber.stringifyNumber
|
---|
29 | };
|
---|
30 | const intHex = {
|
---|
31 | identify: value => intIdentify(value) && value >= 0,
|
---|
32 | default: true,
|
---|
33 | tag: 'tag:yaml.org,2002:int',
|
---|
34 | format: 'HEX',
|
---|
35 | test: /^0x[0-9a-fA-F]+$/,
|
---|
36 | resolve: (str, _onError, opt) => intResolve(str, 2, 16, opt),
|
---|
37 | stringify: node => intStringify(node, 16, '0x')
|
---|
38 | };
|
---|
39 |
|
---|
40 | exports.int = int;
|
---|
41 | exports.intHex = intHex;
|
---|
42 | exports.intOct = intOct;
|
---|