js-yaml-source-map
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published

js-yaml sourcemaps

A library for finding YAML source locations after parsing by js-yaml.

Installation

npm install js-yaml-source-map js-yaml

Usage

---
# file: example.yaml
fruits:
  - apple
  - banana
  - orange
people:
  - name: Eric
    age: 26
  - name: Lily
    age: 22
states:
  CA: California
  NY: New York
    capital: Albany
  TX: Texas
import fs from "fs";
import yaml from "js-yaml";
import SourceMap from "js-yaml-source-map";

const data = fs.readFileSync("./example.yaml", "utf8");

const map = new SourceMap();
// pass map.listen() to the listener option
const loaded = yaml.load(data, { listener: map.listen() });

console.log(loaded); // { fruits: [ 'apple', 'banana', 'orange' ], ... }

// different syntaxes supported
console.log(map.lookup("fruits")); // { line: 4, column: 10, position: 42 }
console.log(map.lookup("people.0.age")); // { line: 9, column: 8, position: 95 }
console.log(map.lookup(".people[1].name")); // { line: 10, column: 9, position: 108}
console.log(map.lookup(["states", "NY", "capital"])); // { line: 16, column: 12, position: 188 }

If you're using CommonJS, you'll need to access the default key:

const SourceMap = require("js-yaml-source-map").default;

const map = new SourceMap();

//...

API Reference

SourceMap

Constructor: new SourceMap()

Properties:

  • SourceMap().map: PathMap

Methods:

  • SourceMap().listen(): (event: "open" | "close", state: State) => void
  • SourceMap().lookup(path: string | string[]): SourceLocation | undefined

Types

interface PathMap {
  [path: string]: {
    line: number;
    position: number;
    lineStart: number;
  };
}

interface SourceLocation {
  line: number;
  column: number;
  position: number;
}

Limitations

  • This library does not work with multi-document sources and yaml.loadAll(). Using it with yaml.loadAll() will result in undefined behavior.
  • Using arrays or objects as keys will not work properly, and will result in undefined behavior.

Package Sidebar

Install

npm i js-yaml-source-map

Weekly Downloads

866

Version

0.2.2

License

MIT

Unpacked Size

25 kB

Total Files

9

Last publish

Collaborators

  • person_v1.32