From eb70258db2c822f55588b3c0eca38a5a3948611e Mon Sep 17 00:00:00 2001 From: Aluan Haddad Date: Tue, 6 Feb 2018 15:26:32 -0500 Subject: [PATCH 1/5] improve the type of the stringify function Update the stringify function based on the declarations in `lib.es5.d.ts` for JSON.parse. Add the documentation taken from `lib.es5.t.ts` --- types/comment-json/index.d.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/types/comment-json/index.d.ts b/types/comment-json/index.d.ts index 9c2652f34e..09ed176ac5 100644 --- a/types/comment-json/index.d.ts +++ b/types/comment-json/index.d.ts @@ -4,5 +4,19 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped export type Reviver = (k: number | string, v: any) => any; + +/** + * Converts a JavaScript Object Notation (JSON) string into an object. + * @param json 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(json: string, reviver?: Reviver, removes_comments?: boolean): any; -export function stringify(value: any, replacer?: any, space?: string | number): string; + +/** + * 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 or 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?: ((key: string, value: any) => any) | (number | string)[] | null, space?: string | number): string; From 14622de78f170911e8cc7547ba20ff4bffd81ff6 Mon Sep 17 00:00:00 2001 From: Aluan Haddad Date: Tue, 6 Feb 2018 15:35:05 -0500 Subject: [PATCH 2/5] lint --- types/comment-json/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/comment-json/index.d.ts b/types/comment-json/index.d.ts index 09ed176ac5..e03f15051c 100644 --- a/types/comment-json/index.d.ts +++ b/types/comment-json/index.d.ts @@ -19,4 +19,4 @@ export function parse(json: string, reviver?: Reviver, removes_comments?: boolea * @param replacer A function that transforms the results or 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?: ((key: string, value: any) => any) | (number | string)[] | null, space?: string | number): string; +export function stringify(value: any, replacer?: ((key: string, value: any) => any) | Array | null, space?: string | number): string; From c240fc8a4339c8ff5337039cd81e0fa575c8f58d Mon Sep 17 00:00:00 2001 From: Aluan Haddad Date: Tue, 6 Feb 2018 15:48:18 -0500 Subject: [PATCH 3/5] match DefinitelyTyped JSDoc asterisk style --- types/comment-json/index.d.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/types/comment-json/index.d.ts b/types/comment-json/index.d.ts index e03f15051c..fcb7608e2a 100644 --- a/types/comment-json/index.d.ts +++ b/types/comment-json/index.d.ts @@ -6,17 +6,17 @@ export type Reviver = (k: number | string, v: any) => any; /** - * Converts a JavaScript Object Notation (JSON) string into an object. - * @param json 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. - */ + * Converts a JavaScript Object Notation (JSON) string into an object. + * @param json 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(json: string, reviver?: Reviver, removes_comments?: boolean): any; /** - * 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 or 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. - */ + * 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 or 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?: ((key: string, value: any) => any) | Array | null, space?: string | number): string; From 5f458aeadb19f8d1b06aafba493d66139fb67aa9 Mon Sep 17 00:00:00 2001 From: Aluan Haddad Date: Tue, 6 Feb 2018 15:48:35 -0500 Subject: [PATCH 4/5] add tests for updated signature --- types/comment-json/comment-json-tests.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/types/comment-json/comment-json-tests.ts b/types/comment-json/comment-json-tests.ts index af91ff422f..56aa8453fe 100644 --- a/types/comment-json/comment-json-tests.ts +++ b/types/comment-json/comment-json-tests.ts @@ -14,3 +14,11 @@ const result = commentJson.parse(` // comment at the bottom `); const str = commentJson.stringify(result); + +const numericallyIndexed = commentJson.stringify(result, (key, value) => { +return key && Number.isInteger(Number(key)) + ? value + : undefined; +}); + +const whiteListed = commentJson.stringify(result, ['a', 1]); From bd3ddd86804078dab667319f6e3bc0886dde5e21 Mon Sep 17 00:00:00 2001 From: Aluan Haddad Date: Tue, 6 Feb 2018 15:55:04 -0500 Subject: [PATCH 5/5] document removes_comments parameter based on source README --- types/comment-json/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/types/comment-json/index.d.ts b/types/comment-json/index.d.ts index fcb7608e2a..0aad1934ff 100644 --- a/types/comment-json/index.d.ts +++ b/types/comment-json/index.d.ts @@ -9,6 +9,7 @@ export type Reviver = (k: number | string, v: any) => any; * Converts a JavaScript Object Notation (JSON) string into an object. * @param json A valid JSON string. * @param reviver A function that transforms the results. This function is called for each member of the object. + * @param removes_comments If true, the comments won't be maintained, which is often used when we want to get a clean object. * If a member contains nested objects, the nested objects are transformed before the parent object is. */ export function parse(json: string, reviver?: Reviver, removes_comments?: boolean): any;