diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..76b8da8 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,77 @@ +declare module 'json-source-map' { + // Options for parsing JSON, allowing for custom behavior such as bigint support and jsonc (JSON with comments). + export interface ParseOptions { + bigint?: boolean; // Specifies if BigInt values should be parsed as native BigInt types. + jsonc?: boolean; // Specifies if JSON with comments (jsonc) should be correctly parsed. + } + + // Options for stringifying values into JSON, allowing for customization of the output. + export interface StringifyOptions { + space?: string | number; // Specifies the indentation for beautifying the output JSON. + es6?: boolean; // Specifies if ES6 features (e.g., Set, Map) are allowed in the output. + } + + // The result of parsing JSON, including the data and pointers to various elements within the JSON string. + export interface ParseResult { + data: any; // The parsed data as a JavaScript object. + pointers: { + [key: string]: { + value: { // The starting position of the value in the original JSON string. + line: number; + column: number; + pos: number; + }; + valueEnd: { // The ending position of the value in the original JSON string. + line: number; + column: number; + pos: number; + }; + key?: { // The starting position of the key in the original JSON string (for object properties). + line: number; + column: number; + pos: number; + }; + keyEnd?: { // The ending position of the key in the original JSON string (for object properties). + line: number; + column: number; + pos: number; + }; + }; + }; + } + + // The result of stringifying a JavaScript value into JSON, including the JSON string and pointers to elements. + export interface StringifyResult { + json: string; // The generated JSON string. + pointers: { + [key: string]: { + value: { // The starting position of the value in the resulting JSON string. + line: number; + column: number; + pos: number; + }; + valueEnd: { // The ending position of the value in the resulting JSON string. + line: number; + column: number; + pos: number; + }; + key?: { // The starting position of the key in the resulting JSON string (for object properties). + line: number; + column: number; + pos: number; + }; + keyEnd?: { // The ending position of the key in the resulting JSON string (for object properties). + line: number; + column: number; + pos: number; + }; + }; + }; + } + + // Parses a JSON string into a JavaScript object, with options for handling BigInt and comments (jsonc). + export function parse(json: string, _?: any, options?: ParseOptions): ParseResult; + + // Converts a JavaScript value to a JSON string, with options for output formatting and ES6 feature inclusion. + export function stringify(value: any, _?: any, options?: string | number | StringifyOptions): StringifyResult; +} diff --git a/package.json b/package.json index 8ba2a8e..a5234dd 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.6.1", "description": "Parse/stringify JSON and provide source-map for JSON-pointers to all nodes", "main": "index.js", + "types": "index.d.ts", "scripts": { "eslint": "eslint index.js spec", "test-spec": "mocha spec -R spec",