From cf6b27fa4ac31a495854aa980bb5150bf993b4c4 Mon Sep 17 00:00:00 2001 From: k15 Date: Fri, 12 Jul 2024 17:12:06 +0700 Subject: [PATCH] feat : JSON stringify and parse --- README.md | 9 ++++++++- package.json | 12 ++++++++++-- src/commands/commandRegister.ts | 20 +++++++++++++++++++- src/handler/json2PhpArray.ts | 2 +- src/handler/jsonparse.spec.ts | 30 ++++++++++++++++++++++++++++++ src/handler/jsonparse.ts | 14 ++++++++++++++ src/handler/jsonstringify.spec.ts | 26 ++++++++++++++++++++++++++ src/handler/jsonstringify.ts | 4 ++++ 8 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 src/handler/jsonparse.spec.ts create mode 100644 src/handler/jsonparse.ts create mode 100644 src/handler/jsonstringify.spec.ts create mode 100644 src/handler/jsonstringify.ts diff --git a/README.md b/README.md index 61ab41d..85f2a3f 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,10 @@ Read on [this page](https://code.visualstudio.com/docs/getstarted/keybindings) t - **enterNumberByLines**: Transform the text to numbered text line by line. +- **JSON Stringify**: Transform the JSON to text. + +- **JSON Parse**: Transform the JSON string to JSON. + These casing conventions provide flexible options to transform your text according to specific requirements. ## Extension Setting @@ -107,7 +111,10 @@ To use the extension, you can execute the following commands: - `casing-convention.kebabCase`: Convert text to kebab-case. - `casing-convention.screamKebabCase`: Convert text to SCREAM-KEBAB-CASE. - `casing-convention.json2ArrayPHP`: Convert json to array php. -- `casing-convention.arrayPHP2Json`: Convert array php to json. +- `casing-convention.arrayPHP2Json`: Convert array to JSON. +- `casing-convention.enterNumberByLines`: Add text number line by line. +- `casing-convention.jsonStringify`: Convert JSON to JSON Stringify. +- `casing-convention.jsonParse`: Convert JSON Stringify to JSON. ## Reporting issues Report any issues on the github [issues page](https://github.com/otnansirk/vscode-casing-convention/issues). Follow the template and add as much information as possible. diff --git a/package.json b/package.json index dab89ba..ebf249c 100644 --- a/package.json +++ b/package.json @@ -170,15 +170,23 @@ }, { "command": "casing-convention.json2ArrayPHP", - "title": "casing: json to array PHP" + "title": "casing: JSON to array PHP" }, { "command": "casing-convention.arrayPHP2Json", - "title": "casing: array PHP to json" + "title": "casing: array PHP to JSON" }, { "command": "casing-convention.enterNumberByLines", "title": "casing: Numbered text" + }, + { + "command": "casing-convention.jsonStringify", + "title": "casing: JSON Stringify" + }, + { + "command": "casing-convention.jsonParse", + "title": "casing: JSON Parse" } ], "menus": { diff --git a/src/commands/commandRegister.ts b/src/commands/commandRegister.ts index a4967f3..e8609c0 100644 --- a/src/commands/commandRegister.ts +++ b/src/commands/commandRegister.ts @@ -25,6 +25,8 @@ import { json2PhpArray } from "../handler/json2PhpArray"; import { phpArray2Json } from "../handler/phpArray2Json"; import { capitalCase } from "../handler/capitalCase"; import { numberedByLines } from "../handler/numberedByLines"; +import { jsonstringify } from "../handler/jsonstringify"; +import { jsonparse } from "../handler/jsonparse"; type CommandType = { @@ -232,7 +234,23 @@ const commandRegister: CommandType[] = [ description: '1. Hello casing convention', detail: 'Transform the text to numbered text line by line.', callback: commandProvider(numberedByLines) - } + }, + { + type: COMMAND_TYPE_HANDLER, + name: 'casing-convention.jsonStringify', + label: 'jsonStringify', + description: '"{\"kirs\": \"OKE\"}"', + detail: 'Transform the JSON to JSON string.', + callback: commandProvider(jsonstringify) + }, + { + type: COMMAND_TYPE_HANDLER, + name: 'casing-convention.jsonParse', + label: 'jsonParse', + description: '{"kirs": "OKE"}"', + detail: 'Transform the JSON string to JSON.', + callback: commandProvider(jsonparse) + }, ]; export default commandRegister; diff --git a/src/handler/json2PhpArray.ts b/src/handler/json2PhpArray.ts index 72e7630..596d9a2 100644 --- a/src/handler/json2PhpArray.ts +++ b/src/handler/json2PhpArray.ts @@ -1,5 +1,5 @@ /** - * Lower case the first character of an input string. + * Transfrom JSON to Array. */ export const json2PhpArray = (str: string) => str .replaceAll('{', '[') diff --git a/src/handler/jsonparse.spec.ts b/src/handler/jsonparse.spec.ts new file mode 100644 index 0000000..0d5dccf --- /dev/null +++ b/src/handler/jsonparse.spec.ts @@ -0,0 +1,30 @@ +import { deepStrictEqual, throws } from "assert"; +import { jsonparse } from "./jsonparse"; // Ensure this path is correct + +describe('jsonparse', function () { + const TEST_CASES = [ + { + input: "{\"kirs\":\"OKE\"}", + expected: "Failed to parse JSON", + isThrow: true + }, + { + input: `${"{\"kirs\":\"OKE\",\"name\":\"kris\"}"}`, + expected: "Failed to parse JSON", + isThrow: true + } + ]; + + TEST_CASES.forEach(({ input, expected, isThrow}) => { + it(`Should convert ${input} to ${JSON.stringify(expected)}`, function () { + if (isThrow) { + throws(() => jsonparse(input), new Error(expected)); + } else { + let result = jsonparse(input); + console.log(`${typeof(input)} to ${typeof(expected)} KRIS`); + + deepStrictEqual(result, expected); + } + }); + }); +}); diff --git a/src/handler/jsonparse.ts b/src/handler/jsonparse.ts new file mode 100644 index 0000000..a2783c5 --- /dev/null +++ b/src/handler/jsonparse.ts @@ -0,0 +1,14 @@ +/** + * Transfor String to JSON. + */ +export const jsonparse = (str: string) => { + try { + const text = JSON.parse(str).toString(); + if (text === '[object Object]') { + throw new Error("Failed to parse JSON"); + } + return text; + } catch (error) { + throw new Error("Failed to parse JSON"); + } +}; \ No newline at end of file diff --git a/src/handler/jsonstringify.spec.ts b/src/handler/jsonstringify.spec.ts new file mode 100644 index 0000000..08fb52a --- /dev/null +++ b/src/handler/jsonstringify.spec.ts @@ -0,0 +1,26 @@ +import { strictEqual } from "assert"; +import { jsonstringify } from "./jsonstringify"; + +describe('jsonstringify', function () { + + const TEST_CASES = [ + { + input: {"kirs": "OKE"}, + expected: "{\"kirs\":\"OKE\"}" + }, + { + input: { + "kirs": "OKE", + "name": "kris" + }, + expected: "{\"kirs\":\"OKE\",\"name\":\"kris\"}" + } + ]; + + TEST_CASES.forEach(({ input, expected }: any) => { + it(`Sould convert ${input} to ${expected}`, function () { + let result = jsonstringify(input); + strictEqual(result, expected.toString()); + }); + }); +}); \ No newline at end of file diff --git a/src/handler/jsonstringify.ts b/src/handler/jsonstringify.ts new file mode 100644 index 0000000..67d8050 --- /dev/null +++ b/src/handler/jsonstringify.ts @@ -0,0 +1,4 @@ +/** + * Transfor JSON to string. + */ +export const jsonstringify = (str: string) => JSON.stringify(str).toString(); \ No newline at end of file