Brak opisu

Document.d.ts 6.0KB

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