From 3d460f345f84a72eabf909a4b4fbde6ab963f025 Mon Sep 17 00:00:00 2001 From: Christoffer Bjelke Date: Thu, 7 Mar 2024 11:51:48 +0100 Subject: [PATCH 1/3] Add JSON.stringify entrypoint and tests --- package.json | 5 +++ src/entrypoints/json-stringify.d.ts | 32 +++++++++++++++++++ src/entrypoints/recommended.d.ts | 1 + src/tests/json-stringify.ts | 48 +++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 src/entrypoints/json-stringify.d.ts create mode 100644 src/tests/json-stringify.ts diff --git a/package.json b/package.json index 09f4bc1..745b70d 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,11 @@ "import": "./dist/json-parse.mjs", "default": "./dist/json-parse.js" }, + "./json-stringify": { + "types": "./dist/json-stringify.d.ts", + "import": "./dist/json-stringify.mjs", + "default": "./dist/json-stringify.js" + }, "./fetch": { "types": "./dist/fetch.d.ts", "import": "./dist/fetch.mjs", diff --git a/src/entrypoints/json-stringify.d.ts b/src/entrypoints/json-stringify.d.ts new file mode 100644 index 0000000..e7ca848 --- /dev/null +++ b/src/entrypoints/json-stringify.d.ts @@ -0,0 +1,32 @@ +interface JSON { + /** + * Converts a JavaScript value to a JavaScript Object Notation (JSON) string or undefined. + * @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. + */ + stringify( + value: T, + replacer?: (this: any, key: string, value: any) => any, + space?: string | number, + ): T extends {} | null + ? string + : T extends undefined + ? undefined + : string | undefined; + /** + * Converts a JavaScript value to a JavaScript Object Notation (JSON) string or undefined. + * @param value A JavaScript value, usually an object or array, to be converted. + * @param replacer An array of strings and numbers that acts as an 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. + */ + stringify( + value: T, + replacer?: (number | string)[] | null, + space?: string | number, + ): T extends {} | null + ? string + : T extends undefined + ? undefined + : string | undefined; +} diff --git a/src/entrypoints/recommended.d.ts b/src/entrypoints/recommended.d.ts index 7e6b2b4..42ca2f1 100644 --- a/src/entrypoints/recommended.d.ts +++ b/src/entrypoints/recommended.d.ts @@ -2,6 +2,7 @@ /// /// /// +/// /// /// /// diff --git a/src/tests/json-stringify.ts b/src/tests/json-stringify.ts new file mode 100644 index 0000000..1120f70 --- /dev/null +++ b/src/tests/json-stringify.ts @@ -0,0 +1,48 @@ +import { doNotExecute, Equal, Expect } from "./utils"; + +doNotExecute(() => { + const result = JSON.stringify({}); + + type tests = [Expect>]; +}); + +doNotExecute(() => { + const result = JSON.stringify(null); + + type tests = [Expect>]; +}); + +doNotExecute(() => { + const result = JSON.stringify(undefined); + + type tests = [Expect>]; +}); + +doNotExecute(() => { + // create a something that is either an object or null + let toBeStringified: {} | null = Math.random() > 0.5 ? {} : null; + const result = JSON.stringify(toBeStringified); + + type tests = [Expect>]; +}); +doNotExecute(() => { + // create a something that is either an object or undefined + let toBeStringified: {} | undefined = Math.random() > 0.5 ? {} : undefined; + const result = JSON.stringify(toBeStringified); + + type tests = [Expect>]; +}); + +doNotExecute(() => { + // create a something that is of type any + let toBeStringified: any; + const result = JSON.stringify(toBeStringified); + + type tests = [Expect>]; +}); + +doNotExecute(() => { + const result = JSON.stringify("undefined"); + + type tests = [Expect>]; +}); From 311c3f380da7c9bb1a129ba9b27160090bb46b1f Mon Sep 17 00:00:00 2001 From: Christoffer Bjelke Date: Thu, 7 Mar 2024 12:01:26 +0100 Subject: [PATCH 2/3] Update JSON.stringify type definition to handle function types --- src/entrypoints/json-stringify.d.ts | 8 ++++++-- src/tests/json-stringify.ts | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/entrypoints/json-stringify.d.ts b/src/entrypoints/json-stringify.d.ts index e7ca848..291b394 100644 --- a/src/entrypoints/json-stringify.d.ts +++ b/src/entrypoints/json-stringify.d.ts @@ -10,7 +10,9 @@ interface JSON { replacer?: (this: any, key: string, value: any) => any, space?: string | number, ): T extends {} | null - ? string + ? T extends () => void + ? undefined + : string : T extends undefined ? undefined : string | undefined; @@ -25,7 +27,9 @@ interface JSON { replacer?: (number | string)[] | null, space?: string | number, ): T extends {} | null - ? string + ? T extends () => void + ? undefined + : string : T extends undefined ? undefined : string | undefined; diff --git a/src/tests/json-stringify.ts b/src/tests/json-stringify.ts index 1120f70..fc142bc 100644 --- a/src/tests/json-stringify.ts +++ b/src/tests/json-stringify.ts @@ -25,6 +25,7 @@ doNotExecute(() => { type tests = [Expect>]; }); + doNotExecute(() => { // create a something that is either an object or undefined let toBeStringified: {} | undefined = Math.random() > 0.5 ? {} : undefined; @@ -33,6 +34,13 @@ doNotExecute(() => { type tests = [Expect>]; }); +doNotExecute(() => { + // create a something that is a function + const result = JSON.stringify(function () {}); + + type tests = [Expect>]; +}); + doNotExecute(() => { // create a something that is of type any let toBeStringified: any; From a9b624d7162714385adb481dd23ac1cbce55d4de Mon Sep 17 00:00:00 2001 From: Christoffer Bjelke Date: Thu, 7 Mar 2024 12:12:24 +0100 Subject: [PATCH 3/3] Update JSON.stringify typings to handle all functions --- src/entrypoints/json-stringify.d.ts | 4 ++-- src/tests/json-stringify.ts | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/entrypoints/json-stringify.d.ts b/src/entrypoints/json-stringify.d.ts index 291b394..87542e4 100644 --- a/src/entrypoints/json-stringify.d.ts +++ b/src/entrypoints/json-stringify.d.ts @@ -10,7 +10,7 @@ interface JSON { replacer?: (this: any, key: string, value: any) => any, space?: string | number, ): T extends {} | null - ? T extends () => void + ? T extends (..._: any) => void ? undefined : string : T extends undefined @@ -27,7 +27,7 @@ interface JSON { replacer?: (number | string)[] | null, space?: string | number, ): T extends {} | null - ? T extends () => void + ? T extends (..._: any) => void ? undefined : string : T extends undefined diff --git a/src/tests/json-stringify.ts b/src/tests/json-stringify.ts index fc142bc..f8b9212 100644 --- a/src/tests/json-stringify.ts +++ b/src/tests/json-stringify.ts @@ -41,6 +41,13 @@ doNotExecute(() => { type tests = [Expect>]; }); +doNotExecute(() => { + // create a something that is a function + const result = JSON.stringify(function (hello: any, world: any) {}); + + type tests = [Expect>]; +}); + doNotExecute(() => { // create a something that is of type any let toBeStringified: any;