Debian Codegraph @

yaml-types/src/bigint.ts

raw
1 import type { Scalar, ScalarTag } from 'yaml'
2 import { StringifyContext, stringifyString } from 'yaml/util'
3
4 /**
5 * `!bigint` BigInt
6 *
7 * [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) values,
8 * using their conventional `123n` representation.
9 */
10 export const bigint: ScalarTag = {
11 identify: (value: any) => {
12 return typeof value === 'bigint' || value instanceof BigInt
13 },
14 tag: '!bigint',
15 resolve(str: string) {
16 if (str.endsWith('n')) str = str.substring(0, str.length - 1)
17 return BigInt(str)
18 },
19 stringify(item: Scalar, ctx: StringifyContext, onComment, onChompKeep) {
20 if (!bigint.identify?.(item.value)) {
21 throw new TypeError(`${item.value} is not a bigint`)
22 }
23 const value = (item.value as BigInt).toString() + 'n'
24 return stringifyString({ value }, ctx, onComment, onChompKeep)
25 }
26 }