Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
data-with-position
Advanced tools
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 17,629 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
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.