Skip to content

Commit 0e48b71

Browse files
authored
feat: add encode/decode commands (#11)
1 parent 1fbeb10 commit 0e48b71

File tree

4 files changed

+143
-41
lines changed

4 files changed

+143
-41
lines changed

Decode URI.png

50.7 KB
Loading

Encode URI.png

51.3 KB
Loading

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ Capitalize first char of each word.
5757

5858
`The quick brown fox jumps over the lazy dog /a``The Quick Brown Fox Jumps Over The Lazy Dog`
5959

60+
### Encode URI `/e`
61+
Encode URI by replacing certain characters with UTF-8 escape sequences.
62+
63+
`https://mozilla.org/?x=шеллы /e``https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B`
64+
65+
See [`encodeURI` on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI)
66+
67+
### Decode URI `/d`
68+
Inverse of `/e`.
69+
70+
`https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B /d``https://mozilla.org/?x=шеллы`
71+
72+
See [`decodeURI` on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI)
73+
6074
### Slugify `/S '<replacement>'`
6175
Remove all non-word and non-digit chars and merge words with specified *replacement* string. If no *replacement* argument is provided, Slugify uses `-` char as an argument.
6276

transform.js

Lines changed: 129 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,78 @@ function run(argv) {
77
const ARGUMENT = /(?:'([^'"]*)'|"([^'"]*)")/g;
88
const COMMAND_SEPARATOR = ' /';
99
const CYRILLIC_TO_LATIN_MAP = {
10-
'а': 'a', 'б': 'b', 'в': 'v', 'г': 'g', 'д': 'd', 'е': 'e', 'ё': 'e', 'ж': 'zh', 'з': 'z', 'и': 'i',
11-
'й': 'y', 'к': 'k', 'л': 'l', 'м': 'm', 'н': 'n', 'о': 'o', 'п': 'p', 'р': 'r', 'с': 's', 'т': 't',
12-
'у': 'u', 'ф': 'f', 'х': 'h', 'ц': 'c', 'ч': 'ch', 'ш': 'sh', 'щ': 'sch', 'ъ': '', 'ы': 'y', 'ь': '',
13-
'э': 'e', 'ю': 'yu', 'я': 'ya',
14-
'є': 'ye', 'ґ': 'g', 'ї': 'yi',
15-
'А': 'A', 'Б': 'B', 'В': 'V', 'Г': 'G', 'Д': 'D', 'Е': 'E', 'Ё': 'E', 'Ж': 'Zh', 'З': 'Z', 'И': 'I',
16-
'Й': 'Y', 'К': 'K', 'Л': 'L', 'М': 'M', 'Н': 'N', 'О': 'O', 'П': 'P', 'Р': 'R', 'С': 'S', 'Т': 'T',
17-
'У': 'U', 'Ф': 'F', 'Х': 'H', 'Ц': 'C', 'Ч': 'Ch', 'Ш': 'Sh', 'Щ': 'Sch', 'Ъ': '', 'Ы': 'Y', 'Ь': '',
18-
'Э': 'E', 'Ю': 'Yu', 'Я': 'Ya',
19-
'Є': 'Ye', 'Ґ': 'G', 'Ї': 'Yi'
10+
а: 'a',
11+
б: 'b',
12+
в: 'v',
13+
г: 'g',
14+
д: 'd',
15+
е: 'e',
16+
ё: 'e',
17+
ж: 'zh',
18+
з: 'z',
19+
и: 'i',
20+
й: 'y',
21+
к: 'k',
22+
л: 'l',
23+
м: 'm',
24+
н: 'n',
25+
о: 'o',
26+
п: 'p',
27+
р: 'r',
28+
с: 's',
29+
т: 't',
30+
у: 'u',
31+
ф: 'f',
32+
х: 'h',
33+
ц: 'c',
34+
ч: 'ch',
35+
ш: 'sh',
36+
щ: 'sch',
37+
ъ: '',
38+
ы: 'y',
39+
ь: '',
40+
э: 'e',
41+
ю: 'yu',
42+
я: 'ya',
43+
є: 'ye',
44+
ґ: 'g',
45+
ї: 'yi',
46+
А: 'A',
47+
Б: 'B',
48+
В: 'V',
49+
Г: 'G',
50+
Д: 'D',
51+
Е: 'E',
52+
Ё: 'E',
53+
Ж: 'Zh',
54+
З: 'Z',
55+
И: 'I',
56+
Й: 'Y',
57+
К: 'K',
58+
Л: 'L',
59+
М: 'M',
60+
Н: 'N',
61+
О: 'O',
62+
П: 'P',
63+
Р: 'R',
64+
С: 'S',
65+
Т: 'T',
66+
У: 'U',
67+
Ф: 'F',
68+
Х: 'H',
69+
Ц: 'C',
70+
Ч: 'Ch',
71+
Ш: 'Sh',
72+
Щ: 'Sch',
73+
Ъ: '',
74+
Ы: 'Y',
75+
Ь: '',
76+
Э: 'E',
77+
Ю: 'Yu',
78+
Я: 'Ya',
79+
Є: 'Ye',
80+
Ґ: 'G',
81+
Ї: 'Yi',
2082
};
2183

2284
let items = [];
@@ -27,9 +89,7 @@ function run(argv) {
2789

2890
const toPascalCase = (string = '') => {
2991
const words = string.match(WORD);
30-
return (words || [])
31-
.map((word) => `${word.charAt(0).toUpperCase()}${word.slice(1)}`)
32-
.join('');
92+
return (words || []).map((word) => `${word.charAt(0).toUpperCase()}${word.slice(1)}`).join('');
3393
};
3494

3595
const toLowerCase = (string = '') => {
@@ -42,12 +102,14 @@ function run(argv) {
42102

43103
const toCamelCase = (string = '') => {
44104
const words = string.match(WORD);
45-
return (words || []).map((word, index) => {
46-
if (index === 0) {
47-
return `${word.charAt(0).toLowerCase()}${word.slice(1)}`;
48-
}
49-
return `${word.charAt(0).toUpperCase()}${word.slice(1)}`;
50-
}).join('');
105+
return (words || [])
106+
.map((word, index) => {
107+
if (index === 0) {
108+
return `${word.charAt(0).toLowerCase()}${word.slice(1)}`;
109+
}
110+
return `${word.charAt(0).toUpperCase()}${word.slice(1)}`;
111+
})
112+
.join('');
51113
};
52114

53115
const toSnakeCase = (string = '') => {
@@ -66,7 +128,10 @@ function run(argv) {
66128

67129
for (let i = 0; i < string.length; i++) {
68130
const currentChar = string.charAt(i);
69-
capitalized += !wordChar.test(previousChar) && wordChar.test(currentChar) ? currentChar.toLocaleUpperCase() : currentChar;
131+
capitalized +=
132+
!wordChar.test(previousChar) && wordChar.test(currentChar)
133+
? currentChar.toLocaleUpperCase()
134+
: currentChar;
70135
previousChar = currentChar;
71136
}
72137

@@ -82,6 +147,14 @@ function run(argv) {
82147
return string.replaceAll(substring, replacement);
83148
};
84149

150+
const toEncodedURI = (string = '') => {
151+
return encodeURI(string);
152+
};
153+
154+
const toDecodedURI = (string = '') => {
155+
return decodeURI(string);
156+
};
157+
85158
const noArgCommands = {
86159
l: {
87160
name: 'Lowercase',
@@ -111,10 +184,18 @@ function run(argv) {
111184
name: 'Capitalize',
112185
transform: toCapitalized,
113186
},
114-
}
187+
e: {
188+
name: 'Encode URI',
189+
transform: toEncodedURI,
190+
},
191+
d: {
192+
name: 'Decode URI',
193+
transform: toDecodedURI,
194+
},
195+
};
115196

116-
const REQUIRED_ARGUMENT = ' (?:\'.*?\'|\".*?\")';
117-
const OPTIONAL_ARGUMENT = '(?: \'.*?\'| \".*?\")?';
197+
const REQUIRED_ARGUMENT = ' (?:\'.*?\'|".*?")';
198+
const OPTIONAL_ARGUMENT = '(?: \'.*?\'| ".*?")?';
118199

119200
const argCommands = {
120201
S: {
@@ -139,7 +220,9 @@ function run(argv) {
139220
};
140221

141222
const AVAILABLE_COMMANDS = new RegExp(
142-
`[${Object.keys(noArgCommands).join('')}]{1}|${Object.values(argCommands).map(({ pattern }) => pattern).join('|')}`,
223+
`[${Object.keys(noArgCommands).join('')}]{1}|${Object.values(argCommands)
224+
.map(({ pattern }) => pattern)
225+
.join('|')}`,
143226
'g'
144227
);
145228

@@ -153,8 +236,9 @@ function run(argv) {
153236
}
154237

155238
if (transformer.args && command.length > 1) {
156-
const commandArgs = [...command.matchAll(ARGUMENT)]
157-
.map((argMatch) => argMatch[1] ?? argMatch[2]); // matching single or double quotes
239+
const commandArgs = [...command.matchAll(ARGUMENT)].map(
240+
(argMatch) => argMatch[1] ?? argMatch[2]
241+
); // matching single or double quotes
158242
return transformer.transform.apply(null, [result, ...commandArgs]);
159243
}
160244

@@ -172,7 +256,7 @@ function run(argv) {
172256
if (transformer) {
173257
result.push(transformer.name);
174258
}
175-
return result
259+
return result;
176260
}, []);
177261
}
178262
};
@@ -197,21 +281,25 @@ function run(argv) {
197281
};
198282
try {
199283
const chainResult = runTransforms(string, commandsSequence);
200-
items = [{
201-
uid: 'chained',
202-
title: isMultilined(chainResult) ? 'Multiline output' : chainResult,
203-
subtitle,
204-
arg: chainResult,
205-
icon,
206-
}];
284+
items = [
285+
{
286+
uid: 'chained',
287+
title: isMultilined(chainResult) ? 'Multiline output' : chainResult,
288+
subtitle,
289+
arg: chainResult,
290+
icon,
291+
},
292+
];
207293
} catch {
208-
items = [{
209-
uid: 'error',
210-
title: 'Error',
211-
subtitle,
212-
arg: string,
213-
icon,
214-
}];
294+
items = [
295+
{
296+
uid: 'error',
297+
title: 'Error',
298+
subtitle,
299+
arg: string,
300+
icon,
301+
},
302+
];
215303
}
216304
} else {
217305
items = Object.values(allCommands).map((command) => {

0 commit comments

Comments
 (0)