Document.d.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import type { YAMLError, YAMLWarning } from '../errors.js';
  2. import { Alias } from '../nodes/Alias.js';
  3. import { NODE_TYPE } from '../nodes/identity.js';
  4. import type { Node, NodeType, ParsedNode, Range } from '../nodes/Node.js';
  5. import { Pair } from '../nodes/Pair.js';
  6. import type { Scalar } from '../nodes/Scalar.js';
  7. import type { YAMLMap } from '../nodes/YAMLMap.js';
  8. import type { YAMLSeq } from '../nodes/YAMLSeq.js';
  9. import type { CreateNodeOptions, DocumentOptions, ParseOptions, SchemaOptions, ToJSOptions, ToStringOptions } from '../options.js';
  10. import { Schema } from '../schema/Schema.js';
  11. import { Directives } from './directives.js';
  12. export type Replacer = any[] | ((key: any, value: any) => unknown);
  13. export declare namespace Document {
  14. /** @ts-ignore The typing of directives fails in TS <= 4.2 */
  15. interface Parsed<Contents extends ParsedNode = ParsedNode, Strict extends boolean = true> extends Document<Contents, Strict> {
  16. directives: Directives;
  17. range: Range;
  18. }
  19. }
  20. export declare class Document<Contents extends Node = Node, Strict extends boolean = true> {
  21. readonly [NODE_TYPE]: symbol;
  22. /** A comment before this Document */
  23. commentBefore: string | null;
  24. /** A comment immediately after this Document */
  25. comment: string | null;
  26. /** The document contents. */
  27. contents: Strict extends true ? Contents | null : Contents;
  28. directives: Strict extends true ? Directives | undefined : Directives;
  29. /** Errors encountered during parsing. */
  30. errors: YAMLError[];
  31. options: Required<Omit<ParseOptions & DocumentOptions, '_directives' | 'lineCounter' | 'version'>>;
  32. /**
  33. * The `[start, value-end, node-end]` character offsets for the part of the
  34. * source parsed into this document (undefined if not parsed). The `value-end`
  35. * and `node-end` positions are themselves not included in their respective
  36. * ranges.
  37. */
  38. range?: Range;
  39. /** The schema used with the document. Use `setSchema()` to change. */
  40. schema: Schema;
  41. /** Warnings encountered during parsing. */
  42. warnings: YAMLWarning[];
  43. /**
  44. * @param value - The initial value for the document, which will be wrapped
  45. * in a Node container.
  46. */
  47. constructor(value?: any, options?: DocumentOptions & SchemaOptions & ParseOptions & CreateNodeOptions);
  48. constructor(value: any, replacer: null | Replacer, options?: DocumentOptions & SchemaOptions & ParseOptions & CreateNodeOptions);
  49. /**
  50. * Create a deep copy of this Document and its contents.
  51. *
  52. * Custom Node values that inherit from `Object` still refer to their original instances.
  53. */
  54. clone(): Document<Contents, Strict>;
  55. /** Adds a value to the document. */
  56. add(value: any): void;
  57. /** Adds a value to the document. */
  58. addIn(path: Iterable<unknown>, value: unknown): void;
  59. /**
  60. * Create a new `Alias` node, ensuring that the target `node` has the required anchor.
  61. *
  62. * If `node` already has an anchor, `name` is ignored.
  63. * Otherwise, the `node.anchor` value will be set to `name`,
  64. * or if an anchor with that name is already present in the document,
  65. * `name` will be used as a prefix for a new unique anchor.
  66. * If `name` is undefined, the generated anchor will use 'a' as a prefix.
  67. */
  68. createAlias(node: Strict extends true ? Scalar | YAMLMap | YAMLSeq : Node, name?: string): Alias;
  69. /**
  70. * Convert any value into a `Node` using the current schema, recursively
  71. * turning objects into collections.
  72. */
  73. createNode<T = unknown>(value: T, options?: CreateNodeOptions): NodeType<T>;
  74. createNode<T = unknown>(value: T, replacer: Replacer | CreateNodeOptions | null, options?: CreateNodeOptions): NodeType<T>;
  75. /**
  76. * Convert a key and a value into a `Pair` using the current schema,
  77. * recursively wrapping all values as `Scalar` or `Collection` nodes.
  78. */
  79. createPair<K extends Node = Node, V extends Node = Node>(key: unknown, value: unknown, options?: CreateNodeOptions): Pair<K, V>;
  80. /**
  81. * Removes a value from the document.
  82. * @returns `true` if the item was found and removed.
  83. */
  84. delete(key: unknown): boolean;
  85. /**
  86. * Removes a value from the document.
  87. * @returns `true` if the item was found and removed.
  88. */
  89. deleteIn(path: Iterable<unknown> | null): boolean;
  90. /**
  91. * Returns item at `key`, or `undefined` if not found. By default unwraps
  92. * scalar values from their surrounding node; to disable set `keepScalar` to
  93. * `true` (collections are always returned intact).
  94. */
  95. get(key: unknown, keepScalar?: boolean): Strict extends true ? unknown : any;
  96. /**
  97. * Returns item at `path`, or `undefined` if not found. By default unwraps
  98. * scalar values from their surrounding node; to disable set `keepScalar` to
  99. * `true` (collections are always returned intact).
  100. */
  101. getIn(path: Iterable<unknown> | null, keepScalar?: boolean): Strict extends true ? unknown : any;
  102. /**
  103. * Checks if the document includes a value with the key `key`.
  104. */
  105. has(key: unknown): boolean;
  106. /**
  107. * Checks if the document includes a value at `path`.
  108. */
  109. hasIn(path: Iterable<unknown> | null): boolean;
  110. /**
  111. * Sets a value in this document. For `!!set`, `value` needs to be a
  112. * boolean to add/remove the item from the set.
  113. */
  114. set(key: any, value: unknown): void;
  115. /**
  116. * Sets a value in this document. For `!!set`, `value` needs to be a
  117. * boolean to add/remove the item from the set.
  118. */
  119. setIn(path: Iterable<unknown> | null, value: unknown): void;
  120. /**
  121. * Change the YAML version and schema used by the document.
  122. * A `null` version disables support for directives, explicit tags, anchors, and aliases.
  123. * It also requires the `schema` option to be given as a `Schema` instance value.
  124. *
  125. * Overrides all previously set schema options.
  126. */
  127. setSchema(version: '1.1' | '1.2' | 'next' | null, options?: SchemaOptions): void;
  128. /** A plain JavaScript representation of the document `contents`. */
  129. toJS(opt?: ToJSOptions & {
  130. [ignored: string]: unknown;
  131. }): any;
  132. /**
  133. * A JSON representation of the document `contents`.
  134. *
  135. * @param jsonArg Used by `JSON.stringify` to indicate the array index or
  136. * property name.
  137. */
  138. toJSON(jsonArg?: string | null, onAnchor?: ToJSOptions['onAnchor']): any;
  139. /** A YAML representation of the document. */
  140. toString(options?: ToStringOptions): string;
  141. }