UNPKG

6.15 kBTypeScriptView Raw
1// Type definitions for js-yaml 4.0
2// Project: https://github.com/nodeca/js-yaml
3// Definitions by: Bart van der Schoor <https://github.com/Bartvds>
4// Sebastian Clausen <https://github.com/sclausen>
5// ExE Boss <https://github.com/ExE-Boss>
6// Armaan Tobaccowalla <https://github.com/ArmaanT>
7// Linus Unnebäck <https://github.com/LinusU>
8// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
9// TypeScript Version: 2.2
10
11export as namespace jsyaml;
12
13export function load(str: string, opts?: LoadOptions): unknown;
14
15export class Type {
16 constructor(tag: string, opts?: TypeConstructorOptions);
17 kind: 'sequence' | 'scalar' | 'mapping' | null;
18 resolve(data: any): boolean;
19 construct(data: any, type?: string): any;
20 instanceOf: object | null;
21 predicate: ((data: object) => boolean) | null;
22 represent: ((data: object) => any) | { [x: string]: (data: object) => any } | null;
23 representName: ((data: object) => any) | null;
24 defaultStyle: string | null;
25 multi: boolean;
26 styleAliases: { [x: string]: any };
27}
28
29export class Schema {
30 constructor(definition: SchemaDefinition | Type[] | Type);
31 extend(types: SchemaDefinition | Type[] | Type): Schema;
32}
33
34export function loadAll(str: string, iterator?: null, opts?: LoadOptions): unknown[];
35export function loadAll(str: string, iterator: (doc: unknown) => void, opts?: LoadOptions): void;
36
37export function dump(obj: any, opts?: DumpOptions): string;
38
39export interface LoadOptions {
40 /** string to be used as a file path in error/warning messages. */
41 filename?: string | undefined;
42 /** function to call on warning messages. */
43 onWarning?(this: null, e: YAMLException): void;
44 /** specifies a schema to use. */
45 schema?: Schema | undefined;
46 /** compatibility with JSON.parse behaviour. */
47 json?: boolean | undefined;
48 /** listener for parse events */
49 listener?(this: State, eventType: EventType, state: State): void;
50}
51
52export type EventType = 'open' | 'close';
53
54export interface State {
55 input: string;
56 filename: string | null;
57 schema: Schema;
58 onWarning: (this: null, e: YAMLException) => void;
59 json: boolean;
60 length: number;
61 position: number;
62 line: number;
63 lineStart: number;
64 lineIndent: number;
65 version: null | number;
66 checkLineBreaks: boolean;
67 kind: string;
68 result: any;
69 implicitTypes: Type[];
70}
71
72export interface DumpOptions {
73 /** indentation width to use (in spaces). */
74 indent?: number | undefined;
75 /** when true, will not add an indentation level to array elements */
76 noArrayIndent?: boolean | undefined;
77 /** do not throw on invalid types (like function in the safe schema) and skip pairs and single values with such types. */
78 skipInvalid?: boolean | undefined;
79 /** specifies level of nesting, when to switch from block to flow style for collections. -1 means block style everwhere */
80 flowLevel?: number | undefined;
81 /** Each tag may have own set of styles. - "tag" => "style" map. */
82 styles?: { [x: string]: any } | undefined;
83 /** specifies a schema to use. */
84 schema?: Schema | undefined;
85 /** if true, sort keys when dumping YAML. If a function, use the function to sort the keys. (default: false) */
86 sortKeys?: boolean | ((a: any, b: any) => number) | undefined;
87 /** set max line width. (default: 80) */
88 lineWidth?: number | undefined;
89 /** if true, don't convert duplicate objects into references (default: false) */
90 noRefs?: boolean | undefined;
91 /** if true don't try to be compatible with older yaml versions. Currently: don't quote "yes", "no" and so on, as required for YAML 1.1 (default: false) */
92 noCompatMode?: boolean | undefined;
93 /**
94 * if true flow sequences will be condensed, omitting the space between `key: value` or `a, b`. Eg. `'[a,b]'` or `{a:{b:c}}`.
95 * Can be useful when using yaml for pretty URL query params as spaces are %-encoded. (default: false).
96 */
97 condenseFlow?: boolean | undefined;
98 /** strings will be quoted using this quoting style. If you specify single quotes, double quotes will still be used for non-printable characters. (default: `'`) */
99 quotingType?: "'" | '"' | undefined;
100 /** if true, all non-key strings will be quoted even if they normally don't need to. (default: false) */
101 forceQuotes?: boolean | undefined;
102 /** callback `function (key, value)` called recursively on each key/value in source object (see `replacer` docs for `JSON.stringify`). */
103 replacer?: ((key: string, value: any) => any) | undefined;
104}
105
106export interface TypeConstructorOptions {
107 kind?: 'sequence' | 'scalar' | 'mapping' | undefined;
108 resolve?: ((data: any) => boolean) | undefined;
109 construct?: ((data: any, type?: string) => any) | undefined;
110 instanceOf?: object | undefined;
111 predicate?: ((data: object) => boolean) | undefined;
112 represent?: ((data: object) => any) | { [x: string]: (data: object) => any } | undefined;
113 representName?: ((data: object) => any) | undefined;
114 defaultStyle?: string | undefined;
115 multi?: boolean | undefined;
116 styleAliases?: { [x: string]: any } | undefined;
117}
118
119export interface SchemaDefinition {
120 implicit?: Type[] | undefined;
121 explicit?: Type[] | undefined;
122}
123
124/** only strings, arrays and plain objects: http://www.yaml.org/spec/1.2/spec.html#id2802346 */
125export let FAILSAFE_SCHEMA: Schema;
126/** only strings, arrays and plain objects: http://www.yaml.org/spec/1.2/spec.html#id2802346 */
127export let JSON_SCHEMA: Schema;
128/** same as JSON_SCHEMA: http://www.yaml.org/spec/1.2/spec.html#id2804923 */
129export let CORE_SCHEMA: Schema;
130/** all supported YAML types */
131export let DEFAULT_SCHEMA: Schema;
132
133export interface Mark {
134 buffer: string;
135 column: number;
136 line: number;
137 name: string;
138 position: number;
139 snippet: string;
140}
141
142export class YAMLException extends Error {
143 constructor(reason?: string, mark?: Mark);
144
145 toString(compact?: boolean): string;
146
147 name: string;
148
149 reason: string;
150
151 message: string;
152
153 mark: Mark;
154}
155
\No newline at end of file