From e30c0d5f05938ce39e3dbf35d8a5b607e7fa5f2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Vitor=20de=20Lima=20Matos?= Date: Tue, 2 Jul 2019 13:37:10 -0300 Subject: [PATCH] Add lossless-json types (#36596) As requested at https://github.com/josdejong/lossless-json/pull/10 --- types/lossless-json/index.d.ts | 81 ++++++++++++++++++++++ types/lossless-json/lossless-json-tests.ts | 13 ++++ types/lossless-json/tsconfig.json | 23 ++++++ types/lossless-json/tslint.json | 1 + 4 files changed, 118 insertions(+) create mode 100644 types/lossless-json/index.d.ts create mode 100644 types/lossless-json/lossless-json-tests.ts create mode 100644 types/lossless-json/tsconfig.json create mode 100644 types/lossless-json/tslint.json diff --git a/types/lossless-json/index.d.ts b/types/lossless-json/index.d.ts new file mode 100644 index 0000000000..eb520606c5 --- /dev/null +++ b/types/lossless-json/index.d.ts @@ -0,0 +1,81 @@ +// Type definitions for lossless-json 1.0 +// Project: https://github.com/josdejong/lossless-json#readme +// Definitions by: André Vitor de Lima Matos +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/** + * Get and/or set configuration options + * @param options.circularRefs enable circularRefs (default is true) + * @return New/current config + */ +export function config({ circularRefs }?: { circularRefs?: boolean }): { circularRefs: boolean }; +/** + * The LosslessJSON.parse() method parses a string as JSON, optionally transforming + * the value produced by parsing. + * + * @param text The string to parse as JSON. See the JSON object for a description of JSON syntax. + * + * @param reviver + * If a function, prescribes how the value originally produced by parsing is + * transformed, before being returned. + * + * @returns Returns the Object corresponding to the given JSON text. + * + * @throws Throws a SyntaxError exception if the string to parse is not valid JSON. + */ + +export function parse(text: string, reviver?: (key: string, value: any) => any): any; +/** + * The LosslessJSON.stringify() method converts a JavaScript value to a JSON string, + * optionally replacing values if a replacer function is specified, or + * optionally including only the specified properties if a replacer array is specified. + * + * @param value + * The value to convert to a JSON string. + * + * @param replacer + * A function that alters the behavior of the stringification process, + * or an array of String and Number objects that serve as a whitelist for + * selecting the properties of the value object to be included in the JSON string. + * If this value is null or not provided, all properties of the object are + * included in the resulting JSON string. + * + * @param space + * A String or Number object that's used to insert white space into the output + * JSON string for readability purposes. If this is a Number, it indicates the + * number of space characters to use as white space; this number is capped at 10 + * if it's larger than that. Values less than 1 indicate that no space should be + * used. If this is a String, the string (or the first 10 characters of the string, + * if it's longer than that) is used as white space. If this parameter is not + * provided (or is null), no white space is used. + * + * @returns String representation of the JSON object. + */ +export function stringify( + value: any, + replacer?: ((key: string, value: any) => any) | Array, + space?: string | number, +): string; + +/** + * A lossless number. Stores its value as string + * @param value + */ +export class LosslessNumber { + // value as string + value: string; + type: 'LosslessNumber'; + isLosslessNumber: true; + constructor(value: string | number); + /** + * Get the value of the LosslessNumber as number. + * Will throw an error when this conversion would result in a truncation of the number. + * @return Number + */ + valueOf(): number; + /** + * Get the value of the LosslessNumber as string. + * @return string + */ + toString(): string; +} diff --git a/types/lossless-json/lossless-json-tests.ts b/types/lossless-json/lossless-json-tests.ts new file mode 100644 index 0000000000..6b00e7548f --- /dev/null +++ b/types/lossless-json/lossless-json-tests.ts @@ -0,0 +1,13 @@ +import LosslessJSON = require('lossless-json'); + +LosslessJSON.config({ circularRefs: false }); + +const parsed = LosslessJSON.parse('{ "n": 123 }', (key, value) => { + if (key && value && value instanceof LosslessJSON.LosslessNumber) { + return value.valueOf(); + } + return value; +}); + +const str = LosslessJSON.stringify({ n: new LosslessJSON.LosslessNumber(parsed.n) }, undefined, 2); +if (typeof str !== 'string') throw new Error('not a string'); diff --git a/types/lossless-json/tsconfig.json b/types/lossless-json/tsconfig.json new file mode 100644 index 0000000000..8a5ad639e4 --- /dev/null +++ b/types/lossless-json/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "lossless-json-tests.ts" + ] +} diff --git a/types/lossless-json/tslint.json b/types/lossless-json/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/lossless-json/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }