forked from eemeli/yaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bool.ts
27 lines (24 loc) · 928 Bytes
/
bool.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { Scalar } from '../../nodes/Scalar.js'
import type { StringifyContext } from '../../stringify/stringify.js'
import type { ScalarTag } from '../types.js'
function boolStringify({ value, source }: Scalar, ctx: StringifyContext) {
const boolObj = value ? trueTag : falseTag
if (source && boolObj.test.test(source)) return source
return value ? ctx.options.trueStr : ctx.options.falseStr
}
export const trueTag: ScalarTag & { test: RegExp } = {
identify: value => value === true,
default: true,
tag: 'tag:yaml.org,2002:bool',
test: /^(?:Y|y|[Yy]es|YES|[Tt]rue|TRUE|[Oo]n|ON)$/,
resolve: () => new Scalar(true),
stringify: boolStringify
}
export const falseTag: ScalarTag & { test: RegExp } = {
identify: value => value === false,
default: true,
tag: 'tag:yaml.org,2002:bool',
test: /^(?:N|n|[Nn]o|NO|[Ff]alse|FALSE|[Oo]ff|OFF)$/,
resolve: () => new Scalar(false),
stringify: boolStringify
}