Skip to content

Commit b7f17e8

Browse files
committed
Merge branch 'up/feat/unicode-to-java' into chore/all-my-stuffs
2 parents c5791b1 + e9a4d06 commit b7f17e8

File tree

8 files changed

+141
-4
lines changed

8 files changed

+141
-4
lines changed

components.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
// @ts-nocheck
44
// Generated by unplugin-vue-components
55
// Read more: https://github.com/vuejs/core/pull/3399
6-
import '@vue/runtime-core'
7-
86
export {}
97

10-
declare module '@vue/runtime-core' {
8+
declare module 'vue' {
119
export interface GlobalComponents {
1210
'404.page': typeof import('./src/pages/404.page.vue')['default']
1311
About: typeof import('./src/pages/About.vue')['default']

locales/en.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,4 +438,7 @@ tools:
438438

439439
multi-link-downloader:
440440
title: Multi link downloader
441-
description: Asynchronously downloads from multiple links into a zip file while a single link downloads directly. (Requires an internet connection)
441+
description: Asynchronously downloads from multiple links into a zip file while a single link downloads directly. (Requires an internet connection)
442+
unicode-to-java-entities:
443+
title: Unicode Characters to Java Entities Converter
444+
description: Unicode Characters to Java Entities Converter and vice-versa

locales/vi.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,7 @@ tools:
390390
text-to-binary:
391391
title: Chuyển đổi văn bản thành nhị phân ASCII
392392
description: Chuyển đổi văn bản thành biểu diễn nhị phân ASCII của nó và ngược lại.
393+
394+
unicode-to-java-entities:
395+
title: Chuyển đổi ký tự Unicode sang thực thể Java
396+
description: Chuyển đổi ký tự Unicode sang thực thể Java và ngược lại

src/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ import { tool as imageExifReader } from './image-exif-reader';
236236
import { tool as ipInRange } from './ip-in-range';
237237
import { tool as isbnValidatorAndParser } from './isbn-validator-and-parser';
238238
import { tool as yamlViewer } from './yaml-viewer';
239+
import { tool as unicodeToJavaEntities } from './unicode-characters-to-java-entities-converter';
239240
import { tool as barcodeReader } from './barcode-reader';
240241
import { tool as barcodeGenerator } from './barcode-generator';
241242
import { tool as htmlToMarkdown } from './html-to-markdown';
@@ -311,6 +312,7 @@ export const toolsByCategory: ToolCategory[] = [
311312
listComparer,
312313
tomlToJson,
313314
tomlToYaml,
315+
unicodeToJavaEntities,
314316
propertiesToYaml,
315317
uuidConverter,
316318
htmlToMarkdown,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { TextWrapDisabled } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
import { translate } from '@/plugins/i18n.plugin';
4+
5+
export const tool = defineTool({
6+
name: translate('tools.unicode-to-java-entities.title'),
7+
path: '/unicode-to-java-entities',
8+
description: translate('tools.unicode-to-java-entities.description'),
9+
keywords: ['java-entities', 'to', 'unicode', 'text'],
10+
component: () => import('./unicode-characters-to-java-entities.vue'),
11+
icon: TextWrapDisabled,
12+
createdAt: new Date('2024-05-16'),
13+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
function strlenFix(str: string): string {
2+
while (str.length < 4) {
3+
str = `0${str}`;
4+
}
5+
return str;
6+
}
7+
8+
function parseUnicodeToJavaEntities(source: string): string {
9+
let result = '';
10+
11+
for (let i = 0; i < source.length; i++) {
12+
const charCode = source.charCodeAt(i);
13+
if (charCode <= 127) {
14+
result += source.charAt(i);
15+
}
16+
else {
17+
result += `\\u${strlenFix(charCode.toString(16).toUpperCase())}`;
18+
}
19+
}
20+
return result;
21+
}
22+
23+
function parseJavaEntitiesToUnicode(source: string): string {
24+
let result = '';
25+
let state: 0 | 1 | 2 = 0;
26+
let chars = 0;
27+
let value = '';
28+
for (let i = 0; i < source.length; i++) {
29+
switch (state) {
30+
case 0:
31+
if (source.charAt(i) === '\\') {
32+
state = 1;
33+
}
34+
else {
35+
result += source.charAt(i);
36+
}
37+
break;
38+
case 1:
39+
if (source.charAt(i) === 'u') {
40+
state = 2;
41+
chars = 0;
42+
value = '';
43+
}
44+
else {
45+
result += `\\${source.charAt(i)}`;
46+
state = 0;
47+
}
48+
break;
49+
case 2:
50+
chars++;
51+
value += source.charAt(i);
52+
if (chars >= 4) {
53+
result += String.fromCharCode(Number.parseInt(value, 16));
54+
state = 0;
55+
}
56+
break;
57+
}
58+
}
59+
60+
return result;
61+
}
62+
63+
export { parseUnicodeToJavaEntities, parseJavaEntitiesToUnicode };
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 { parseJavaEntitiesToUnicode, parseUnicodeToJavaEntities } from './unicode-characters-to-java-entities.service';
3+
4+
describe('unicode-to-entities', () => {
5+
describe('convertTextToUnicode', () => {
6+
it('a unicode string is converted to java entities representation', () => {
7+
expect(parseUnicodeToJavaEntities('là')).toBe('l\\u00E0');
8+
expect(parseUnicodeToJavaEntities('sơn tùng MTP')).toBe('s\\u01A1n t\\u00F9ng MTP');
9+
expect(parseUnicodeToJavaEntities('')).toBe('');
10+
});
11+
});
12+
13+
describe('entities-to-unicode', () => {
14+
it('java entities string is converted to its unicode representation', () => {
15+
expect(parseJavaEntitiesToUnicode('l\u00E0')).toBe('là');
16+
expect(parseJavaEntitiesToUnicode('s\u01A1n t\u00F9ng MTP')).toBe('sơn tùng MTP');
17+
expect(parseJavaEntitiesToUnicode('')).toBe('');
18+
});
19+
});
20+
});
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 { parseJavaEntitiesToUnicode, parseUnicodeToJavaEntities } from './unicode-characters-to-java-entities.service';
3+
import { useCopy } from '@/composable/copy';
4+
5+
const inputUnicode = ref('');
6+
const entitiesFromUnicode = computed(() => inputUnicode.value.trim() === '' ? '' : parseUnicodeToJavaEntities(inputUnicode.value));
7+
const { copy: copyJavaEntities } = useCopy({ source: entitiesFromUnicode });
8+
9+
const inputJavaEntities = ref('');
10+
const unicodeFromEntities = computed(() => inputJavaEntities.value.trim() === '' ? '' : parseJavaEntitiesToUnicode(inputJavaEntities.value));
11+
const { copy: copyUnicode } = useCopy({ source: unicodeFromEntities });
12+
</script>
13+
14+
<template>
15+
<c-card title="Unicode Characters to Java entities">
16+
<c-input-text v-model:value="inputUnicode" placeholder="e.g. 'Hello Avengers'" label="Enter Unicode Characters to convert to Java entities" autosize raw-text multiline autofocus test-id="unicode-to-entities-input" />
17+
<c-input-text v-model:value="entitiesFromUnicode" label="Java entities from your text" placeholder="The unicode representation of your text will be here" raw-text multiline readonly mt-2 test-id="unicode-to-entities-output" />
18+
<div mt-2 flex justify-center>
19+
<c-button :disabled="!entitiesFromUnicode" @click="copyJavaEntities()">
20+
Copy Java entities to clipboard
21+
</c-button>
22+
</div>
23+
</c-card>
24+
25+
<c-card title="Java entities to Unicode Characters">
26+
<c-input-text v-model:value="inputJavaEntities" placeholder="Input Java entities" label="Enter Java entities to convert to Unicode Characters" autosize raw-text multiline test-id="entities-to-unicode-input" />
27+
<c-input-text v-model:value="unicodeFromEntities" label="Text from your Java entities" placeholder="The text representation of your unicode will be here" multiline raw-text readonly mt-2 test-id="entities-to-unicode-output" />
28+
<div mt-2 flex justify-center>
29+
<c-button :disabled="!unicodeFromEntities" @click="copyUnicode()">
30+
Copy Unicode to clipboard
31+
</c-button>
32+
</div>
33+
</c-card>
34+
</template>

0 commit comments

Comments
 (0)