jsonc-parser
Advanced tools
Comparing version 1.0.0 to 1.0.1
1.0.1 | ||
================== | ||
- added the *format* API: computes edits to format a JSON document. | ||
- added the *modify* API: computes edits to insert, remove or replace a property or value in a JSON document. | ||
- added the *allyEdits* API: applies edits to a document | ||
1.0.0 | ||
@@ -3,0 +9,0 @@ ================== |
@@ -143,3 +143,9 @@ export declare enum ScanError { | ||
export declare function parseTree(text: string, errors?: ParseError[], options?: ParseOptions): Node; | ||
export declare function findNodeAtLocation(root: Node, path: JSONPath): Node; | ||
/** | ||
* Finds the node at the given path in a JSON DOM. | ||
*/ | ||
export declare function findNodeAtLocation(root: Node, path: JSONPath): Node | undefined; | ||
/** | ||
* Evaluates the JavaScript object of the given JSON DOM node | ||
*/ | ||
export declare function getNodeValue(node: Node): any; | ||
@@ -184,1 +190,91 @@ /** | ||
} | ||
/** | ||
* Represents a text modification | ||
*/ | ||
export interface Edit { | ||
/** | ||
* The start offset of the modification. | ||
*/ | ||
offset: number; | ||
/** | ||
* The length of the modification. Must not be negative. Empty length represents an *insert*. | ||
*/ | ||
length: number; | ||
/** | ||
* The new content. Empty content represents a *remove*. | ||
*/ | ||
content: string; | ||
} | ||
/** | ||
* A text range in the document | ||
*/ | ||
export interface Range { | ||
/** | ||
* The start offset of the range. | ||
*/ | ||
offset: number; | ||
/** | ||
* The length of the range. Must not be negative. | ||
*/ | ||
length: number; | ||
} | ||
export interface FormattingOptions { | ||
/** | ||
* If indentation is based on spaces (`insertSpaces` = true), then what is the number of spaces that make an indent? | ||
*/ | ||
tabSize: number; | ||
/** | ||
* Is indentation based on spaces? | ||
*/ | ||
insertSpaces: boolean; | ||
/** | ||
* The default 'end of line' character | ||
*/ | ||
eol: string; | ||
} | ||
/** | ||
* Computes the edits needed to format a JSON document. | ||
* | ||
* @param documentText The input text | ||
* @param range The range to format or `undefined` to format the full content | ||
* @param options The formatting options | ||
* @returns A list of edit operations describing the formatting changes to the original document. Edits can be either inserts, replacements or | ||
* removals of text segments. All offsets refer to the original state of the document. No two edits must change or remove the same range of | ||
* text in the original document. However, multiple edits can have | ||
* the same offset, for example multiple inserts, or an insert followed by a remove or replace. The order in the array defines which edit is applied first. | ||
* To apply edits to an input, you can use `applyEdits` | ||
*/ | ||
export declare function format(documentText: string, range: Range, options: FormattingOptions): Edit[]; | ||
/** | ||
* Options used when computing the modification edits | ||
*/ | ||
export interface ModificationOptions { | ||
/** | ||
* Formatting options | ||
*/ | ||
formattingOptions: FormattingOptions; | ||
/** | ||
* Optional fucntion to define the insertion index given an existing list of properties. | ||
*/ | ||
getInsertionIndex?: (properties: string[]) => number; | ||
} | ||
/** | ||
* Computes the edits needed to modify a value in the JSON document. | ||
* | ||
* @param documentText The input text | ||
* @param path The path of the value to change. The path represents either to the document root, a property or an array item. | ||
* If the path points to an non-existing property or item, it will be created. | ||
* @param value The new value for the specified property or item. If the value is undefined, | ||
* the property or item will be removed. | ||
* @param options Options | ||
* @returns A list of edit operations describing the formatting changes to the original document. Edits can be either inserts, replacements or | ||
* removals of text segments. All offsets refer to the original state of the document. No two edits must change or remove the same range of | ||
* text in the original document. However, multiple edits can have | ||
* the same offset, for example multiple inserts, or an insert followed by a remove or replace. The order in the array defines which edit is applied first. | ||
* To apply edits to an input, you can use `applyEdits` | ||
*/ | ||
export declare function modify(text: string, path: JSONPath, value: any, options: ModificationOptions): Edit[]; | ||
/** | ||
* Applies edits to a input string. | ||
*/ | ||
export declare function applyEdits(text: string, edits: Edit[]): string; |
@@ -7,3 +7,3 @@ (function (factory) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
define(["require", "exports", "./format", "./edit"], factory); | ||
} | ||
@@ -16,2 +16,5 @@ })(function (require, exports) { | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var format_1 = require("./format"); | ||
var edit_1 = require("./edit"); | ||
var ScanError; | ||
@@ -199,2 +202,3 @@ (function (ScanError) { | ||
scanError = ScanError.InvalidCharacter; | ||
// mark as error but continue with string | ||
} | ||
@@ -446,6 +450,6 @@ } | ||
var previousNodeInst = { | ||
value: void 0, | ||
offset: void 0, | ||
length: void 0, | ||
type: void 0 | ||
value: {}, | ||
offset: 0, | ||
length: 0, | ||
type: 'object' | ||
}; | ||
@@ -515,3 +519,3 @@ var isAtPropertyKey = false; | ||
} | ||
if (sep === ':' && previousNode.type === 'property') { | ||
if (sep === ':' && previousNode && previousNode.type === 'property') { | ||
previousNode.columnOffset = offset; | ||
@@ -672,2 +676,5 @@ isAtPropertyKey = false; | ||
exports.parseTree = parseTree; | ||
/** | ||
* Finds the node at the given path in a JSON DOM. | ||
*/ | ||
function findNodeAtLocation(root, path) { | ||
@@ -681,3 +688,3 @@ if (!root) { | ||
if (typeof segment === 'string') { | ||
if (node.type !== 'object') { | ||
if (node.type !== 'object' || !Array.isArray(node.children)) { | ||
return void 0; | ||
@@ -688,3 +695,3 @@ } | ||
var propertyNode = _b[_a]; | ||
if (propertyNode.children[0].value === segment) { | ||
if (Array.isArray(propertyNode.children) && propertyNode.children[0].value === segment) { | ||
node = propertyNode.children[1]; | ||
@@ -701,3 +708,3 @@ found = true; | ||
var index = segment; | ||
if (node.type !== 'array' || index < 0 || index >= node.children.length) { | ||
if (node.type !== 'array' || index < 0 || !Array.isArray(node.children) || index >= node.children.length) { | ||
return void 0; | ||
@@ -711,2 +718,5 @@ } | ||
exports.findNodeAtLocation = findNodeAtLocation; | ||
/** | ||
* Evaluates the JavaScript object of the given JSON DOM node | ||
*/ | ||
function getNodeValue(node) { | ||
@@ -717,3 +727,3 @@ if (node.type === 'array') { | ||
else if (node.type === 'object') { | ||
var obj = {}; | ||
var obj = Object.create(null); | ||
for (var _i = 0, _a = node.children; _i < _a.length; _i++) { | ||
@@ -928,3 +938,48 @@ var prop = _a[_i]; | ||
exports.visit = visit; | ||
/** | ||
* Computes the edits needed to format a JSON document. | ||
* | ||
* @param documentText The input text | ||
* @param range The range to format or `undefined` to format the full content | ||
* @param options The formatting options | ||
* @returns A list of edit operations describing the formatting changes to the original document. Edits can be either inserts, replacements or | ||
* removals of text segments. All offsets refer to the original state of the document. No two edits must change or remove the same range of | ||
* text in the original document. However, multiple edits can have | ||
* the same offset, for example multiple inserts, or an insert followed by a remove or replace. The order in the array defines which edit is applied first. | ||
* To apply edits to an input, you can use `applyEdits` | ||
*/ | ||
function format(documentText, range, options) { | ||
return format_1.format(documentText, range, options); | ||
} | ||
exports.format = format; | ||
/** | ||
* Computes the edits needed to modify a value in the JSON document. | ||
* | ||
* @param documentText The input text | ||
* @param path The path of the value to change. The path represents either to the document root, a property or an array item. | ||
* If the path points to an non-existing property or item, it will be created. | ||
* @param value The new value for the specified property or item. If the value is undefined, | ||
* the property or item will be removed. | ||
* @param options Options | ||
* @returns A list of edit operations describing the formatting changes to the original document. Edits can be either inserts, replacements or | ||
* removals of text segments. All offsets refer to the original state of the document. No two edits must change or remove the same range of | ||
* text in the original document. However, multiple edits can have | ||
* the same offset, for example multiple inserts, or an insert followed by a remove or replace. The order in the array defines which edit is applied first. | ||
* To apply edits to an input, you can use `applyEdits` | ||
*/ | ||
function modify(text, path, value, options) { | ||
return edit_1.setProperty(text, path, value, options.formattingOptions, options.getInsertionIndex); | ||
} | ||
exports.modify = modify; | ||
/** | ||
* Applies edits to a input string. | ||
*/ | ||
function applyEdits(text, edits) { | ||
for (var i = edits.length - 1; i >= 0; i--) { | ||
text = edit_1.applyEdit(text, edits[i]); | ||
} | ||
return text; | ||
} | ||
exports.applyEdits = applyEdits; | ||
}); | ||
//# sourceMappingURL=main.js.map |
{ | ||
"name": "jsonc-parser", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Scanner and parser for JSON with comments.", | ||
@@ -18,8 +18,9 @@ "main": "./lib/main.js", | ||
"mocha": "^2.4.5", | ||
"typescript": "^2.1.5", | ||
"@types/node": "^6.0.46", | ||
"@types/mocha": "^2.2.32" | ||
"typescript": "^2.7.1", | ||
"@types/node": "^7.0.43", | ||
"@types/mocha": "^2.2.32", | ||
"tslint": "^5.9.1" | ||
}, | ||
"scripts": { | ||
"prepublish": "tsc -p ./src", | ||
"prepare": "tsc -p ./src", | ||
"compile": "tsc -p ./src", | ||
@@ -26,0 +27,0 @@ "watch": "tsc -w -p ./src", |
103
README.md
@@ -12,5 +12,10 @@ # jsonc-parser | ||
- the *scanner* tokenizes the input string into tokens and token offsets | ||
- the *parse* function evaluates the JavaScipt object represented by JSON string in a fault tolerant fashion. | ||
- the *visit* function implements a 'SAX' style parser with callbacks for the encountered properties and values. | ||
- the *getLocation* API returns a path that describes the location of a given offset in a given file. | ||
- the *parseTree* function computes a hierarchical DOM with offsets representing the encountered properties and values. | ||
- the *parse* function evaluates the JavaScipt object represented by JSON string in a fault tolerant fashion. | ||
- the *getLocation* API returns a location object that describes the property or value located at a given offset in a JSON document. | ||
- ths *findNodeAtLocation* API finds the node at a given location path in a JSON DOM. | ||
- the *format* API computes edits to format a JSON document. | ||
- the *modify* API computes edits to insert, remove or replace a property or value in a JSON document. | ||
- the *applyEdits* API applies edits to a document. | ||
@@ -181,4 +186,96 @@ Installation | ||
/** | ||
* Finds the node at the given path in a JSON DOM. | ||
*/ | ||
export function findNodeAtLocation(root: Node, path: JSONPath): Node | undefined; | ||
/** | ||
* Evaluates the JavaScript object of the given JSON DOM node | ||
*/ | ||
export function getNodeValue(node: Node): any; | ||
/** | ||
* Computes the edits needed to format a JSON document. | ||
* | ||
* @param documentText The input text | ||
* @param range The range to format or `undefined` to format the full content | ||
* @param options The formatting options | ||
* @returns A list of edit operations describing the formatting changes to the original document. Edits can be either inserts, replacements or | ||
* removals of text segments. All offsets refer to the original state of the document. No two edits must change or remove the same range of | ||
* text in the original document. However, multiple edits can have | ||
* the same offset, for example multiple inserts, or an insert followed by a remove or replace. The order in the array defines which edit is applied first. | ||
* To apply edits to an input, you can use `applyEdits` | ||
*/ | ||
export function format(documentText: string, range: Range, options: FormattingOptions): Edit[]; | ||
/** | ||
* Computes the edits needed to modify a value in the JSON document. | ||
* | ||
* @param documentText The input text | ||
* @param path The path of the value to change. The path represents either to the document root, a property or an array item. | ||
* If the path points to an non-existing property or item, it will be created. | ||
* @param value The new value for the specified property or item. If the value is undefined, | ||
* the property or item will be removed. | ||
* @param options Options | ||
* @returns A list of edit operations describing the formatting changes to the original document. Edits can be either inserts, replacements or | ||
* removals of text segments. All offsets refer to the original state of the document. No two edits must change or remove the same range of | ||
* text in the original document. However, multiple edits can have | ||
* the same offset, for example multiple inserts, or an insert followed by a remove or replace. The order in the array defines which edit is applied first. | ||
* To apply edits to an input, you can use `applyEdits` | ||
*/ | ||
export function modify(text: string, path: JSONPath, value: any, options: ModificationOptions): Edit[]; | ||
/** | ||
* Applies edits to a input string. | ||
*/ | ||
export function applyEdits(text: string, edits: Edit[]): string; | ||
/** | ||
* Represents a text modification | ||
*/ | ||
export interface Edit { | ||
/** | ||
* The start offset of the modification. | ||
*/ | ||
offset: number; | ||
/** | ||
* The length of the modification. Must not be negative. Empty length represents an *insert*. | ||
*/ | ||
length: number; | ||
/** | ||
* The new content. Empty content represents a *remove*. | ||
*/ | ||
content: string; | ||
} | ||
/** | ||
* A text range in the document | ||
*/ | ||
export interface Range { | ||
/** | ||
* The start offset of the range. | ||
*/ | ||
offset: number; | ||
/** | ||
* The length of the range. Must not be negative. | ||
*/ | ||
length: number; | ||
} | ||
export interface FormattingOptions { | ||
/** | ||
* If indentation is based on spaces (`insertSpaces` = true), then what is the number of spaces that make an indent? | ||
*/ | ||
tabSize: number; | ||
/** | ||
* Is indentation based on spaces? | ||
*/ | ||
insertSpaces: boolean; | ||
/** | ||
* The default 'end of line' character | ||
*/ | ||
eol: string; | ||
} | ||
``` | ||
@@ -192,2 +289,2 @@ | ||
Copyright 2016, Microsoft | ||
Copyright 2018, Microsoft |
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
86176
13
1648
287
5