Schema-807430ba.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. 'use strict';
  2. var _rollupPluginBabelHelpers = require('./_rollupPluginBabelHelpers-eed30217.js');
  3. var stringifyNumber = require('./stringifyNumber-dea1120c.js');
  4. const MERGE_KEY = '<<';
  5. class Merge extends stringifyNumber.Pair {
  6. constructor(pair) {
  7. if (pair instanceof stringifyNumber.Pair) {
  8. let seq = pair.value;
  9. if (!(seq instanceof stringifyNumber.YAMLSeq)) {
  10. seq = new stringifyNumber.YAMLSeq();
  11. seq.items.push(pair.value);
  12. seq.range = pair.value.range;
  13. }
  14. super(pair.key, seq);
  15. this.range = pair.range;
  16. } else {
  17. super(new stringifyNumber.Scalar(MERGE_KEY), new stringifyNumber.YAMLSeq());
  18. }
  19. this.type = stringifyNumber.Pair.Type.MERGE_PAIR;
  20. } // If the value associated with a merge key is a single mapping node, each of
  21. // its key/value pairs is inserted into the current mapping, unless the key
  22. // already exists in it. If the value associated with the merge key is a
  23. // sequence, then this sequence is expected to contain mapping nodes and each
  24. // of these nodes is merged in turn according to its order in the sequence.
  25. // Keys in mapping nodes earlier in the sequence override keys specified in
  26. // later mapping nodes. -- http://yaml.org/type/merge.html
  27. addToJSMap(ctx, map) {
  28. for (const {
  29. source
  30. } of this.value.items) {
  31. if (!(source instanceof stringifyNumber.YAMLMap)) throw new Error('Merge sources must be maps');
  32. const srcMap = source.toJSON(null, ctx, Map);
  33. for (const [key, value] of srcMap) {
  34. if (map instanceof Map) {
  35. if (!map.has(key)) map.set(key, value);
  36. } else if (map instanceof Set) {
  37. map.add(key);
  38. } else if (!Object.prototype.hasOwnProperty.call(map, key)) {
  39. Object.defineProperty(map, key, {
  40. value,
  41. writable: true,
  42. enumerable: true,
  43. configurable: true
  44. });
  45. }
  46. }
  47. }
  48. return map;
  49. }
  50. toString(ctx, onComment) {
  51. const seq = this.value;
  52. if (seq.items.length > 1) return super.toString(ctx, onComment);
  53. this.value = seq.items[0];
  54. const str = super.toString(ctx, onComment);
  55. this.value = seq;
  56. return str;
  57. }
  58. }
  59. function createMap(schema, obj, ctx) {
  60. const {
  61. keepUndefined,
  62. replacer
  63. } = ctx;
  64. const map = new stringifyNumber.YAMLMap(schema);
  65. const add = (key, value) => {
  66. if (typeof replacer === 'function') value = replacer.call(obj, key, value);else if (Array.isArray(replacer) && !replacer.includes(key)) return;
  67. if (value !== undefined || keepUndefined) map.items.push(stringifyNumber.createPair(key, value, ctx));
  68. };
  69. if (obj instanceof Map) {
  70. for (const [key, value] of obj) add(key, value);
  71. } else if (obj && typeof obj === 'object') {
  72. for (const key of Object.keys(obj)) add(key, obj[key]);
  73. }
  74. if (typeof schema.sortMapEntries === 'function') {
  75. map.items.sort(schema.sortMapEntries);
  76. }
  77. return map;
  78. }
  79. const map = {
  80. createNode: createMap,
  81. default: true,
  82. nodeClass: stringifyNumber.YAMLMap,
  83. tag: 'tag:yaml.org,2002:map',
  84. resolve: map => map
  85. };
  86. function createSeq(schema, obj, ctx) {
  87. const {
  88. replacer
  89. } = ctx;
  90. const seq = new stringifyNumber.YAMLSeq(schema);
  91. if (obj && obj[Symbol.iterator]) {
  92. let i = 0;
  93. for (let it of obj) {
  94. if (typeof replacer === 'function') {
  95. const key = obj instanceof Set ? it : String(i++);
  96. it = replacer.call(obj, key, it);
  97. }
  98. seq.items.push(stringifyNumber.createNode(it, null, ctx));
  99. }
  100. }
  101. return seq;
  102. }
  103. const seq = {
  104. createNode: createSeq,
  105. default: true,
  106. nodeClass: stringifyNumber.YAMLSeq,
  107. tag: 'tag:yaml.org,2002:seq',
  108. resolve: seq => seq
  109. };
  110. const string = {
  111. identify: value => typeof value === 'string',
  112. default: true,
  113. tag: 'tag:yaml.org,2002:str',
  114. resolve: str => str,
  115. stringify(item, ctx, onComment, onChompKeep) {
  116. ctx = Object.assign({
  117. actualString: true
  118. }, ctx);
  119. return stringifyNumber.stringifyString(item, ctx, onComment, onChompKeep);
  120. },
  121. options: stringifyNumber.strOptions
  122. };
  123. const failsafe = [map, seq, string];
  124. /* global BigInt */
  125. const intIdentify = value => typeof value === 'bigint' || Number.isInteger(value);
  126. const intResolve = (src, offset, radix) => stringifyNumber.intOptions.asBigInt ? BigInt(src) : parseInt(src.substring(offset), radix);
  127. function intStringify(node, radix, prefix) {
  128. const {
  129. value
  130. } = node;
  131. if (intIdentify(value) && value >= 0) return prefix + value.toString(radix);
  132. return stringifyNumber.stringifyNumber(node);
  133. }
  134. const nullObj = {
  135. identify: value => value == null,
  136. createNode: (schema, value, ctx) => ctx.wrapScalars ? new stringifyNumber.Scalar(null) : null,
  137. default: true,
  138. tag: 'tag:yaml.org,2002:null',
  139. test: /^(?:~|[Nn]ull|NULL)?$/,
  140. resolve: str => {
  141. const node = new stringifyNumber.Scalar(null);
  142. node.sourceStr = str;
  143. return node;
  144. },
  145. options: stringifyNumber.nullOptions,
  146. stringify: ({
  147. sourceStr
  148. }) => sourceStr !== null && sourceStr !== void 0 ? sourceStr : stringifyNumber.nullOptions.nullStr
  149. };
  150. const boolObj = {
  151. identify: value => typeof value === 'boolean',
  152. default: true,
  153. tag: 'tag:yaml.org,2002:bool',
  154. test: /^(?:[Tt]rue|TRUE|[Ff]alse|FALSE)$/,
  155. resolve: str => str[0] === 't' || str[0] === 'T',
  156. options: stringifyNumber.boolOptions,
  157. stringify: ({
  158. value
  159. }) => value ? stringifyNumber.boolOptions.trueStr : stringifyNumber.boolOptions.falseStr
  160. };
  161. const octObj = {
  162. identify: value => intIdentify(value) && value >= 0,
  163. default: true,
  164. tag: 'tag:yaml.org,2002:int',
  165. format: 'OCT',
  166. test: /^0o[0-7]+$/,
  167. resolve: str => intResolve(str, 2, 8),
  168. options: stringifyNumber.intOptions,
  169. stringify: node => intStringify(node, 8, '0o')
  170. };
  171. const intObj = {
  172. identify: intIdentify,
  173. default: true,
  174. tag: 'tag:yaml.org,2002:int',
  175. test: /^[-+]?[0-9]+$/,
  176. resolve: str => intResolve(str, 0, 10),
  177. options: stringifyNumber.intOptions,
  178. stringify: stringifyNumber.stringifyNumber
  179. };
  180. const hexObj = {
  181. identify: value => intIdentify(value) && value >= 0,
  182. default: true,
  183. tag: 'tag:yaml.org,2002:int',
  184. format: 'HEX',
  185. test: /^0x[0-9a-fA-F]+$/,
  186. resolve: str => intResolve(str, 2, 16),
  187. options: stringifyNumber.intOptions,
  188. stringify: node => intStringify(node, 16, '0x')
  189. };
  190. const nanObj = {
  191. identify: value => typeof value === 'number',
  192. default: true,
  193. tag: 'tag:yaml.org,2002:float',
  194. test: /^(?:[-+]?\.(?:inf|Inf|INF|nan|NaN|NAN))$/,
  195. resolve: str => str.slice(-3).toLowerCase() === 'nan' ? NaN : str[0] === '-' ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY,
  196. stringify: stringifyNumber.stringifyNumber
  197. };
  198. const expObj = {
  199. identify: value => typeof value === 'number',
  200. default: true,
  201. tag: 'tag:yaml.org,2002:float',
  202. format: 'EXP',
  203. test: /^[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)[eE][-+]?[0-9]+$/,
  204. resolve: str => parseFloat(str),
  205. stringify: ({
  206. value
  207. }) => Number(value).toExponential()
  208. };
  209. const floatObj = {
  210. identify: value => typeof value === 'number',
  211. default: true,
  212. tag: 'tag:yaml.org,2002:float',
  213. test: /^[-+]?(?:\.[0-9]+|[0-9]+\.[0-9]*)$/,
  214. resolve(str) {
  215. const node = new stringifyNumber.Scalar(parseFloat(str));
  216. const dot = str.indexOf('.');
  217. if (dot !== -1 && str[str.length - 1] === '0') node.minFractionDigits = str.length - dot - 1;
  218. return node;
  219. },
  220. stringify: stringifyNumber.stringifyNumber
  221. };
  222. const core = failsafe.concat([nullObj, boolObj, octObj, intObj, hexObj, nanObj, expObj, floatObj]);
  223. /* global BigInt */
  224. const intIdentify$1 = value => typeof value === 'bigint' || Number.isInteger(value);
  225. const stringifyJSON = ({
  226. value
  227. }) => JSON.stringify(value);
  228. const json = [map, seq, {
  229. identify: value => typeof value === 'string',
  230. default: true,
  231. tag: 'tag:yaml.org,2002:str',
  232. resolve: str => str,
  233. stringify: stringifyJSON
  234. }, {
  235. identify: value => value == null,
  236. createNode: (schema, value, ctx) => ctx.wrapScalars ? new stringifyNumber.Scalar(null) : null,
  237. default: true,
  238. tag: 'tag:yaml.org,2002:null',
  239. test: /^null$/,
  240. resolve: () => null,
  241. stringify: stringifyJSON
  242. }, {
  243. identify: value => typeof value === 'boolean',
  244. default: true,
  245. tag: 'tag:yaml.org,2002:bool',
  246. test: /^true|false$/,
  247. resolve: str => str === 'true',
  248. stringify: stringifyJSON
  249. }, {
  250. identify: intIdentify$1,
  251. default: true,
  252. tag: 'tag:yaml.org,2002:int',
  253. test: /^-?(?:0|[1-9][0-9]*)$/,
  254. resolve: str => stringifyNumber.intOptions.asBigInt ? BigInt(str) : parseInt(str, 10),
  255. stringify: ({
  256. value
  257. }) => intIdentify$1(value) ? value.toString() : JSON.stringify(value)
  258. }, {
  259. identify: value => typeof value === 'number',
  260. default: true,
  261. tag: 'tag:yaml.org,2002:float',
  262. test: /^-?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][-+]?[0-9]+)?$/,
  263. resolve: str => parseFloat(str),
  264. stringify: stringifyJSON
  265. }, {
  266. default: true,
  267. test: /^/,
  268. resolve(str, onError) {
  269. onError(`Unresolved plain scalar ${JSON.stringify(str)}`);
  270. return str;
  271. }
  272. }];
  273. /* global atob, btoa, Buffer */
  274. const binary = {
  275. identify: value => value instanceof Uint8Array,
  276. // Buffer inherits from Uint8Array
  277. default: false,
  278. tag: 'tag:yaml.org,2002:binary',
  279. /**
  280. * Returns a Buffer in node and an Uint8Array in browsers
  281. *
  282. * To use the resulting buffer as an image, you'll want to do something like:
  283. *
  284. * const blob = new Blob([buffer], { type: 'image/jpeg' })
  285. * document.querySelector('#photo').src = URL.createObjectURL(blob)
  286. */
  287. resolve(src, onError) {
  288. if (typeof Buffer === 'function') {
  289. return Buffer.from(src, 'base64');
  290. } else if (typeof atob === 'function') {
  291. // On IE 11, atob() can't handle newlines
  292. const str = atob(src.replace(/[\n\r]/g, ''));
  293. const buffer = new Uint8Array(str.length);
  294. for (let i = 0; i < str.length; ++i) buffer[i] = str.charCodeAt(i);
  295. return buffer;
  296. } else {
  297. onError('This environment does not support reading binary tags; either Buffer or atob is required');
  298. return src;
  299. }
  300. },
  301. options: stringifyNumber.binaryOptions,
  302. stringify: ({
  303. comment,
  304. type,
  305. value
  306. }, ctx, onComment, onChompKeep) => {
  307. let src;
  308. if (typeof Buffer === 'function') {
  309. src = value instanceof Buffer ? value.toString('base64') : Buffer.from(value.buffer).toString('base64');
  310. } else if (typeof btoa === 'function') {
  311. let s = '';
  312. for (let i = 0; i < value.length; ++i) s += String.fromCharCode(value[i]);
  313. src = btoa(s);
  314. } else {
  315. throw new Error('This environment does not support writing binary tags; either Buffer or btoa is required');
  316. }
  317. if (!type) type = stringifyNumber.binaryOptions.defaultType;
  318. if (type === _rollupPluginBabelHelpers.Type.QUOTE_DOUBLE) {
  319. value = src;
  320. } else {
  321. const {
  322. lineWidth
  323. } = stringifyNumber.binaryOptions;
  324. const n = Math.ceil(src.length / lineWidth);
  325. const lines = new Array(n);
  326. for (let i = 0, o = 0; i < n; ++i, o += lineWidth) {
  327. lines[i] = src.substr(o, lineWidth);
  328. }
  329. value = lines.join(type === _rollupPluginBabelHelpers.Type.BLOCK_LITERAL ? '\n' : ' ');
  330. }
  331. return stringifyNumber.stringifyString({
  332. comment,
  333. type,
  334. value
  335. }, ctx, onComment, onChompKeep);
  336. }
  337. };
  338. function parsePairs(seq, onError) {
  339. if (seq instanceof stringifyNumber.YAMLSeq) {
  340. for (let i = 0; i < seq.items.length; ++i) {
  341. let item = seq.items[i];
  342. if (item instanceof stringifyNumber.Pair) continue;else if (item instanceof stringifyNumber.YAMLMap) {
  343. if (item.items.length > 1) onError('Each pair must have its own sequence indicator');
  344. const pair = item.items[0] || new stringifyNumber.Pair();
  345. if (item.commentBefore) pair.commentBefore = pair.commentBefore ? `${item.commentBefore}\n${pair.commentBefore}` : item.commentBefore;
  346. if (item.comment) pair.comment = pair.comment ? `${item.comment}\n${pair.comment}` : item.comment;
  347. item = pair;
  348. }
  349. seq.items[i] = item instanceof stringifyNumber.Pair ? item : new stringifyNumber.Pair(item);
  350. }
  351. } else onError('Expected a sequence for this tag');
  352. return seq;
  353. }
  354. function createPairs(schema, iterable, ctx) {
  355. const {
  356. replacer
  357. } = ctx;
  358. const pairs = new stringifyNumber.YAMLSeq(schema);
  359. pairs.tag = 'tag:yaml.org,2002:pairs';
  360. let i = 0;
  361. for (let it of iterable) {
  362. if (typeof replacer === 'function') it = replacer.call(iterable, String(i++), it);
  363. let key, value;
  364. if (Array.isArray(it)) {
  365. if (it.length === 2) {
  366. key = it[0];
  367. value = it[1];
  368. } else throw new TypeError(`Expected [key, value] tuple: ${it}`);
  369. } else if (it && it instanceof Object) {
  370. const keys = Object.keys(it);
  371. if (keys.length === 1) {
  372. key = keys[0];
  373. value = it[key];
  374. } else throw new TypeError(`Expected { key: value } tuple: ${it}`);
  375. } else {
  376. key = it;
  377. }
  378. pairs.items.push(stringifyNumber.createPair(key, value, ctx));
  379. }
  380. return pairs;
  381. }
  382. const pairs = {
  383. default: false,
  384. tag: 'tag:yaml.org,2002:pairs',
  385. resolve: parsePairs,
  386. createNode: createPairs
  387. };
  388. class YAMLOMap extends stringifyNumber.YAMLSeq {
  389. constructor() {
  390. super();
  391. _rollupPluginBabelHelpers._defineProperty(this, "add", stringifyNumber.YAMLMap.prototype.add.bind(this));
  392. _rollupPluginBabelHelpers._defineProperty(this, "delete", stringifyNumber.YAMLMap.prototype.delete.bind(this));
  393. _rollupPluginBabelHelpers._defineProperty(this, "get", stringifyNumber.YAMLMap.prototype.get.bind(this));
  394. _rollupPluginBabelHelpers._defineProperty(this, "has", stringifyNumber.YAMLMap.prototype.has.bind(this));
  395. _rollupPluginBabelHelpers._defineProperty(this, "set", stringifyNumber.YAMLMap.prototype.set.bind(this));
  396. this.tag = YAMLOMap.tag;
  397. }
  398. toJSON(_, ctx) {
  399. const map = new Map();
  400. if (ctx && ctx.onCreate) ctx.onCreate(map);
  401. for (const pair of this.items) {
  402. let key, value;
  403. if (pair instanceof stringifyNumber.Pair) {
  404. key = stringifyNumber.toJS(pair.key, '', ctx);
  405. value = stringifyNumber.toJS(pair.value, key, ctx);
  406. } else {
  407. key = stringifyNumber.toJS(pair, '', ctx);
  408. }
  409. if (map.has(key)) throw new Error('Ordered maps must not include duplicate keys');
  410. map.set(key, value);
  411. }
  412. return map;
  413. }
  414. }
  415. _rollupPluginBabelHelpers._defineProperty(YAMLOMap, "tag", 'tag:yaml.org,2002:omap');
  416. function parseOMap(seq, onError) {
  417. const pairs = parsePairs(seq, onError);
  418. const seenKeys = [];
  419. for (const {
  420. key
  421. } of pairs.items) {
  422. if (key instanceof stringifyNumber.Scalar) {
  423. if (seenKeys.includes(key.value)) {
  424. onError(`Ordered maps must not include duplicate keys: ${key.value}`);
  425. } else {
  426. seenKeys.push(key.value);
  427. }
  428. }
  429. }
  430. return Object.assign(new YAMLOMap(), pairs);
  431. }
  432. function createOMap(schema, iterable, ctx) {
  433. const pairs = createPairs(schema, iterable, ctx);
  434. const omap = new YAMLOMap();
  435. omap.items = pairs.items;
  436. return omap;
  437. }
  438. const omap = {
  439. identify: value => value instanceof Map,
  440. nodeClass: YAMLOMap,
  441. default: false,
  442. tag: 'tag:yaml.org,2002:omap',
  443. resolve: parseOMap,
  444. createNode: createOMap
  445. };
  446. class YAMLSet extends stringifyNumber.YAMLMap {
  447. constructor(schema) {
  448. super(schema);
  449. this.tag = YAMLSet.tag;
  450. }
  451. add(key) {
  452. const pair = key instanceof stringifyNumber.Pair ? key : new stringifyNumber.Pair(key);
  453. const prev = stringifyNumber.findPair(this.items, pair.key);
  454. if (!prev) this.items.push(pair);
  455. }
  456. get(key, keepPair) {
  457. const pair = stringifyNumber.findPair(this.items, key);
  458. return !keepPair && pair instanceof stringifyNumber.Pair ? pair.key instanceof stringifyNumber.Scalar ? pair.key.value : pair.key : pair;
  459. }
  460. set(key, value) {
  461. if (typeof value !== 'boolean') throw new Error(`Expected boolean value for set(key, value) in a YAML set, not ${typeof value}`);
  462. const prev = stringifyNumber.findPair(this.items, key);
  463. if (prev && !value) {
  464. this.items.splice(this.items.indexOf(prev), 1);
  465. } else if (!prev && value) {
  466. this.items.push(new stringifyNumber.Pair(key));
  467. }
  468. }
  469. toJSON(_, ctx) {
  470. return super.toJSON(_, ctx, Set);
  471. }
  472. toString(ctx, onComment, onChompKeep) {
  473. if (!ctx) return JSON.stringify(this);
  474. if (this.hasAllNullValues()) return super.toString(ctx, onComment, onChompKeep);else throw new Error('Set items must all have null values');
  475. }
  476. }
  477. _rollupPluginBabelHelpers._defineProperty(YAMLSet, "tag", 'tag:yaml.org,2002:set');
  478. function parseSet(map, onError) {
  479. if (map instanceof stringifyNumber.YAMLMap) {
  480. if (map.hasAllNullValues()) return Object.assign(new YAMLSet(), map);else onError('Set items must all have null values');
  481. } else onError('Expected a mapping for this tag');
  482. return map;
  483. }
  484. function createSet(schema, iterable, ctx) {
  485. const {
  486. replacer
  487. } = ctx;
  488. const set = new YAMLSet(schema);
  489. for (let value of iterable) {
  490. if (typeof replacer === 'function') value = replacer.call(iterable, value, value);
  491. set.items.push(stringifyNumber.createPair(value, null, ctx));
  492. }
  493. return set;
  494. }
  495. const set = {
  496. identify: value => value instanceof Set,
  497. nodeClass: YAMLSet,
  498. default: false,
  499. tag: 'tag:yaml.org,2002:set',
  500. resolve: parseSet,
  501. createNode: createSet
  502. };
  503. /* global BigInt */
  504. const parseSexagesimal = (str, isInt) => {
  505. const sign = str[0];
  506. const parts = sign === '-' || sign === '+' ? str.substring(1) : str;
  507. const num = n => isInt && stringifyNumber.intOptions.asBigInt ? BigInt(n) : Number(n);
  508. const res = parts.replace(/_/g, '').split(':').reduce((res, p) => res * num(60) + num(p), num(0));
  509. return sign === '-' ? num(-1) * res : res;
  510. }; // hhhh:mm:ss.sss
  511. const stringifySexagesimal = ({
  512. value
  513. }) => {
  514. let num = n => n;
  515. if (typeof value === 'bigint') num = n => BigInt(n);else if (isNaN(value) || !isFinite(value)) return stringifyNumber.stringifyNumber(value);
  516. let sign = '';
  517. if (value < 0) {
  518. sign = '-';
  519. value *= num(-1);
  520. }
  521. const _60 = num(60);
  522. const parts = [value % _60]; // seconds, including ms
  523. if (value < 60) {
  524. parts.unshift(0); // at least one : is required
  525. } else {
  526. value = (value - parts[0]) / _60;
  527. parts.unshift(value % _60); // minutes
  528. if (value >= 60) {
  529. value = (value - parts[0]) / _60;
  530. parts.unshift(value); // hours
  531. }
  532. }
  533. return sign + parts.map(n => n < 10 ? '0' + String(n) : String(n)).join(':').replace(/000000\d*$/, '') // % 60 may introduce error
  534. ;
  535. };
  536. const intTime = {
  537. identify: value => typeof value === 'bigint' || Number.isInteger(value),
  538. default: true,
  539. tag: 'tag:yaml.org,2002:int',
  540. format: 'TIME',
  541. test: /^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+$/,
  542. resolve: str => parseSexagesimal(str, true),
  543. stringify: stringifySexagesimal
  544. };
  545. const floatTime = {
  546. identify: value => typeof value === 'number',
  547. default: true,
  548. tag: 'tag:yaml.org,2002:float',
  549. format: 'TIME',
  550. test: /^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*$/,
  551. resolve: str => parseSexagesimal(str, false),
  552. stringify: stringifySexagesimal
  553. };
  554. const timestamp = {
  555. identify: value => value instanceof Date,
  556. default: true,
  557. tag: 'tag:yaml.org,2002:timestamp',
  558. // If the time zone is omitted, the timestamp is assumed to be specified in UTC. The time part
  559. // may be omitted altogether, resulting in a date format. In such a case, the time part is
  560. // assumed to be 00:00:00Z (start of day, UTC).
  561. test: RegExp('^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})' + // YYYY-Mm-Dd
  562. '(?:' + // time is optional
  563. '(?:t|T|[ \\t]+)' + // t | T | whitespace
  564. '([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}(\\.[0-9]+)?)' + // Hh:Mm:Ss(.ss)?
  565. '(?:[ \\t]*(Z|[-+][012]?[0-9](?::[0-9]{2})?))?' + // Z | +5 | -03:30
  566. ')?$'),
  567. resolve(str) {
  568. let [, year, month, day, hour, minute, second, millisec, tz] = str.match(timestamp.test);
  569. if (millisec) millisec = (millisec + '00').substr(1, 3);
  570. let date = Date.UTC(year, month - 1, day, hour || 0, minute || 0, second || 0, millisec || 0);
  571. if (tz && tz !== 'Z') {
  572. let d = parseSexagesimal(tz, false);
  573. if (Math.abs(d) < 30) d *= 60;
  574. date -= 60000 * d;
  575. }
  576. return new Date(date);
  577. },
  578. stringify: ({
  579. value
  580. }) => value.toISOString().replace(/((T00:00)?:00)?\.000Z$/, '')
  581. };
  582. /* global BigInt */
  583. const boolStringify = ({
  584. value
  585. }) => value ? stringifyNumber.boolOptions.trueStr : stringifyNumber.boolOptions.falseStr;
  586. const intIdentify$2 = value => typeof value === 'bigint' || Number.isInteger(value);
  587. function intResolve$1(str, offset, radix) {
  588. const sign = str[0];
  589. if (sign === '-' || sign === '+') offset += 1;
  590. str = str.substring(offset).replace(/_/g, '');
  591. if (stringifyNumber.intOptions.asBigInt) {
  592. switch (radix) {
  593. case 2:
  594. str = `0b${str}`;
  595. break;
  596. case 8:
  597. str = `0o${str}`;
  598. break;
  599. case 16:
  600. str = `0x${str}`;
  601. break;
  602. }
  603. const n = BigInt(str);
  604. return sign === '-' ? BigInt(-1) * n : n;
  605. }
  606. const n = parseInt(str, radix);
  607. return sign === '-' ? -1 * n : n;
  608. }
  609. function intStringify$1(node, radix, prefix) {
  610. const {
  611. value
  612. } = node;
  613. if (intIdentify$2(value)) {
  614. const str = value.toString(radix);
  615. return value < 0 ? '-' + prefix + str.substr(1) : prefix + str;
  616. }
  617. return stringifyNumber.stringifyNumber(node);
  618. }
  619. const yaml11 = failsafe.concat([{
  620. identify: value => value == null,
  621. createNode: (schema, value, ctx) => ctx.wrapScalars ? new stringifyNumber.Scalar(null) : null,
  622. default: true,
  623. tag: 'tag:yaml.org,2002:null',
  624. test: /^(?:~|[Nn]ull|NULL)?$/,
  625. resolve: str => {
  626. const node = new stringifyNumber.Scalar(null);
  627. node.sourceStr = str;
  628. return node;
  629. },
  630. options: stringifyNumber.nullOptions,
  631. stringify: ({
  632. sourceStr
  633. }) => sourceStr !== null && sourceStr !== void 0 ? sourceStr : stringifyNumber.nullOptions.nullStr
  634. }, {
  635. identify: value => typeof value === 'boolean',
  636. default: true,
  637. tag: 'tag:yaml.org,2002:bool',
  638. test: /^(?:Y|y|[Yy]es|YES|[Tt]rue|TRUE|[Oo]n|ON)$/,
  639. resolve: () => true,
  640. options: stringifyNumber.boolOptions,
  641. stringify: boolStringify
  642. }, {
  643. identify: value => typeof value === 'boolean',
  644. default: true,
  645. tag: 'tag:yaml.org,2002:bool',
  646. test: /^(?:N|n|[Nn]o|NO|[Ff]alse|FALSE|[Oo]ff|OFF)$/,
  647. resolve: () => false,
  648. options: stringifyNumber.boolOptions,
  649. stringify: boolStringify
  650. }, {
  651. identify: intIdentify$2,
  652. default: true,
  653. tag: 'tag:yaml.org,2002:int',
  654. format: 'BIN',
  655. test: /^[-+]?0b[0-1_]+$/,
  656. resolve: str => intResolve$1(str, 2, 2),
  657. stringify: node => intStringify$1(node, 2, '0b')
  658. }, {
  659. identify: intIdentify$2,
  660. default: true,
  661. tag: 'tag:yaml.org,2002:int',
  662. format: 'OCT',
  663. test: /^[-+]?0[0-7_]+$/,
  664. resolve: str => intResolve$1(str, 1, 8),
  665. stringify: node => intStringify$1(node, 8, '0')
  666. }, {
  667. identify: intIdentify$2,
  668. default: true,
  669. tag: 'tag:yaml.org,2002:int',
  670. test: /^[-+]?[0-9][0-9_]*$/,
  671. resolve: str => intResolve$1(str, 0, 10),
  672. stringify: stringifyNumber.stringifyNumber
  673. }, {
  674. identify: intIdentify$2,
  675. default: true,
  676. tag: 'tag:yaml.org,2002:int',
  677. format: 'HEX',
  678. test: /^[-+]?0x[0-9a-fA-F_]+$/,
  679. resolve: str => intResolve$1(str, 2, 16),
  680. stringify: node => intStringify$1(node, 16, '0x')
  681. }, {
  682. identify: value => typeof value === 'number',
  683. default: true,
  684. tag: 'tag:yaml.org,2002:float',
  685. test: /^[-+]?\.(?:inf|Inf|INF|nan|NaN|NAN)$/,
  686. resolve: str => str.slice(-3).toLowerCase() === 'nan' ? NaN : str[0] === '-' ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY,
  687. stringify: stringifyNumber.stringifyNumber
  688. }, {
  689. identify: value => typeof value === 'number',
  690. default: true,
  691. tag: 'tag:yaml.org,2002:float',
  692. format: 'EXP',
  693. test: /^[-+]?(?:[0-9][0-9_]*)?(?:\.[0-9_]*)?[eE][-+]?[0-9]+$/,
  694. resolve: str => parseFloat(str.replace(/_/g, '')),
  695. stringify: ({
  696. value
  697. }) => Number(value).toExponential()
  698. }, {
  699. identify: value => typeof value === 'number',
  700. default: true,
  701. tag: 'tag:yaml.org,2002:float',
  702. test: /^[-+]?(?:[0-9][0-9_]*)?\.[0-9_]*$/,
  703. resolve(str) {
  704. const node = new stringifyNumber.Scalar(parseFloat(str.replace(/_/g, '')));
  705. const dot = str.indexOf('.');
  706. if (dot !== -1) {
  707. const f = str.substring(dot + 1).replace(/_/g, '');
  708. if (f[f.length - 1] === '0') node.minFractionDigits = f.length;
  709. }
  710. return node;
  711. },
  712. stringify: stringifyNumber.stringifyNumber
  713. }], binary, omap, pairs, set, intTime, floatTime, timestamp);
  714. const schemas = {
  715. core,
  716. failsafe,
  717. json,
  718. yaml11
  719. };
  720. const tags = {
  721. binary,
  722. bool: boolObj,
  723. float: floatObj,
  724. floatExp: expObj,
  725. floatNaN: nanObj,
  726. floatTime,
  727. int: intObj,
  728. intHex: hexObj,
  729. intOct: octObj,
  730. intTime,
  731. map,
  732. null: nullObj,
  733. omap,
  734. pairs,
  735. seq,
  736. set,
  737. timestamp
  738. };
  739. function getSchemaTags(schemas, knownTags, customTags, schemaId) {
  740. let tags = schemas[schemaId.replace(/\W/g, '')]; // 'yaml-1.1' -> 'yaml11'
  741. if (!tags) {
  742. const keys = Object.keys(schemas).map(key => JSON.stringify(key)).join(', ');
  743. throw new Error(`Unknown schema "${schemaId}"; use one of ${keys}`);
  744. }
  745. if (Array.isArray(customTags)) {
  746. for (const tag of customTags) tags = tags.concat(tag);
  747. } else if (typeof customTags === 'function') {
  748. tags = customTags(tags.slice());
  749. }
  750. for (let i = 0; i < tags.length; ++i) {
  751. const tag = tags[i];
  752. if (typeof tag === 'string') {
  753. const tagObj = knownTags[tag];
  754. if (!tagObj) {
  755. const keys = Object.keys(knownTags).map(key => JSON.stringify(key)).join(', ');
  756. throw new Error(`Unknown custom tag "${tag}"; use one of ${keys}`);
  757. }
  758. tags[i] = tagObj;
  759. }
  760. }
  761. return tags;
  762. }
  763. const sortMapEntriesByKey = (a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
  764. const coreKnownTags = {
  765. 'tag:yaml.org,2002:binary': tags.binary,
  766. 'tag:yaml.org,2002:omap': tags.omap,
  767. 'tag:yaml.org,2002:pairs': tags.pairs,
  768. 'tag:yaml.org,2002:set': tags.set,
  769. 'tag:yaml.org,2002:timestamp': tags.timestamp
  770. };
  771. class Schema {
  772. constructor({
  773. customTags,
  774. merge,
  775. resolveKnownTags,
  776. schema,
  777. sortMapEntries
  778. }) {
  779. this.merge = !!merge;
  780. this.name = schema;
  781. this.knownTags = resolveKnownTags ? coreKnownTags : {};
  782. this.tags = getSchemaTags(schemas, tags, customTags, schema); // Used by createNode(), to avoid circular dependencies
  783. this.map = tags.map;
  784. this.seq = tags.seq; // Used by createMap()
  785. this.sortMapEntries = sortMapEntries === true ? sortMapEntriesByKey : sortMapEntries || null;
  786. }
  787. }
  788. exports.MERGE_KEY = MERGE_KEY;
  789. exports.Merge = Merge;
  790. exports.Schema = Schema;