/
Scalar.ts
52 lines (41 loc) · 1.22 KB
/
Scalar.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import type { Type } from '../constants.js'
import { NodeBase, SCALAR } from './Node.js'
import { toJS, ToJSContext } from './toJS.js'
export const isScalarValue = (value: unknown) =>
!value || (typeof value !== 'function' && typeof value !== 'object')
export declare namespace Scalar {
interface Parsed extends Scalar {
range: [number, number]
source: string
}
type Type =
| Type.BLOCK_FOLDED
| Type.BLOCK_LITERAL
| Type.PLAIN
| Type.QUOTE_DOUBLE
| Type.QUOTE_SINGLE
}
export class Scalar<T = unknown> extends NodeBase {
value: T
declare type?: Scalar.Type
/**
* By default (undefined), numbers use decimal notation.
* The YAML 1.2 core schema only supports 'HEX' and 'OCT'.
* The YAML 1.1 schema also supports 'BIN' and 'TIME'
*/
declare format?: string
/** If `value` is a number, use this value when stringifying this node. */
declare minFractionDigits?: number
/** Set during parsing to the source string value */
declare source?: string
constructor(value: T) {
super(SCALAR)
this.value = value
}
toJSON(arg?: any, ctx?: ToJSContext) {
return ctx && ctx.keep ? this.value : toJS(this.value, arg, ctx)
}
toString() {
return String(this.value)
}
}