mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-04 13:29:13 -04:00
Merge e9a4d06d39
into 76a19d218d
This commit is contained in:
commit
d92036f62a
8 changed files with 142 additions and 3 deletions
5
components.d.ts
vendored
5
components.d.ts
vendored
|
@ -3,11 +3,9 @@
|
|||
// @ts-nocheck
|
||||
// Generated by unplugin-vue-components
|
||||
// Read more: https://github.com/vuejs/core/pull/3399
|
||||
import '@vue/runtime-core'
|
||||
|
||||
export {}
|
||||
|
||||
declare module '@vue/runtime-core' {
|
||||
declare module 'vue' {
|
||||
export interface GlobalComponents {
|
||||
'404.page': typeof import('./src/pages/404.page.vue')['default']
|
||||
About: typeof import('./src/pages/About.vue')['default']
|
||||
|
@ -179,6 +177,7 @@ declare module '@vue/runtime-core' {
|
|||
'Tool.layout': typeof import('./src/layouts/tool.layout.vue')['default']
|
||||
ToolCard: typeof import('./src/components/ToolCard.vue')['default']
|
||||
UlidGenerator: typeof import('./src/tools/ulid-generator/ulid-generator.vue')['default']
|
||||
UnicodeCharactersToJavaEntities: typeof import('./src/tools/unicode-characters-to-java-entities-converter/unicode-characters-to-java-entities.vue')['default']
|
||||
UrlEncoder: typeof import('./src/tools/url-encoder/url-encoder.vue')['default']
|
||||
UrlParser: typeof import('./src/tools/url-parser/url-parser.vue')['default']
|
||||
UserAgentParser: typeof import('./src/tools/user-agent-parser/user-agent-parser.vue')['default']
|
||||
|
|
|
@ -391,3 +391,7 @@ tools:
|
|||
text-to-binary:
|
||||
title: Text to ASCII binary
|
||||
description: Convert text to its ASCII binary representation and vice-versa.
|
||||
|
||||
unicode-to-java-entities:
|
||||
title: Unicode Characters to Java Entities Converter
|
||||
description: Unicode Characters to Java Entities Converter and vice-versa
|
|
@ -380,3 +380,7 @@ tools:
|
|||
text-to-binary:
|
||||
title: Chuyển đổi văn bản thành nhị phân ASCII
|
||||
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.
|
||||
|
||||
unicode-to-java-entities:
|
||||
title: Chuyển đổi ký tự Unicode sang thực thể Java
|
||||
description: Chuyển đổi ký tự Unicode sang thực thể Java và ngược lại
|
|
@ -81,6 +81,7 @@ import { tool as uuidGenerator } from './uuid-generator';
|
|||
import { tool as macAddressLookup } from './mac-address-lookup';
|
||||
import { tool as xmlFormatter } from './xml-formatter';
|
||||
import { tool as yamlViewer } from './yaml-viewer';
|
||||
import { tool as unicodeToJavaEntities } from './unicode-characters-to-java-entities-converter';
|
||||
|
||||
export const toolsByCategory: ToolCategory[] = [
|
||||
{
|
||||
|
@ -107,6 +108,7 @@ export const toolsByCategory: ToolCategory[] = [
|
|||
listConverter,
|
||||
tomlToJson,
|
||||
tomlToYaml,
|
||||
unicodeToJavaEntities,
|
||||
],
|
||||
},
|
||||
{
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
import { TextWrapDisabled } from '@vicons/tabler';
|
||||
import { defineTool } from '../tool';
|
||||
import { translate } from '@/plugins/i18n.plugin';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: translate('tools.unicode-to-java-entities.title'),
|
||||
path: '/unicode-to-java-entities',
|
||||
description: translate('tools.unicode-to-java-entities.description'),
|
||||
keywords: ['java-entities', 'to', 'unicode', 'text'],
|
||||
component: () => import('./unicode-characters-to-java-entities.vue'),
|
||||
icon: TextWrapDisabled,
|
||||
createdAt: new Date('2024-05-16'),
|
||||
});
|
|
@ -0,0 +1,63 @@
|
|||
function strlenFix(str: string): string {
|
||||
while (str.length < 4) {
|
||||
str = `0${str}`;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
function parseUnicodeToJavaEntities(source: string): string {
|
||||
let result = '';
|
||||
|
||||
for (let i = 0; i < source.length; i++) {
|
||||
const charCode = source.charCodeAt(i);
|
||||
if (charCode <= 127) {
|
||||
result += source.charAt(i);
|
||||
}
|
||||
else {
|
||||
result += `\\u${strlenFix(charCode.toString(16).toUpperCase())}`;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function parseJavaEntitiesToUnicode(source: string): string {
|
||||
let result = '';
|
||||
let state: 0 | 1 | 2 = 0;
|
||||
let chars = 0;
|
||||
let value = '';
|
||||
for (let i = 0; i < source.length; i++) {
|
||||
switch (state) {
|
||||
case 0:
|
||||
if (source.charAt(i) === '\\') {
|
||||
state = 1;
|
||||
}
|
||||
else {
|
||||
result += source.charAt(i);
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if (source.charAt(i) === 'u') {
|
||||
state = 2;
|
||||
chars = 0;
|
||||
value = '';
|
||||
}
|
||||
else {
|
||||
result += `\\${source.charAt(i)}`;
|
||||
state = 0;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
chars++;
|
||||
value += source.charAt(i);
|
||||
if (chars >= 4) {
|
||||
result += String.fromCharCode(Number.parseInt(value, 16));
|
||||
state = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export { parseUnicodeToJavaEntities, parseJavaEntitiesToUnicode };
|
|
@ -0,0 +1,20 @@
|
|||
import { describe, expect, it } from 'vitest';
|
||||
import { parseJavaEntitiesToUnicode, parseUnicodeToJavaEntities } from './unicode-characters-to-java-entities.service';
|
||||
|
||||
describe('unicode-to-entities', () => {
|
||||
describe('convertTextToUnicode', () => {
|
||||
it('a unicode string is converted to java entities representation', () => {
|
||||
expect(parseUnicodeToJavaEntities('là')).toBe('l\\u00E0');
|
||||
expect(parseUnicodeToJavaEntities('sơn tùng MTP')).toBe('s\\u01A1n t\\u00F9ng MTP');
|
||||
expect(parseUnicodeToJavaEntities('')).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('entities-to-unicode', () => {
|
||||
it('java entities string is converted to its unicode representation', () => {
|
||||
expect(parseJavaEntitiesToUnicode('l\u00E0')).toBe('là');
|
||||
expect(parseJavaEntitiesToUnicode('s\u01A1n t\u00F9ng MTP')).toBe('sơn tùng MTP');
|
||||
expect(parseJavaEntitiesToUnicode('')).toBe('');
|
||||
});
|
||||
});
|
||||
});
|
|
@ -0,0 +1,34 @@
|
|||
<script setup lang="ts">
|
||||
import { parseJavaEntitiesToUnicode, parseUnicodeToJavaEntities } from './unicode-characters-to-java-entities.service';
|
||||
import { useCopy } from '@/composable/copy';
|
||||
|
||||
const inputUnicode = ref('');
|
||||
const entitiesFromUnicode = computed(() => inputUnicode.value.trim() === '' ? '' : parseUnicodeToJavaEntities(inputUnicode.value));
|
||||
const { copy: copyJavaEntities } = useCopy({ source: entitiesFromUnicode });
|
||||
|
||||
const inputJavaEntities = ref('');
|
||||
const unicodeFromEntities = computed(() => inputJavaEntities.value.trim() === '' ? '' : parseJavaEntitiesToUnicode(inputJavaEntities.value));
|
||||
const { copy: copyUnicode } = useCopy({ source: unicodeFromEntities });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<c-card title="Unicode Characters to Java entities">
|
||||
<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" />
|
||||
<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" />
|
||||
<div mt-2 flex justify-center>
|
||||
<c-button :disabled="!entitiesFromUnicode" @click="copyJavaEntities()">
|
||||
Copy Java entities to clipboard
|
||||
</c-button>
|
||||
</div>
|
||||
</c-card>
|
||||
|
||||
<c-card title="Java entities to Unicode Characters">
|
||||
<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" />
|
||||
<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" />
|
||||
<div mt-2 flex justify-center>
|
||||
<c-button :disabled="!unicodeFromEntities" @click="copyUnicode()">
|
||||
Copy Unicode to clipboard
|
||||
</c-button>
|
||||
</div>
|
||||
</c-card>
|
||||
</template>
|
Loading…
Add table
Add a link
Reference in a new issue