source: node_modules/yaml/dist/schema/core/int.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 16 months ago

Initial commit

  • Property mode set to 100644
File size: 1.4 KB
Line 
1'use strict';
2
3var stringifyNumber = require('../../stringify/stringifyNumber.js');
4
5const intIdentify = (value) => typeof value === 'bigint' || Number.isInteger(value);
6const intResolve = (str, offset, radix, { intAsBigInt }) => (intAsBigInt ? BigInt(str) : parseInt(str.substring(offset), radix));
7function 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}
13const 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};
22const 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};
30const 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
40exports.int = int;
41exports.intHex = intHex;
42exports.intOct = intOct;
Note: See TracBrowser for help on using the repository browser.