YAMLMap.d.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import type { BlockMap, FlowCollection } from '../parse/cst.js';
  2. import type { Schema } from '../schema/Schema.js';
  3. import type { StringifyContext } from '../stringify/stringify.js';
  4. import { CreateNodeContext } from '../util.js';
  5. import { Collection } from './Collection.js';
  6. import type { ParsedNode, Range } from './Node.js';
  7. import { Pair } from './Pair.js';
  8. import { Scalar } from './Scalar.js';
  9. import type { ToJSContext } from './toJS.js';
  10. export type MapLike = Map<unknown, unknown> | Set<unknown> | Record<string | number | symbol, unknown>;
  11. export declare function findPair<K = unknown, V = unknown>(items: Iterable<Pair<K, V>>, key: unknown): Pair<K, V> | undefined;
  12. export declare namespace YAMLMap {
  13. interface Parsed<K extends ParsedNode = ParsedNode, V extends ParsedNode | null = ParsedNode | null> extends YAMLMap<K, V> {
  14. items: Pair<K, V>[];
  15. range: Range;
  16. srcToken?: BlockMap | FlowCollection;
  17. }
  18. }
  19. export declare class YAMLMap<K = unknown, V = unknown> extends Collection {
  20. static get tagName(): 'tag:yaml.org,2002:map';
  21. items: Pair<K, V>[];
  22. constructor(schema?: Schema);
  23. /**
  24. * A generic collection parsing method that can be extended
  25. * to other node classes that inherit from YAMLMap
  26. */
  27. static from(schema: Schema, obj: unknown, ctx: CreateNodeContext): YAMLMap<unknown, unknown>;
  28. /**
  29. * Adds a value to the collection.
  30. *
  31. * @param overwrite - If not set `true`, using a key that is already in the
  32. * collection will throw. Otherwise, overwrites the previous value.
  33. */
  34. add(pair: Pair<K, V> | {
  35. key: K;
  36. value: V;
  37. }, overwrite?: boolean): void;
  38. delete(key: unknown): boolean;
  39. get(key: unknown, keepScalar: true): Scalar<V> | undefined;
  40. get(key: unknown, keepScalar?: false): V | undefined;
  41. get(key: unknown, keepScalar?: boolean): V | Scalar<V> | undefined;
  42. has(key: unknown): boolean;
  43. set(key: K, value: V): void;
  44. /**
  45. * @param ctx - Conversion context, originally set in Document#toJS()
  46. * @param {Class} Type - If set, forces the returned collection type
  47. * @returns Instance of Type, Map, or Object
  48. */
  49. toJSON<T extends MapLike = Map<unknown, unknown>>(_?: unknown, ctx?: ToJSContext, Type?: {
  50. new (): T;
  51. }): any;
  52. toString(ctx?: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string;
  53. }