Skip to content

Commit

Permalink
feat : base64 encode and decode
Browse files Browse the repository at this point in the history
  • Loading branch information
otnansirk committed Dec 6, 2024
1 parent 04f3f59 commit ec985cd
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ Read on [this page](https://code.visualstudio.com/docs/getstarted/keybindings) t

- **json2ObjectJS**: Transform the JSON to Object JS.

- **base64Encode**: Encodes to Base64.

- **base64Decode**: Decodes from Base64.

These casing conventions provide flexible options to transform your text according to specific requirements.

## Extension Setting
Expand Down Expand Up @@ -131,6 +135,8 @@ To use the extension, you can execute the following commands:
- `casing-convention.jsonMinify`: Convert JSON to one line format without space.
- `casing-convention.json2ObjectJS`: Convert JSON to ObjectJS.
- `casing-convention.objectJS2Json`: Convert ObjectJS to JSON.
- `casing-convention.base64Encode`: Encodes to base64.
- `casing-convention.base64Decode`: Decodes from base64.

## 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
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@
{
"command": "casing-convention.objectJS2Json",
"title": "casing: Object JS to JSON"
},
{
"command": "casing-convention.base64Encode",
"title": "casing: Base64 encode"
},
{
"command": "casing-convention.base64Decode",
"title": "casing: Base64 decode"
}
],
"menus": {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/commandProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ const commandProvider = (callback: any) => async () => {
});

// Display a message box to the user
await vscode.window.showInformationMessage(`Success convert to ${callback.name}`, {modal:false});
await vscode.window.showInformationMessage(`${callback.name} : Successfully`, {modal:false});
} catch (err) {
// Display a message box to the user
if (err instanceof HasNoSelected) {
await vscode.window.showErrorMessage(err.message);
} else {
await vscode.window.showErrorMessage(`Failed convert to ${callback.name}`);
} else {
await vscode.window.showErrorMessage(`${callback.name} : Failed`);
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/commands/commandRegister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import { jsonMinify } from "../handler/jsonMinify";
import { json2ObjectJS } from "../handler/json2ObjectJS";
import { objectJS2Json } from "../handler/objectJS2Json";
import { snakeCase } from "../handler/snakeCase";
import { base64Decode } from "../handler/base64Decode";
import { base64Encode } from "../handler/base64Encode";


type CommandType = {
Expand Down Expand Up @@ -287,6 +289,22 @@ const commandRegister: CommandType[] = [
detail: 'Transform the Object JS to JSON.',
callback: commandProvider(objectJS2Json)
},
{
type: COMMAND_TYPE_HANDLER,
name: 'casing-convention.base64Encode',
label: 'base64Encode',
description: '',
detail: 'Transform base6 to string.',
callback: commandProvider(base64Encode)
},
{
type: COMMAND_TYPE_HANDLER,
name: 'casing-convention.base64Decode',
label: 'base64Decode',
description: '',
detail: 'Transform string to base6.',
callback: commandProvider(base64Decode)
},
];

export default commandRegister;
Expand Down
23 changes: 23 additions & 0 deletions src/handler/base64Decode.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { strictEqual } from "assert";
import { base64Decode } from "./base64Decode";

describe('base64Decode', function() {

const TEST_CASES = [
{
input: "amF2YXNjcmlwdA==",
expected: "javascript"
},
{
input: "SGksIGtyaXMgaG93IGFyZSB5b3UgPw==",
expected: "Hi, kris how are you ?"
}
];

TEST_CASES.forEach(({input, expected}: any) => {
it(`Sould convert ${input} to ${expected}`, function() {
let result = base64Decode(input);
strictEqual(result, expected);
});
});
});
1 change: 1 addition & 0 deletions src/handler/base64Decode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const base64Decode = (text: string): string => atob(text);
23 changes: 23 additions & 0 deletions src/handler/base64Encode.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { strictEqual } from "assert";
import { base64Encode } from "./base64Encode";

describe('base64Encode', function() {

const TEST_CASES = [
{
input: "javascript",
expected: "amF2YXNjcmlwdA=="
},
{
input: "Hi, kris how are you ?",
expected: "SGksIGtyaXMgaG93IGFyZSB5b3UgPw=="
}
];

TEST_CASES.forEach(({input, expected}: any) => {
it(`Sould convert ${input} to ${expected}`, function() {
let result = base64Encode(input);
strictEqual(result, expected);
});
});
});
1 change: 1 addition & 0 deletions src/handler/base64Encode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const base64Encode = (text: string): string => btoa(text);

0 comments on commit ec985cd

Please sign in to comment.