Socket
Socket
Sign inDemoInstall

data-with-position

Package Overview
Dependencies
1
Maintainers
4
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    data-with-position

Pseudo AST that contains data with its positions attached.


Version published
Weekly downloads
69K
increased by9.29%
Maintainers
4
Install size
425 kB
Created
Weekly downloads
 

Readme

Source

Data with position

Pseudo AST that contains data with its positions attached. Its main goal is to work with the data from YAML and be able to report errors at specific locations. Inspired by pseudo-yaml-ast.

This module is a part of litvis.

TypeScript typings are included ✅

Usage example

import { fromYaml, getPosition, getValue } from "data-with-position";

const dataWithPosition = fromYaml(`obj:
  arr:
  - nums:
    - 1
    - 2
    - 3
    strs:
    - '1'
    - '2'
    - '3'
  num: 1
  str: '1'
`);

console.log(getValue(dataWithPosition.obj.str));
// "1"

console.log(getValue(dataWithPosition.obj.num));
// 1

console.log(getValue(dataWithPosition.obj.arr[0].nums));
// [1, 2, 3]

console.log(getValue(dataWithPosition.obj.arr[0].strs));
// ["1", "2", "3"]

console.log(getPosition(dataWithPosition.obj.str));
// { start: { line: 12, column: 3 }, end: { line: 12, column: 11 } }

console.log(getPosition(dataWithPosition.obj.arr[0]));
// { start: { line: 3, column: 5 }, end: { line: 11, column: 3 } }

Rows and columns in position are 1-indexed for compatibility with unist Position.

Objects and arrays are iterable just like normal JavaScript entities, which means you can write for loops with no need to call getValue() (this function can be expensive near the root of a large tree). In order to detect the kind of a data node, you can use getKind function:

import { fromYaml, getKind, getPosition } from "data-with-position";

const dataWithPosition = fromYaml("...");

if (getKind(dataWithPosition) === "array") {
  for (let i = 0; i < dataWithPosition.length; i += 1) {
    console.log(getPosition(dataWithPosition[i]));
  }
  // or
  for (const element of dataWithPosition) {
    console.log(getPosition(element));
  }
}

if (getKind(dataWithPosition) === "object") {
  for (const key in dataWithPosition) {
    console.log(getPosition(dataWithPosition[key]));
  }
}

The results of getKind() are compatible with kind-of.

import kindOf from "kind-of";
import { getKind, getValue } from "data-with-position";

kindOf(getValue(dataWithPosition.element)) ===
  getKind(dataWithPosition.element);
// always true, even when element is undefined

Keywords

FAQs

Last updated on 30 Sep 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc