Skip to content

Commit eaf979b

Browse files
hieudt-2054CorentinTh
authored andcommitted
feat(new-tool): added unicode conversion utilities (CorentinTh#858)
* feat: add Text to Unicode tool * Update src/tools/text-to-unicode/index.ts --------- Co-authored-by: Corentin THOMASSET <[email protected]>
1 parent 22e836b commit eaf979b

File tree

7 files changed

+103
-0
lines changed

7 files changed

+103
-0
lines changed

components.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ declare module '@vue/runtime-core' {
198198
TextStatistics: typeof import('./src/tools/text-statistics/text-statistics.vue')['default']
199199
TextToBinary: typeof import('./src/tools/text-to-binary/text-to-binary.vue')['default']
200200
TextToNatoAlphabet: typeof import('./src/tools/text-to-nato-alphabet/text-to-nato-alphabet.vue')['default']
201+
TextToUnicode: typeof import('./src/tools/text-to-unicode/text-to-unicode.vue')['default']
201202
TokenDisplay: typeof import('./src/tools/otp-code-generator-and-validator/token-display.vue')['default']
202203
'TokenGenerator.tool': typeof import('./src/tools/token-generator/token-generator.tool.vue')['default']
203204
TomlToJson: typeof import('./src/tools/toml-to-json/toml-to-json.vue')['default']

src/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { tool as base64FileConverter } from './base64-file-converter';
22
import { tool as base64StringConverter } from './base64-string-converter';
33
import { tool as basicAuthGenerator } from './basic-auth-generator';
4+
import { tool as textToUnicode } from './text-to-unicode';
45
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
56
import { tool as numeronymGenerator } from './numeronym-generator';
67
import { tool as macAddressGenerator } from './mac-address-generator';
@@ -93,6 +94,7 @@ export const toolsByCategory: ToolCategory[] = [
9394
caseConverter,
9495
textToNatoAlphabet,
9596
textToBinary,
97+
textToUnicode,
9698
yamlToJson,
9799
yamlToToml,
98100
jsonToYaml,

src/tools/text-to-unicode/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { TextWrap } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'Text to Unicode',
6+
path: '/text-to-unicode',
7+
description: 'Parse and convert text to unicode and vice-versa',
8+
keywords: ['text', 'to', 'unicode'],
9+
component: () => import('./text-to-unicode.vue'),
10+
icon: TextWrap,
11+
createdAt: new Date('2024-01-31'),
12+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { expect, test } from '@playwright/test';
2+
3+
test.describe('Tool - Text to Unicode', () => {
4+
test.beforeEach(async ({ page }) => {
5+
await page.goto('/text-to-unicode');
6+
});
7+
8+
test('Has correct title', async ({ page }) => {
9+
await expect(page).toHaveTitle('Text to Unicode - IT Tools');
10+
});
11+
12+
test('Text to unicode conversion', async ({ page }) => {
13+
await page.getByTestId('text-to-unicode-input').fill('it-tools');
14+
const unicode = await page.getByTestId('text-to-unicode-output').inputValue();
15+
16+
expect(unicode).toEqual('&#105;&#116;&#45;&#116;&#111;&#111;&#108;&#115;');
17+
});
18+
19+
test('Unicode to text conversion', async ({ page }) => {
20+
await page.getByTestId('unicode-to-text-input').fill('&#105;&#116;&#45;&#116;&#111;&#111;&#108;&#115;');
21+
const text = await page.getByTestId('unicode-to-text-output').inputValue();
22+
23+
expect(text).toEqual('it-tools');
24+
});
25+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { convertTextToUnicode, convertUnicodeToText } from './text-to-unicode.service';
3+
4+
describe('text-to-unicode', () => {
5+
describe('convertTextToUnicode', () => {
6+
it('a text string is converted to unicode representation', () => {
7+
expect(convertTextToUnicode('A')).toBe('&#65;');
8+
expect(convertTextToUnicode('linke the string convert to unicode')).toBe('&#108;&#105;&#110;&#107;&#101;&#32;&#116;&#104;&#101;&#32;&#115;&#116;&#114;&#105;&#110;&#103;&#32;&#99;&#111;&#110;&#118;&#101;&#114;&#116;&#32;&#116;&#111;&#32;&#117;&#110;&#105;&#99;&#111;&#100;&#101;');
9+
expect(convertTextToUnicode('')).toBe('');
10+
});
11+
});
12+
13+
describe('convertUnicodeToText', () => {
14+
it('an unicode string is converted to its text representation', () => {
15+
expect(convertUnicodeToText('&#65;')).toBe('A');
16+
expect(convertUnicodeToText('&#108;&#105;&#110;&#107;&#101;&#32;&#116;&#104;&#101;&#32;&#115;&#116;&#114;&#105;&#110;&#103;&#32;&#99;&#111;&#110;&#118;&#101;&#114;&#116;&#32;&#116;&#111;&#32;&#117;&#110;&#105;&#99;&#111;&#100;&#101;')).toBe('linke the string convert to unicode');
17+
expect(convertUnicodeToText('')).toBe('');
18+
});
19+
});
20+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function convertTextToUnicode(text: string): string {
2+
return text.split('').map(value => `&#${value.charCodeAt(0)};`).join('');
3+
}
4+
5+
function convertUnicodeToText(unicodeStr: string): string {
6+
return unicodeStr.replace(/&#(\d+);/g, (match, dec) => String.fromCharCode(dec));
7+
}
8+
9+
export { convertTextToUnicode, convertUnicodeToText };
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<script setup lang="ts">
2+
import { convertTextToUnicode, convertUnicodeToText } from './text-to-unicode.service';
3+
import { useCopy } from '@/composable/copy';
4+
5+
const inputText = ref('');
6+
const unicodeFromText = computed(() => inputText.value.trim() === '' ? '' : convertTextToUnicode(inputText.value));
7+
const { copy: copyUnicode } = useCopy({ source: unicodeFromText });
8+
9+
const inputUnicode = ref('');
10+
const textFromUnicode = computed(() => inputUnicode.value.trim() === '' ? '' : convertUnicodeToText(inputUnicode.value));
11+
const { copy: copyText } = useCopy({ source: textFromUnicode });
12+
</script>
13+
14+
<template>
15+
<c-card title="Text to Unicode">
16+
<c-input-text v-model:value="inputText" multiline placeholder="e.g. 'Hello Avengers'" label="Enter text to convert to binary" autosize autofocus raw-text test-id="text-to-unicode-input" />
17+
<c-input-text v-model:value="unicodeFromText" label="Unicode from your text" multiline raw-text readonly mt-2 placeholder="The unicode representation of your text will be here" test-id="text-to-unicode-output" />
18+
<div mt-2 flex justify-center>
19+
<c-button :disabled="!unicodeFromText" @click="copyUnicode()">
20+
Copy binary to clipboard
21+
</c-button>
22+
</div>
23+
</c-card>
24+
25+
<c-card title="Unicode to Text">
26+
<c-input-text v-model:value="inputUnicode" multiline placeholder="Input Unicode" label="Enter unicode to convert to text" autosize raw-text test-id="unicode-to-text-input" />
27+
<c-input-text v-model:value="textFromUnicode" label="Text from your Unicode" multiline raw-text readonly mt-2 placeholder="The text representation of your unicode will be here" test-id="unicode-to-text-output" />
28+
<div mt-2 flex justify-center>
29+
<c-button :disabled="!textFromUnicode" @click="copyText()">
30+
Copy text to clipboard
31+
</c-button>
32+
</div>
33+
</c-card>
34+
</template>

0 commit comments

Comments
 (0)