Skip to content

Commit 37ae2b4

Browse files
authored
Backslash encoder (#28)
* Backslash * bump version
1 parent d604081 commit 37ae2b4

File tree

8 files changed

+67
-11
lines changed

8 files changed

+67
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- [x] 13. Js Console
2929
- [x] 14. HTML Entity Encode / Decode
3030
- [x] 15. URL Encode / Decode
31+
- [x] 16. Backslash Encode / Decode
3132

3233
## Demo
3334

src/components/Main.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import CronEditor from './cron/Cron';
2121
import JsConsole from './notebook/JavaScript';
2222
import HtmlEntityCodec from './html/HtmlEntityCodec';
2323
import UrlCodec from './url/UrlCodec';
24+
import BackSlashCodec from './text/BackSlash';
2425

2526
interface MenuItem {
2627
path: string;
@@ -143,6 +144,13 @@ const defaultRoutes: MenuItem[] = [
143144
show: false,
144145
Component: UrlCodec,
145146
},
147+
{
148+
icon: <FontAwesomeIcon icon="slash" transform={{ rotate: 42 }} />,
149+
path: '/back-slash-encoder',
150+
name: 'Backslash Encoder',
151+
show: false,
152+
Component: BackSlashCodec,
153+
},
146154
];
147155

148156
const Main = () => {

src/components/common/1-to-1.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@ import { clipboard } from 'electron';
22
import React, { useState } from 'react';
33

44
interface OneToOneProps {
5-
fromDefault: string;
6-
fromFunc: (f: string) => string;
5+
defaultInput: string;
6+
forwardFunc: (f: string) => string;
77
inverseFunc: (r: string) => string;
88
}
99

10-
const OneToOne = ({ fromDefault, fromFunc, inverseFunc }: OneToOneProps) => {
11-
const [from, setFrom] = useState(fromDefault);
12-
const [to, setTo] = useState(fromFunc(from));
10+
const OneToOne = ({
11+
defaultInput,
12+
forwardFunc,
13+
inverseFunc,
14+
}: OneToOneProps) => {
15+
const [from, setFrom] = useState(defaultInput);
16+
const [to, setTo] = useState(forwardFunc(from));
1317

1418
const [fromCopied, setFromCopied] = useState(false);
1519
const [toCopied, setToCopied] = useState(false);
1620

1721
const changeFrom = (value: string) => {
1822
setFrom(value);
19-
setTo(fromFunc(value));
23+
setTo(forwardFunc(value));
2024
};
2125

2226
const changeTo = (value: string) => {

src/components/html/HtmlEntityCodec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import OneToOne from '../common/1-to-1';
55
const HtmlEntityCodec = () => {
66
return (
77
<OneToOne
8-
fromDefault='<script>alert("Hello");</script>'
9-
fromFunc={escape}
8+
defaultInput='<script>alert("Hello");</script>'
9+
forwardFunc={escape}
1010
inverseFunc={unescape}
1111
/>
1212
);

src/components/text/BackSlash.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from 'react';
2+
import OneToOne from '../common/1-to-1';
3+
4+
const addSlashes = (string: string) => {
5+
return (
6+
string
7+
.replace(/\\/g, '\\\\')
8+
// eslint-disable-next-line no-control-regex
9+
.replace(/\u0008/g, '\\b')
10+
.replace(/\t/g, '\\t')
11+
.replace(/\n/g, '\\n')
12+
.replace(/\f/g, '\\f')
13+
.replace(/\r/g, '\\r')
14+
.replace(/'/g, "\\'")
15+
.replace(/"/g, '\\"')
16+
);
17+
};
18+
19+
const removeSlashes = (string: string) => {
20+
return string
21+
.replace(/\\"/g, '"')
22+
.replace(/\\'/g, "'")
23+
.replace(/\\r/g, '\r')
24+
.replace(/\\f/g, '\f')
25+
.replace(/\\n/g, '\n')
26+
.replace(/\\t/g, '\t')
27+
.replace(/\\b/, '\u0008')
28+
.replace(/\\\\/g, '\\');
29+
};
30+
31+
const BackSlashCodec = () => {
32+
return (
33+
<OneToOne
34+
defaultInput="Hello\nworld!"
35+
forwardFunc={removeSlashes}
36+
inverseFunc={addSlashes}
37+
/>
38+
);
39+
};
40+
41+
export default BackSlashCodec;

src/components/url/UrlCodec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import OneToOne from '../common/1-to-1';
44
const UrlCodec = () => {
55
return (
66
<OneToOne
7-
fromDefault="https://plainlab.github.io/?q=plainbelt"
8-
fromFunc={encodeURIComponent}
7+
defaultInput="https://plainlab.github.io/?q=plainbelt"
8+
forwardFunc={encodeURIComponent}
99
inverseFunc={decodeURIComponent}
1010
/>
1111
);

src/helpers/fontAwesome.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
faCheck,
2626
faLink,
2727
faFileCode,
28+
faSlash,
2829
} from '@fortawesome/free-solid-svg-icons';
2930

3031
library.add(
@@ -50,5 +51,6 @@ library.add(
5051
faSlidersH,
5152
faLink,
5253
faFileCode,
54+
faSlash,
5355
faCheck
5456
);

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "plainbelt",
33
"productName": "PlainBelt",
4-
"version": "0.0.16",
4+
"version": "0.0.17",
55
"description": "A plain toolbelt for developers",
66
"main": "./main.prod.js",
77
"author": {

0 commit comments

Comments
 (0)