UNPKG

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