From 47a20469245d7c747192ac25ab769e8d23e2b5ac Mon Sep 17 00:00:00 2001 From: Elizabeth Craig Date: Wed, 28 Nov 2018 18:37:23 -0800 Subject: [PATCH 1/2] Add definitions for jju --- types/jju/index.d.ts | 213 ++++++++++++++++++++++++++++++++++++++++ types/jju/jju-tests.ts | 4 + types/jju/tsconfig.json | 23 +++++ types/jju/tslint.json | 1 + 4 files changed, 241 insertions(+) create mode 100644 types/jju/index.d.ts create mode 100644 types/jju/jju-tests.ts create mode 100644 types/jju/tsconfig.json create mode 100644 types/jju/tslint.json diff --git a/types/jju/index.d.ts b/types/jju/index.d.ts new file mode 100644 index 0000000000..46fbf2f77b --- /dev/null +++ b/types/jju/index.d.ts @@ -0,0 +1,213 @@ +// Type definitions for jju 1.4 +// Project: https://github.com/rlidwka/jju +// Definitions by: Elizabeth Craig +// Alex Kocharin +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +// Disabling unified-signatures rule so different documentation can be provided for each signature. +// tslint:disable:unified-signatures + +export interface ParseOptions { + /** + * What to do with reserved keys (default 'ignore'). + * - "ignore" - ignore reserved keys + * - "throw" - throw SyntaxError in case of reserved keys + * - "replace" - replace reserved keys, this is the default JSON.parse behaviour, unsafe + */ + reserved_keys?: 'ignore' | 'throw' | 'replace'; + + /** + * Create object as `Object.create(null)` instead of `{}`. + * - If reserved_keys != 'replace', default is false. + * - If reserved_keys == 'replace', default is true. + * + * It is usually unsafe and not recommended to change this option to false in the last case. + */ + null_prototype?: boolean; + + /** + * Reviver function (follows the JSON spec). This function is called for each member of the object. + * If a member contains nested objects, the nested objects are transformed before the parent object is. + */ + reviver?: (key: any, value: any) => any; + + /** + * Operation mode (default 'json5'). Set to 'json' if you want to throw on non-strict json files. + */ + mode?: 'json5' | 'json' | 'cjson'; +} + +export interface StringifyOptions { + /** + * Output ascii only (default false). + * If this option is enabled, output will not have any characters except 0x20-0x7f. + */ + ascii?: boolean; + + /** + * This option follows JSON specification. + * @default '\t' + */ + indent?: string | number | boolean; + + /** + * Enquoting char. + * - If `mode` is 'json', default is " + * - Otherwise, default is ' + */ + quote?: '"' | "'"; + + /** + * Whether keys quoting in objects is required or not. + * If you want `{"q": 1}` instead of `{q: 1}`, set it to true. + * - If `mode` is 'json', default is true + * - Otherwise, default is false + */ + quote_keys?: boolean; + + /** + * Sort all keys while stringifying. + * By default sort order will depend on implementation--with v8 it's insertion order. + * If set to true, all keys (but not arrays) will be sorted alphabetically. + * You can provide your own sorting function as well. + * @default false + */ + sort_keys?: boolean | ((a: any, b: any) => number); + + /** + * Replacer function or array. This option follows JSON specification. + * If a function, used to transform the results. + * If an array, acts as a approved list for selecting the object properties that will be stringified. + */ + replacer?: ((key: string, value: any) => any) | Array; + + /** + * Don't output trailing comma. If this option is set, arrays like `[1,2,3,]` will never be generated. + * Otherwise they may be generated for pretty printing. + * - If `mode` is JSON, default is true + * - Otherwise, default is false + */ + no_trailing_comma?: boolean; + + /** + * Operation mode. Set it to 'json' if you want correct json in the output. + * If it is 'json', following options are implied: + * - options.quote = '"' + * - options.no_trailing_comma = true + * - options.quote_keys = true + * - '\x' literals are not used + */ + mode?: 'json' | 'json5' | 'cjson'; +} + +/** + * Represents a token in a JSON file. + */ +export interface Token { + /** Raw text of this token. If you join all raws, you will get the original document. */ + raw: string; + /** Type of the token. */ + type: 'whitespace' | 'comment' | 'key' | 'literal' | 'separator' | 'newline'; + /** Path to the current token in the syntax tree. */ + stack: string[]; + /** Value of the token if token is a key or literal. */ + value?: any; +} + +/** + * Object defining a programming style in which the JSON document was written. + */ +export interface JsonStyle { + /** Preferred indentation. */ + indent: string; + /** Preferred newline. */ + newline: string; + /** " or ' depending on which quote is preferred. */ + quote: string; + /** True if unquoted keys were used at least once. */ + quote_keys: boolean; + /** True if input has a whitespace token. */ + has_whitespace: boolean; + /** True if input has a comment token. */ + has_comments: boolean; + /** True if input has a newline token. */ + has_newlines: boolean; + /** True if input has at least one trailing comma. */ + has_trailing_comma: boolean; +} + +/** + * Parse json/json5 text and returns a javascript value it corresponds to. + * @param text Text to parse + * @param options Parser options + */ +export function parse(text: string, options?: ParseOptions): any; +/** + * Compatibility syntax (follows JSON specification). + * Converts a JavaScript Object Notation (JSON) string into an object. + * @param text A valid JSON string. + * @param reviver A function that transforms the results. This function is called for each member of the object. + * If a member contains nested objects, the nested objects are transformed before the parent object is. + */ +export function parse(text: string, reviver?: (key: any, value: any) => any): any; + +/** + * Convert javascript value to an appropriate json/json5 text. + * @param value Value to serialize + * @param options Serializer options + */ +export function stringify(value: any, options?: StringifyOptions): string; +/** + * Compatibility syntax (follows JSON specification). + * Converts a JavaScript value to a JavaScript Object Notation (JSON) string. + * @param value A JavaScript value, usually an object or array, to be converted. + * @param replacer A function that transforms the results. + * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read. + */ +export function stringify(value: any, replacer?: (key: string, value: any) => any, space?: string | number): string; +/** + * Compatibility syntax (follows JSON specification). + * Converts a JavaScript value to a JavaScript Object Notation (JSON) string. + * @param value A JavaScript value, usually an object or array, to be converted. + * @param replacer An array of strings and numbers that acts as a approved list for selecting the object properties that will be stringified. + * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read. + */ +export function stringify(value: any, replacer?: Array | null, space?: string | number): string; + +/** + * Parse json/json5 text and return an array of tokens it consists of. + * @param text Text to tokenize + * @param options Parser options + */ +export function tokenize(text: string, options?: ParseOptions): Token[]; + +/** + * Parse json/json5 text and try to guess indentation, quoting style, etc. + * @param text Text to analyze + * @param options Parser options + */ +export function analyze(text: string, options?: ParseOptions): JsonStyle; + +/** + * Change json/json5 text, preserving original formatting as much as possible. + * @param text Original text + * @param new_value New value you want to set + * @param options Parser or stringifier options + * + * @example + * // here is your original JSON document: + * var input = '{"foo": "bar", "baz": 123}' + * + * // you need to parse it first: + * var json = jju.parse(input, {mode: 'json'}) + * // json is { foo: 'bar', baz: 123 } + * + * // then you can change it as you like: + * json.foo = 'quux' + * json.hello = 'world' + * + * // then you run an update function to change the original json: + * var output = jju.update(input, json, {mode: 'json'}) + * // output is '{"foo": "quux", "baz": 123, "hello": "world"}' + */ +export function update(text: string, new_value: any, options?: ParseOptions | StringifyOptions): string; diff --git a/types/jju/jju-tests.ts b/types/jju/jju-tests.ts new file mode 100644 index 0000000000..0de4d973b1 --- /dev/null +++ b/types/jju/jju-tests.ts @@ -0,0 +1,4 @@ +import * as jju from 'jju'; + +jju.parse('{}'); // $ExpectType any +jju.stringify({}); // $ExpectType string diff --git a/types/jju/tsconfig.json b/types/jju/tsconfig.json new file mode 100644 index 0000000000..08bb6638c7 --- /dev/null +++ b/types/jju/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "jju-tests.ts" + ] +} diff --git a/types/jju/tslint.json b/types/jju/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/jju/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 5068c7952765e6d19337c6fedaa58dba6195ff9d Mon Sep 17 00:00:00 2001 From: Elizabeth Craig Date: Wed, 28 Nov 2018 20:29:55 -0800 Subject: [PATCH 2/2] Allow parse and stringify options for update() --- types/jju/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/jju/index.d.ts b/types/jju/index.d.ts index 46fbf2f77b..f8bf96d639 100644 --- a/types/jju/index.d.ts +++ b/types/jju/index.d.ts @@ -192,7 +192,7 @@ export function analyze(text: string, options?: ParseOptions): JsonStyle; * Change json/json5 text, preserving original formatting as much as possible. * @param text Original text * @param new_value New value you want to set - * @param options Parser or stringifier options + * @param options Parser and stringifier options * * @example * // here is your original JSON document: @@ -210,4 +210,4 @@ export function analyze(text: string, options?: ParseOptions): JsonStyle; * var output = jju.update(input, json, {mode: 'json'}) * // output is '{"foo": "quux", "baz": 123, "hello": "world"}' */ -export function update(text: string, new_value: any, options?: ParseOptions | StringifyOptions): string; +export function update(text: string, new_value: any, options?: ParseOptions & StringifyOptions): string;