Options
All
  • Public
  • Public/Protected
  • All
Menu

@stoplight/yaml

Maintainability Test Coverage

Useful functions when working with YAML.

Installation

Supported in modern browsers and node.

# latest stable
yarn add @stoplight/yaml

Usage

// basic example of parseWithPointers
import { parseWithPointers } from "@stoplight/yaml";

const result = parseWithPointers("foo: bar");

console.log(result.data); // => the {foo: "bar"} JS object
// basic example of getJsonPathForPosition and getLocationForJsonPath
import { getJsonPathForPosition, getLocationForJsonPath, parseWithPointers } from "@stoplight/yaml";

const result = parseWithPointers(`hello: world
address:
    street: 123`
);

const path = getJsonPathForPosition(result, { line: 2, character: 7 }); // line and character are 0-based
console.log(path); // -> ["address", "street"];

const position = getLocationForJsonPath(result, ['address']);
console.log(position.range.start); // { line: 1, character: 8 } line and character are 0-based
console.log(position.range.end); // { line: 2, character: 15 } line and character are 0-based

Contributing

  1. Clone repo.
  2. Create / checkout feature/{name}, chore/{name}, or fix/{name} branch.
  3. Install deps: yarn.
  4. Make your changes.
  5. Run tests: yarn test.prod.
  6. Stage relevant files to git.
  7. Commit: yarn commit. NOTE: Commits that don't follow the conventional format will be rejected. yarn commit creates this format for you, or you can put it together manually and then do a regular git commit.
  8. Push: git push.
  9. Open PR targeting the next branch.

Index

Type aliases

YamlParserResult

YamlParserResult: IParserResult<T, YAMLNode, number[]>

Functions

buildJsonPath

  • buildJsonPath(node: YAMLNode): (string | number)[]

Const getJsonPathForPosition

  • getJsonPathForPosition(__namedParameters: object, __namedParameters: object): undefined | (string | number)[]
  • Parameters

    • __namedParameters: object
    • __namedParameters: object

    Returns undefined | (string | number)[]

Const getLocationForJsonPath

  • getLocationForJsonPath(__namedParameters: object, path: (string | number)[], closest?: undefined | false | true): undefined | ILocation
  • Parameters

    • __namedParameters: object
    • path: (string | number)[]
    • Default value closest: undefined | false | true = false

    Returns undefined | ILocation

Const isValidNode

  • isValidNode(node: YAMLNode): boolean
  • Parameters

    • node: YAMLNode

    Returns boolean

Const lineForPosition

  • lineForPosition(pos: number, lines: number[], start?: number, end?: undefined | number): number
  • A performant way to find the appropriate line for the given position.

    This is key to making the yaml line mapping performant.

    Parameters

    • pos: number
    • lines: number[]
    • Default value start: number = 0
    • Optional end: undefined | number

    Returns number

Const parse

  • parse<T>(value: string): T
  • Type parameters

    • T

    Parameters

    • value: string

    Returns T

Const parseWithPointers

  • parseWithPointers<T>(value: string, options?: LoadOptions): YamlParserResult<T>

Const safeStringify

  • safeStringify(value: any, options?: DumpOptions): string
  • Parameters

    • value: any
    • Optional options: DumpOptions

    Returns string

Const walkAST

  • walkAST(node: YAMLNode | null, duplicatedMappingKeys?: YAMLNode[]): unknown
  • Parameters

    • node: YAMLNode | null
    • Optional duplicatedMappingKeys: YAMLNode[]

    Returns unknown