Security News
The Dark Side of Open Source
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
data-with-position
Advanced tools
Readme
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 ✅
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
FAQs
Pseudo AST that contains data with its positions attached.
The npm package data-with-position receives a total of 65,583 weekly downloads. As such, data-with-position popularity was classified as popular.
We found that data-with-position demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
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.
Security News
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
Research
Security News
The Socket Research team found this npm package includes code for collecting sensitive developer information, including your operating system username, Git username, and Git email.
Security News
OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.