Giter Club home page Giter Club logo

js-yaml-source-map's Introduction

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.

js-yaml-source-map's People

Contributors

tctree333 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.