Skip to content

Commit

Permalink
feat : JSON stringify and parse
Browse files Browse the repository at this point in the history
  • Loading branch information
otnansirk committed Jul 12, 2024
1 parent cbf600d commit cf6b27f
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 5 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
20 changes: 19 additions & 1 deletion src/commands/commandRegister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/handler/json2PhpArray.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Lower case the first character of an input string.
* Transfrom JSON to Array.
*/
export const json2PhpArray = (str: string) => str
.replaceAll('{', '[')
Expand Down
30 changes: 30 additions & 0 deletions src/handler/jsonparse.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
});
14 changes: 14 additions & 0 deletions src/handler/jsonparse.ts
Original file line number Diff line number Diff line change
@@ -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");
}
};
26 changes: 26 additions & 0 deletions src/handler/jsonstringify.spec.ts
Original file line number Diff line number Diff line change
@@ -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());
});
});
});
4 changes: 4 additions & 0 deletions src/handler/jsonstringify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Transfor JSON to string.
*/
export const jsonstringify = (str: string) => JSON.stringify(str).toString();

0 comments on commit cf6b27f

Please sign in to comment.