mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-06-03 06:27:46 +08:00
Merge pull request #23466 from aluanhaddad/patch-1
improve the type of the stringify function
This commit is contained in:
@@ -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]);
|
||||
|
||||
17
types/comment-json/index.d.ts
vendored
17
types/comment-json/index.d.ts
vendored
@@ -4,5 +4,20 @@
|
||||
// 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.
|
||||
* @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;
|
||||
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) | Array<number | string> | null, space?: string | number): string;
|
||||
|
||||
Reference in New Issue
Block a user