@stoplight/yaml
Advanced tools
Comparing version 2.1.0 to 2.1.1
{ | ||
"name": "@stoplight/yaml", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "Useful functions when working with YAML.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -74,2 +74,8 @@ "use strict"; | ||
}; | ||
function getLineLength(lineMap, line) { | ||
if (line === 0) { | ||
return Math.max(0, lineMap[0] - 1); | ||
} | ||
return Math.max(0, lineMap[line] - lineMap[line - 1] - 1); | ||
} | ||
const transformErrors = (errors, lineMap) => { | ||
@@ -84,8 +90,8 @@ const validations = []; | ||
start: { | ||
line: error.mark.line - 1, | ||
line: error.mark.line, | ||
character: error.mark.column, | ||
}, | ||
end: { | ||
line: error.mark.line - 1, | ||
character: error.mark.toLineEnd ? lineMap[error.mark.line - 1] : error.mark.column, | ||
line: error.mark.line, | ||
character: error.mark.toLineEnd ? getLineLength(lineMap, error.mark.line) : error.mark.column, | ||
}, | ||
@@ -92,0 +98,0 @@ }, |
@@ -21,3 +21,5 @@ # @stoplight/yaml | ||
- **[parseWithPointers](https://stoplightio.github.io/yaml/globals.html#parsewithpointers)**: Parses YAML into JSON and also returns a source map that includes a JSON path pointer for every property in the result (with line information). | ||
- **[getJsonPathForPosition](https://stoplightio.github.io/yaml/globals.html#getjsonpathforposition)**: Computes JSON path for given position. | ||
- **[getLocationForJsonPath](https://stoplightio.github.io/yaml/globals.html#getlocationforjsonpath)**: Retrieves location of node matching given JSON path. | ||
- **[parseWithPointers](https://stoplightio.github.io/yaml/globals.html#parsewithpointers)**: Parses YAML into JSON and also returns diagnostics as well as full ast with line information. | ||
@@ -31,5 +33,21 @@ ```ts | ||
console.log(result.data); // => the {foo: "bar"} JS object | ||
console.log(result.pointers); // => the source map with a single "#/foo" pointer that has position info for the foo property | ||
``` | ||
```ts | ||
// basic example of getJsonPathForPosition and getLocationForJsonPath | ||
import { getJsonPathForPosition, getLocationForJsonPath, parseWithPointers } from "@stoplight/yaml"; | ||
const result = parseWithPointers(`hello: world | ||
address: | ||
street: 123` | ||
); | ||
const path = getJsonPathForPosition(result, { line: 2, character: 7 }); // line and character are 0-based | ||
console.log(path); // -> ["address", "street"]; | ||
const position = getLocationForJsonPath(result, ['address']); | ||
console.log(position.range.start); // { line: 1, character: 8 } line and character are 0-based | ||
console.log(position.range.end); // { line: 2, character: 15 } line and character are 0-based | ||
``` | ||
### Contributing | ||
@@ -36,0 +54,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40100
385
62