mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-24 06:40:20 +03:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
19efa59aa9 | ||
|
|
bb6995320c | ||
|
|
f9cc935f14 | ||
|
|
9501a60dc0 |
No files matched your search
@@ -286,9 +286,6 @@
|
||||
"watchTriggerable": true,
|
||||
"watchWithFilter": true,
|
||||
"whenever": true,
|
||||
"toValue": true,
|
||||
"injectLocal": true,
|
||||
"provideLocal": true,
|
||||
"useClipboardItems": true
|
||||
"toValue": true
|
||||
}
|
||||
}
|
||||
Vendored
-9
@@ -36,7 +36,6 @@ declare global {
|
||||
const h: typeof import('vue')['h']
|
||||
const ignorableWatch: typeof import('@vueuse/core')['ignorableWatch']
|
||||
const inject: typeof import('vue')['inject']
|
||||
const injectLocal: typeof import('@vueuse/core')['injectLocal']
|
||||
const isDefined: typeof import('@vueuse/core')['isDefined']
|
||||
const isProxy: typeof import('vue')['isProxy']
|
||||
const isReactive: typeof import('vue')['isReactive']
|
||||
@@ -66,7 +65,6 @@ declare global {
|
||||
const onUpdated: typeof import('vue')['onUpdated']
|
||||
const pausableWatch: typeof import('@vueuse/core')['pausableWatch']
|
||||
const provide: typeof import('vue')['provide']
|
||||
const provideLocal: typeof import('@vueuse/core')['provideLocal']
|
||||
const reactify: typeof import('@vueuse/core')['reactify']
|
||||
const reactifyObject: typeof import('@vueuse/core')['reactifyObject']
|
||||
const reactive: typeof import('vue')['reactive']
|
||||
@@ -130,7 +128,6 @@ declare global {
|
||||
const useBrowserLocation: typeof import('@vueuse/core')['useBrowserLocation']
|
||||
const useCached: typeof import('@vueuse/core')['useCached']
|
||||
const useClipboard: typeof import('@vueuse/core')['useClipboard']
|
||||
const useClipboardItems: typeof import('@vueuse/core')['useClipboardItems']
|
||||
const useCloned: typeof import('@vueuse/core')['useCloned']
|
||||
const useColorMode: typeof import('@vueuse/core')['useColorMode']
|
||||
const useConfirmDialog: typeof import('@vueuse/core')['useConfirmDialog']
|
||||
@@ -329,7 +326,6 @@ declare module 'vue' {
|
||||
readonly h: UnwrapRef<typeof import('vue')['h']>
|
||||
readonly ignorableWatch: UnwrapRef<typeof import('@vueuse/core')['ignorableWatch']>
|
||||
readonly inject: UnwrapRef<typeof import('vue')['inject']>
|
||||
readonly injectLocal: UnwrapRef<typeof import('@vueuse/core')['injectLocal']>
|
||||
readonly isDefined: UnwrapRef<typeof import('@vueuse/core')['isDefined']>
|
||||
readonly isProxy: UnwrapRef<typeof import('vue')['isProxy']>
|
||||
readonly isReactive: UnwrapRef<typeof import('vue')['isReactive']>
|
||||
@@ -359,7 +355,6 @@ declare module 'vue' {
|
||||
readonly onUpdated: UnwrapRef<typeof import('vue')['onUpdated']>
|
||||
readonly pausableWatch: UnwrapRef<typeof import('@vueuse/core')['pausableWatch']>
|
||||
readonly provide: UnwrapRef<typeof import('vue')['provide']>
|
||||
readonly provideLocal: UnwrapRef<typeof import('@vueuse/core')['provideLocal']>
|
||||
readonly reactify: UnwrapRef<typeof import('@vueuse/core')['reactify']>
|
||||
readonly reactifyObject: UnwrapRef<typeof import('@vueuse/core')['reactifyObject']>
|
||||
readonly reactive: UnwrapRef<typeof import('vue')['reactive']>
|
||||
@@ -423,7 +418,6 @@ declare module 'vue' {
|
||||
readonly useBrowserLocation: UnwrapRef<typeof import('@vueuse/core')['useBrowserLocation']>
|
||||
readonly useCached: UnwrapRef<typeof import('@vueuse/core')['useCached']>
|
||||
readonly useClipboard: UnwrapRef<typeof import('@vueuse/core')['useClipboard']>
|
||||
readonly useClipboardItems: UnwrapRef<typeof import('@vueuse/core')['useClipboardItems']>
|
||||
readonly useCloned: UnwrapRef<typeof import('@vueuse/core')['useCloned']>
|
||||
readonly useColorMode: UnwrapRef<typeof import('@vueuse/core')['useColorMode']>
|
||||
readonly useConfirmDialog: UnwrapRef<typeof import('@vueuse/core')['useConfirmDialog']>
|
||||
@@ -616,7 +610,6 @@ declare module '@vue/runtime-core' {
|
||||
readonly h: UnwrapRef<typeof import('vue')['h']>
|
||||
readonly ignorableWatch: UnwrapRef<typeof import('@vueuse/core')['ignorableWatch']>
|
||||
readonly inject: UnwrapRef<typeof import('vue')['inject']>
|
||||
readonly injectLocal: UnwrapRef<typeof import('@vueuse/core')['injectLocal']>
|
||||
readonly isDefined: UnwrapRef<typeof import('@vueuse/core')['isDefined']>
|
||||
readonly isProxy: UnwrapRef<typeof import('vue')['isProxy']>
|
||||
readonly isReactive: UnwrapRef<typeof import('vue')['isReactive']>
|
||||
@@ -646,7 +639,6 @@ declare module '@vue/runtime-core' {
|
||||
readonly onUpdated: UnwrapRef<typeof import('vue')['onUpdated']>
|
||||
readonly pausableWatch: UnwrapRef<typeof import('@vueuse/core')['pausableWatch']>
|
||||
readonly provide: UnwrapRef<typeof import('vue')['provide']>
|
||||
readonly provideLocal: UnwrapRef<typeof import('@vueuse/core')['provideLocal']>
|
||||
readonly reactify: UnwrapRef<typeof import('@vueuse/core')['reactify']>
|
||||
readonly reactifyObject: UnwrapRef<typeof import('@vueuse/core')['reactifyObject']>
|
||||
readonly reactive: UnwrapRef<typeof import('vue')['reactive']>
|
||||
@@ -710,7 +702,6 @@ declare module '@vue/runtime-core' {
|
||||
readonly useBrowserLocation: UnwrapRef<typeof import('@vueuse/core')['useBrowserLocation']>
|
||||
readonly useCached: UnwrapRef<typeof import('@vueuse/core')['useCached']>
|
||||
readonly useClipboard: UnwrapRef<typeof import('@vueuse/core')['useClipboard']>
|
||||
readonly useClipboardItems: UnwrapRef<typeof import('@vueuse/core')['useClipboardItems']>
|
||||
readonly useCloned: UnwrapRef<typeof import('@vueuse/core')['useCloned']>
|
||||
readonly useColorMode: UnwrapRef<typeof import('@vueuse/core')['useColorMode']>
|
||||
readonly useConfirmDialog: UnwrapRef<typeof import('@vueuse/core')['useConfirmDialog']>
|
||||
|
||||
Vendored
+3
-3
@@ -37,6 +37,7 @@ declare module '@vue/runtime-core' {
|
||||
'CFileUpload.demo': typeof import('./src/ui/c-file-upload/c-file-upload.demo.vue')['default']
|
||||
ChmodCalculator: typeof import('./src/tools/chmod-calculator/chmod-calculator.vue')['default']
|
||||
Chronometer: typeof import('./src/tools/chronometer/chronometer.vue')['default']
|
||||
CidrInCidr: typeof import('./src/tools/cidr-in-cidr/cidr-in-cidr.vue')['default']
|
||||
CInputText: typeof import('./src/ui/c-input-text/c-input-text.vue')['default']
|
||||
'CInputText.demo': typeof import('./src/ui/c-input-text/c-input-text.demo.vue')['default']
|
||||
CKeyValueList: typeof import('./src/ui/c-key-value-list/c-key-value-list.vue')['default']
|
||||
@@ -103,6 +104,7 @@ declare module '@vue/runtime-core' {
|
||||
IconMdiTriangleDown: typeof import('~icons/mdi/triangle-down')['default']
|
||||
InputCopyable: typeof import('./src/components/InputCopyable.vue')['default']
|
||||
IntegerBaseConverter: typeof import('./src/tools/integer-base-converter/integer-base-converter.vue')['default']
|
||||
IpInRange: typeof import('./src/tools/cidr-in-cidr/cidr-in-cidr.vue')['default']
|
||||
Ipv4AddressConverter: typeof import('./src/tools/ipv4-address-converter/ipv4-address-converter.vue')['default']
|
||||
Ipv4RangeExpander: typeof import('./src/tools/ipv4-range-expander/ipv4-range-expander.vue')['default']
|
||||
Ipv4SubnetCalculator: typeof import('./src/tools/ipv4-subnet-calculator/ipv4-subnet-calculator.vue')['default']
|
||||
@@ -130,19 +132,16 @@ declare module '@vue/runtime-core' {
|
||||
MetaTagGenerator: typeof import('./src/tools/meta-tag-generator/meta-tag-generator.vue')['default']
|
||||
MimeTypes: typeof import('./src/tools/mime-types/mime-types.vue')['default']
|
||||
NavbarButtons: typeof import('./src/components/NavbarButtons.vue')['default']
|
||||
NCheckbox: typeof import('naive-ui')['NCheckbox']
|
||||
NCollapseTransition: typeof import('naive-ui')['NCollapseTransition']
|
||||
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
|
||||
NDivider: typeof import('naive-ui')['NDivider']
|
||||
NEllipsis: typeof import('naive-ui')['NEllipsis']
|
||||
NFormItem: typeof import('naive-ui')['NFormItem']
|
||||
NH1: typeof import('naive-ui')['NH1']
|
||||
NH3: typeof import('naive-ui')['NH3']
|
||||
NIcon: typeof import('naive-ui')['NIcon']
|
||||
NLayout: typeof import('naive-ui')['NLayout']
|
||||
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
|
||||
NMenu: typeof import('naive-ui')['NMenu']
|
||||
NSpace: typeof import('naive-ui')['NSpace']
|
||||
NTable: typeof import('naive-ui')['NTable']
|
||||
NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default']
|
||||
OtpCodeGeneratorAndValidator: typeof import('./src/tools/otp-code-generator-and-validator/otp-code-generator-and-validator.vue')['default']
|
||||
@@ -162,6 +161,7 @@ declare module '@vue/runtime-core' {
|
||||
RouterView: typeof import('vue-router')['RouterView']
|
||||
RsaKeyPairGenerator: typeof import('./src/tools/rsa-key-pair-generator/rsa-key-pair-generator.vue')['default']
|
||||
SafelinkDecoder: typeof import('./src/tools/safelink-decoder/safelink-decoder.vue')['default']
|
||||
SafelinkDecoder: typeof import('./src/tools/safelink-decoder/safelink-decoder.vue')['default']
|
||||
SlugifyString: typeof import('./src/tools/slugify-string/slugify-string.vue')['default']
|
||||
SpanCopyable: typeof import('./src/components/SpanCopyable.vue')['default']
|
||||
SqlPrettify: typeof import('./src/tools/sql-prettify/sql-prettify.vue')['default']
|
||||
|
||||
@@ -70,6 +70,7 @@
|
||||
"highlight.js": "^11.7.0",
|
||||
"iarna-toml-esm": "^3.0.5",
|
||||
"ibantools": "^4.3.3",
|
||||
"ip-matching": "^2.1.2",
|
||||
"js-base64": "^3.7.6",
|
||||
"json5": "^2.2.3",
|
||||
"jwt-decode": "^3.1.2",
|
||||
|
||||
Generated
+2601
-2490
File diff suppressed because it is too large.
Load diff
@@ -128,7 +128,7 @@ function activateOption(option: PaletteOption) {
|
||||
<c-input-text ref="inputRef" v-model:value="searchPrompt" raw-text placeholder="Type to search a tool or a command..." autofocus clearable />
|
||||
|
||||
<div v-for="(options, category) in filteredSearchResult" :key="category">
|
||||
<div ml-3 mt-3 text-sm text-primary font-bold op-60>
|
||||
<div ml-3 mt-3 text-sm font-bold text-primary op-60>
|
||||
{{ category }}
|
||||
</div>
|
||||
<command-palette-option v-for="option in options" :key="option.name" :option="option" :selected="selectedOptionIndex === getOptionIndex(option)" @activated="activateOption" />
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { cidrInCidr } from './cidr-in-cidr.service';
|
||||
|
||||
describe('cidr-in-cidr', () => {
|
||||
describe('cidrInCidr', () => {
|
||||
it('should return correct inclusion', () => {
|
||||
expect(cidrInCidr({ baseRange: '192.168.0.0/16', ipOrRangeToTest: '192.168.0.1' }).isIncluded).toBe(true); // NOSONAR
|
||||
expect(cidrInCidr({ baseRange: '192.168.1.0/24', ipOrRangeToTest: '192.168.0.10' }).isIncluded).toBe(false); // NOSONAR
|
||||
expect(cidrInCidr({ baseRange: '192.168.1.*', ipOrRangeToTest: '192.168.0.10' }).isIncluded).toBe(false); // NOSONAR
|
||||
|
||||
expect(cidrInCidr({ baseRange: '192.168.0.0/24', ipOrRangeToTest: '192.168.0.0/28' }).isIncluded).toBe(true); // NOSONAR
|
||||
expect(cidrInCidr({ baseRange: '192.168.20.0/24', ipOrRangeToTest: '192.168.1.0/28' }).isIncluded).toBe(false); // NOSONAR
|
||||
expect(cidrInCidr({ baseRange: '192.168.20.*', ipOrRangeToTest: '192.168.1.0/28' }).isIncluded).toBe(false); // NOSONAR
|
||||
expect(cidrInCidr({ baseRange: '192.168.0.*', ipOrRangeToTest: '192.168.0.0/28' }).isIncluded).toBe(true); // NOSONAR
|
||||
|
||||
expect(cidrInCidr({ baseRange: '192.168.20.1-192.168.20.15', ipOrRangeToTest: '192.168.20.12' }).isIncluded).toBe(true); // NOSONAR
|
||||
expect(cidrInCidr({ baseRange: '192.168.20.1-192.168.20.15', ipOrRangeToTest: '192.168.20.20' }).isIncluded).toBe(false); // NOSONAR
|
||||
|
||||
expect(cidrInCidr({ baseRange: '192.168.20.1-192.168.20.15', ipOrRangeToTest: '192.168.20.9-192.168.20.12' }).isIncluded).toBe(true); // NOSONAR
|
||||
expect(cidrInCidr({ baseRange: '192.168.20.1-192.168.20.15', ipOrRangeToTest: '192.168.20.9-192.168.20.20' }).isIncluded).toBe(false); // NOSONAR
|
||||
|
||||
expect(cidrInCidr({ baseRange: '192.168.20.0-192.168.20.255', ipOrRangeToTest: '192.168.20.0/28' }).isIncluded).toBe(true); // NOSONAR
|
||||
expect(cidrInCidr({ baseRange: '192.168.20.0-192.168.20.255', ipOrRangeToTest: '192.168.1.0/28' }).isIncluded).toBe(false); // NOSONAR
|
||||
expect(cidrInCidr({ baseRange: '192.168.0.0-192.168.1.255', ipOrRangeToTest: '192.168.1.0/28' }).isIncluded).toBe(true); // NOSONAR
|
||||
});
|
||||
it('should return correct subnet', () => {
|
||||
expect(cidrInCidr({ baseRange: '192.168.0.0/16', ipOrRangeToTest: '192.168.0.1' }).baseSubnets).to.deep.eq([ // NOSONAR
|
||||
{
|
||||
cidr: '192.168.0.0/16', // NOSONAR
|
||||
end: '192.168.255.255', // NOSONAR
|
||||
start: '192.168.0.0', // NOSONAR
|
||||
},
|
||||
]);
|
||||
expect(cidrInCidr({ baseRange: '192.168.20.1-192.168.20.3', ipOrRangeToTest: '192.168.1.0/28' }).baseSubnets).to.deep.eq([ // NOSONAR
|
||||
{
|
||||
cidr: '192.168.20.1/32', // NOSONAR
|
||||
end: '192.168.20.1', // NOSONAR
|
||||
start: '192.168.20.1', // NOSONAR
|
||||
},
|
||||
{
|
||||
cidr: '192.168.20.2/31', // NOSONAR
|
||||
end: '192.168.20.3', // NOSONAR
|
||||
start: '192.168.20.2', // NOSONAR
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
import { getMatch } from 'ip-matching';
|
||||
|
||||
export function cidrInCidr(
|
||||
{ baseRange, ipOrRangeToTest }:
|
||||
{
|
||||
baseRange: string
|
||||
ipOrRangeToTest: string
|
||||
}) {
|
||||
const baseMatchMasks = getMatch(baseRange)?.convertToMasks() || [];
|
||||
const ipOrRangeToTestMatchMasks = getMatch(ipOrRangeToTest)?.convertToMasks() || [];
|
||||
|
||||
const baseSubnets = baseMatchMasks.map((mask) => {
|
||||
const subnet = mask.convertToSubnet();
|
||||
if (!subnet) {
|
||||
return { cidr: '', start: '', end: '' };
|
||||
}
|
||||
return {
|
||||
cidr: subnet.toString(),
|
||||
start: subnet.getFirst().toString(),
|
||||
end: subnet.getLast().toString(),
|
||||
};
|
||||
}).filter(subnet => subnet.cidr !== '');
|
||||
|
||||
return {
|
||||
baseSubnets,
|
||||
isIncluded: baseMatchMasks.some(baseMask => ipOrRangeToTestMatchMasks.every(ipOrRangeMask => ipOrRangeMask.isSubsetOf(baseMask))),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<script setup lang="ts">
|
||||
import { useStorage } from '@vueuse/core';
|
||||
import { Check as CheckIcon, LetterX as CrossIcon } from '@vicons/tabler';
|
||||
import { getMatch } from 'ip-matching';
|
||||
import { cidrInCidr } from './cidr-in-cidr.service';
|
||||
import { withDefaultOnError } from '@/utils/defaults';
|
||||
import { isNotThrowing } from '@/utils/boolean';
|
||||
import SpanCopyable from '@/components/SpanCopyable.vue';
|
||||
|
||||
const baseRange = useStorage('cidr-in-cidr:range', '192.168.0.1/24'); // NOSONAR
|
||||
const ipOrRangeToTest = useStorage('cidr-in-cidr:ip', '192.168.0.1'); // NOSONAR
|
||||
|
||||
const matchResult = computed(() => withDefaultOnError(
|
||||
() => cidrInCidr({ baseRange: baseRange.value, ipOrRangeToTest: ipOrRangeToTest.value }),
|
||||
{ baseSubnets: [], isIncluded: false }));
|
||||
|
||||
const rangeValidationRules = [
|
||||
{
|
||||
message: 'We cannot parse this CIDR/IP Range/Mask/Wildcard, check the format',
|
||||
validator: (value: string) => isNotThrowing(() => getMatch(value)) && getMatch(value),
|
||||
},
|
||||
];
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<c-input-text
|
||||
v-model:value="baseRange"
|
||||
label="An IPv4/6 CIDR/Range/Mask/Wildcard (base network)"
|
||||
placeholder="The ipv4/6 CIDR..."
|
||||
:validation-rules="rangeValidationRules"
|
||||
mb-4
|
||||
/>
|
||||
|
||||
<c-input-text
|
||||
v-model:value="ipOrRangeToTest"
|
||||
label="An IPv4/6 CIDR/Range/Mask/Wildcard (to test for inclusion)"
|
||||
placeholder="The An IPv4/6 CIDR/Range/Mask/Wildcard..."
|
||||
:validation-rules="rangeValidationRules"
|
||||
mb-4
|
||||
/>
|
||||
|
||||
<n-divider />
|
||||
|
||||
<div flex justify-center>
|
||||
<span v-if="matchResult.isIncluded">
|
||||
<n-icon color="green">
|
||||
<CheckIcon />
|
||||
</n-icon>
|
||||
Included
|
||||
</span>
|
||||
<span v-else>
|
||||
<n-icon color="red">
|
||||
<CrossIcon />
|
||||
</n-icon>
|
||||
Not included
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<n-divider />
|
||||
|
||||
<c-card title="Subnets">
|
||||
<n-table>
|
||||
<tbody>
|
||||
<tr v-for="{ cidr, start, end } in matchResult.baseSubnets" :key="cidr">
|
||||
<td font-bold>
|
||||
<SpanCopyable :value="cidr" />
|
||||
</td>
|
||||
<td>
|
||||
<SpanCopyable :value="start" />
|
||||
</td>
|
||||
<td>
|
||||
<SpanCopyable :value="end" />
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</n-table>
|
||||
</c-card>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,13 @@
|
||||
import { UnfoldMoreOutlined } from '@vicons/material';
|
||||
import { defineTool } from '../tool';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: 'IPv4-6/IPRange/CIDR in IPRange/CIDR/IPMask',
|
||||
path: '/cidr-in-cidr',
|
||||
description: 'Given a CIDR/IP Range/Wildcard IP/IP Mask, tell if a given IPv4-6/Range/CIDR/Wildcard IP/IP Mask is in subnet range',
|
||||
keywords: ['ip', 'cidr', 'range', 'mask', 'wildcard', 'ipv4', 'ipv6', 'subnet', 'include', 'inclusion'],
|
||||
component: () => import('./cidr-in-cidr.vue'),
|
||||
redirectFrom: ['/ip-in-range'],
|
||||
icon: UnfoldMoreOutlined,
|
||||
createdAt: new Date('2025-01-12'),
|
||||
});
|
||||
+10
-1
@@ -86,6 +86,7 @@ import { tool as urlParser } from './url-parser';
|
||||
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 cidrInCidr } from './cidr-in-cidr';
|
||||
import { tool as yamlViewer } from './yaml-viewer';
|
||||
|
||||
export const toolsByCategory: ToolCategory[] = [
|
||||
@@ -164,7 +165,15 @@ export const toolsByCategory: ToolCategory[] = [
|
||||
},
|
||||
{
|
||||
name: 'Network',
|
||||
components: [ipv4SubnetCalculator, ipv4AddressConverter, ipv4RangeExpander, macAddressLookup, macAddressGenerator, ipv6UlaGenerator],
|
||||
components: [
|
||||
ipv4SubnetCalculator,
|
||||
ipv4AddressConverter,
|
||||
cidrInCidr,
|
||||
ipv4RangeExpander,
|
||||
macAddressLookup,
|
||||
macAddressGenerator,
|
||||
ipv6UlaGenerator,
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'Math',
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { expect, test } from '@playwright/test';
|
||||
|
||||
test.describe('Tool - Text to UTF-8 binary', () => {
|
||||
test.describe('Tool - Text to ASCII binary', () => {
|
||||
test.beforeEach(async ({ page }) => {
|
||||
await page.goto('/text-to-binary');
|
||||
});
|
||||
|
||||
test('Has correct title', async ({ page }) => {
|
||||
await expect(page).toHaveTitle('Text to UTF-8 binary - IT Tools');
|
||||
await expect(page).toHaveTitle('Text to ASCII binary - IT Tools');
|
||||
});
|
||||
|
||||
test('Text to binary conversion', async ({ page }) => {
|
||||
@@ -17,9 +17,7 @@ test.describe('Tool - Text to UTF-8 binary', () => {
|
||||
});
|
||||
|
||||
test('Binary to text conversion', async ({ page }) => {
|
||||
await page
|
||||
.getByTestId('binary-to-text-input')
|
||||
.fill('01101001 01110100 00101101 01110100 01101111 01101111 01101100 01110011');
|
||||
await page.getByTestId('binary-to-text-input').fill('01101001 01110100 00101101 01110100 01101111 01101111 01101100 01110011');
|
||||
const text = await page.getByTestId('binary-to-text-output').inputValue();
|
||||
|
||||
expect(text).toEqual('it-tools');
|
||||
|
||||
@@ -1,103 +1,32 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { convertTextToUtf8Binary, convertUtf8BinaryToText } from './text-to-binary.models';
|
||||
import { convertAsciiBinaryToText, convertTextToAsciiBinary } from './text-to-binary.models';
|
||||
|
||||
describe('text-to-binary', () => {
|
||||
const utf8Tests = [
|
||||
{
|
||||
text: '文字',
|
||||
binary: '11100110 10010110 10000111 11100101 10101101 10010111',
|
||||
decimal: '',
|
||||
octal: '',
|
||||
hex: '',
|
||||
},
|
||||
{
|
||||
text: '💩',
|
||||
binary: '11110000 10011111 10010010 10101001',
|
||||
decimal: '',
|
||||
octal: '',
|
||||
hex: '',
|
||||
},
|
||||
];
|
||||
|
||||
describe('convertTextToUtf8Binary', () => {
|
||||
it('a text string is converted to its UTF-8 binary representation', () => {
|
||||
expect(convertTextToUtf8Binary('A')).toBe('01000001');
|
||||
expect(convertTextToUtf8Binary('A', { base: 8 })).toBe('0101');
|
||||
expect(convertTextToUtf8Binary('A', { base: 10 })).toBe('65');
|
||||
expect(convertTextToUtf8Binary('A', { base: 16 })).toBe('41');
|
||||
expect(convertTextToUtf8Binary('hello')).toBe('01101000 01100101 01101100 01101100 01101111');
|
||||
expect(convertTextToUtf8Binary('hello', { base: 8 })).toBe('0150 0145 0154 0154 0157');
|
||||
expect(convertTextToUtf8Binary('hello', { base: 10 })).toBe('104 101 108 108 111');
|
||||
expect(convertTextToUtf8Binary('hello', { base: 16 })).toBe('68 65 6c 6c 6f');
|
||||
expect(convertTextToUtf8Binary('')).toBe('');
|
||||
describe('convertTextToAsciiBinary', () => {
|
||||
it('a text string is converted to its ascii binary representation', () => {
|
||||
expect(convertTextToAsciiBinary('A')).toBe('01000001');
|
||||
expect(convertTextToAsciiBinary('hello')).toBe('01101000 01100101 01101100 01101100 01101111');
|
||||
expect(convertTextToAsciiBinary('')).toBe('');
|
||||
});
|
||||
it('the separator between octets can be changed', () => {
|
||||
expect(convertTextToUtf8Binary('hello', { separator: '' })).toBe('0110100001100101011011000110110001101111');
|
||||
expect(convertTextToUtf8Binary('hello', { separator: '-' })).toBe('01101000-01100101-01101100-01101100-01101111');
|
||||
expect(convertTextToUtf8Binary('hello', { separator: '-', base: 16 })).toBe('68-65-6c-6c-6f');
|
||||
});
|
||||
it('works with non-ASCII input', () => {
|
||||
for (const { text, binary } of utf8Tests) {
|
||||
const converted = convertTextToUtf8Binary(text);
|
||||
expect(converted).toBe(binary);
|
||||
}
|
||||
|
||||
expect(convertTextToUtf8Binary('💩 A', { base: 2 })).toBe('11110000 10011111 10010010 10101001 00100000 01000001');
|
||||
expect(convertTextToUtf8Binary('💩 A', { base: 8 })).toBe('0360 0237 0222 0251 040 0101');
|
||||
expect(convertTextToUtf8Binary('💩 A', { base: 10 })).toBe('240 159 146 169 32 65');
|
||||
expect(convertTextToUtf8Binary('💩 A', { base: 16 })).toBe('f0 9f 92 a9 20 41');
|
||||
expect(convertTextToAsciiBinary('hello', { separator: '' })).toBe('0110100001100101011011000110110001101111');
|
||||
});
|
||||
});
|
||||
|
||||
describe('convertUtf8BinaryToText', () => {
|
||||
describe('convertAsciiBinaryToText', () => {
|
||||
it('an ascii binary string is converted to its text representation', () => {
|
||||
expect(convertUtf8BinaryToText('01101000 01100101 01101100 01101100 01101111')).toBe('hello');
|
||||
expect(convertUtf8BinaryToText('01101000 01100101 01101100 01101100 01101111', { base: 2 })).toBe('hello');
|
||||
expect(convertUtf8BinaryToText('0150 0145 0154 0154 0157', { base: 8 })).toBe('hello');
|
||||
expect(convertUtf8BinaryToText('104 101 108 108 111', { base: 10 })).toBe('hello');
|
||||
expect(convertUtf8BinaryToText('68 65 6c 6c 6f', { base: 16 })).toBe('hello');
|
||||
|
||||
expect(convertUtf8BinaryToText('11110000 10011111 10010010 10101001 00100000 01000001', { base: 2 })).toBe('💩 A');
|
||||
expect(convertUtf8BinaryToText('0360 0237 0222 0251 040 0101', { base: 8 })).toBe('💩 A');
|
||||
expect(convertUtf8BinaryToText('240 159 146 169 32 65', { base: 10 })).toBe('💩 A');
|
||||
expect(convertUtf8BinaryToText('f0 9f 92 a9 20 41', { base: 16 })).toBe('💩 A');
|
||||
|
||||
expect(convertUtf8BinaryToText('')).toBe('');
|
||||
|
||||
expect(convertUtf8BinaryToText('01000001')).toBe('A');
|
||||
expect(convertUtf8BinaryToText('0101', { base: 8 })).toBe('A');
|
||||
expect(convertUtf8BinaryToText('65', { base: 10 })).toBe('A');
|
||||
expect(convertUtf8BinaryToText('41', { base: 16 })).toBe('A');
|
||||
expect(convertAsciiBinaryToText('01101000 01100101 01101100 01101100 01101111')).toBe('hello');
|
||||
expect(convertAsciiBinaryToText('01000001')).toBe('A');
|
||||
expect(convertTextToAsciiBinary('')).toBe('');
|
||||
});
|
||||
|
||||
it('the given string is cleaned before conversion', () => {
|
||||
expect(convertUtf8BinaryToText(' 01000 001garbage')).toBe('A');
|
||||
expect(convertUtf8BinaryToText(' 65garbage', { base: 10 })).toBe('A');
|
||||
expect(convertUtf8BinaryToText(' 41xxxx', { base: 16 })).toBe('A');
|
||||
it('the given binary string is cleaned before conversion', () => {
|
||||
expect(convertAsciiBinaryToText(' 01000 001garbage')).toBe('A');
|
||||
});
|
||||
|
||||
it('throws an error if the given binary string is not an integer number of complete octets', () => {
|
||||
expect(() => convertUtf8BinaryToText('010000011')).toThrow('Invalid binary string');
|
||||
expect(() => convertUtf8BinaryToText('010000011 010000011')).toThrow('Invalid binary string');
|
||||
expect(() => convertUtf8BinaryToText('1')).toThrow('Invalid binary string');
|
||||
});
|
||||
|
||||
it('throws an error if the given binary string is not valid UTF-8', () => {
|
||||
expect(() => convertUtf8BinaryToText('11111111')).toThrow();
|
||||
});
|
||||
|
||||
it('the given string is cleaned from prefix before conversion', () => {
|
||||
expect(convertUtf8BinaryToText('0b01000001')).toBe('A');
|
||||
expect(convertUtf8BinaryToText('0x41', { base: 16 })).toBe('A');
|
||||
expect(convertUtf8BinaryToText('0x68 0x65 0x6c 0x6c 0x6f', { base: 16 })).toBe('hello');
|
||||
expect(convertUtf8BinaryToText('\\x68\\x65\\x6c\\x6c\\x6f', { base: 16 })).toBe('hello');
|
||||
});
|
||||
|
||||
it('works with non-ASCII input', () => {
|
||||
for (const { text, binary } of utf8Tests) {
|
||||
const reverted = convertUtf8BinaryToText(binary);
|
||||
expect(reverted).toBe(text);
|
||||
}
|
||||
it('throws an error if the given binary string as no complete octet', () => {
|
||||
expect(() => convertAsciiBinaryToText('010000011')).toThrow('Invalid binary string');
|
||||
expect(() => convertAsciiBinaryToText('1')).toThrow('Invalid binary string');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,67 +1,22 @@
|
||||
export { convertTextToUtf8Binary, convertUtf8BinaryToText };
|
||||
export { convertTextToAsciiBinary, convertAsciiBinaryToText };
|
||||
|
||||
export type EncodingBase = 2 | 8 | 10 | 16;
|
||||
|
||||
function convertTextToUtf8Binary(text: string, { separator = ' ', base = 2 }: { separator?: string; base?: EncodingBase } = {}): string {
|
||||
if (!text?.trim()) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return [...new TextEncoder().encode(text)].map((char) => {
|
||||
const charInBase = char.toString(base);
|
||||
if (base === 2) {
|
||||
return charInBase.padStart(8, '0');
|
||||
}
|
||||
if (base === 8) {
|
||||
return `0${charInBase}`;
|
||||
}
|
||||
if (base === 16) {
|
||||
return charInBase.padStart(2, '0');
|
||||
}
|
||||
|
||||
return charInBase;
|
||||
}).join(separator);
|
||||
function convertTextToAsciiBinary(text: string, { separator = ' ' }: { separator?: string } = {}): string {
|
||||
return text
|
||||
.split('')
|
||||
.map(char => char.charCodeAt(0).toString(2).padStart(8, '0'))
|
||||
.join(separator);
|
||||
}
|
||||
|
||||
function convertUtf8BinaryToText(binary: string, { base = 2 }: { base?: EncodingBase } = {}): string {
|
||||
if (!binary?.trim()) {
|
||||
return '';
|
||||
function convertAsciiBinaryToText(binary: string): string {
|
||||
const cleanBinary = binary.replace(/[^01]/g, '');
|
||||
|
||||
if (cleanBinary.length % 8) {
|
||||
throw new Error('Invalid binary string');
|
||||
}
|
||||
|
||||
let codepoints: number[] = [];
|
||||
if (base === 2) {
|
||||
const cleanBinary = binary.replace(/0b/g, '').replace(/[^01]/g, '').trim();
|
||||
|
||||
if (cleanBinary.length % 8) {
|
||||
throw new Error('Invalid binary string');
|
||||
}
|
||||
|
||||
codepoints = cleanBinary
|
||||
.split(/([01]{8})/)
|
||||
.filter(Boolean)
|
||||
.map(binary => Number.parseInt(binary, 2));
|
||||
}
|
||||
else if (base === 16) {
|
||||
const cleanBinary = binary.replace(/0x|\\x/g, '').replace(/[^0-9A-Fa-f]/g, '');
|
||||
|
||||
if (cleanBinary.length % 2) {
|
||||
throw new Error('Invalid hexadecimal string');
|
||||
}
|
||||
|
||||
codepoints = cleanBinary
|
||||
.split(/([0-9A-Fa-f]{2})/)
|
||||
.filter(Boolean)
|
||||
.map(binary => Number.parseInt(binary, 16));
|
||||
}
|
||||
else {
|
||||
const cleanBinary = binary.replace(/0o/g, '').replace(/[^\d\s]/g, '');
|
||||
codepoints = cleanBinary
|
||||
.split(/\s/)
|
||||
.filter(Boolean)
|
||||
.map(binary => Number.parseInt(binary, base));
|
||||
}
|
||||
|
||||
return new TextDecoder(undefined, { fatal: true }).decode(
|
||||
Uint8Array.from(codepoints),
|
||||
);
|
||||
return cleanBinary
|
||||
.split(/(\d{8})/)
|
||||
.filter(Boolean)
|
||||
.map(binary => String.fromCharCode(Number.parseInt(binary, 2)))
|
||||
.join('');
|
||||
}
|
||||
@@ -1,95 +1,42 @@
|
||||
<script setup lang="ts">
|
||||
import { type EncodingBase, convertTextToUtf8Binary, convertUtf8BinaryToText } from './text-to-binary.models';
|
||||
import { convertAsciiBinaryToText, convertTextToAsciiBinary } from './text-to-binary.models';
|
||||
import { withDefaultOnError } from '@/utils/defaults';
|
||||
import { useCopy } from '@/composable/copy';
|
||||
import { isNotThrowing } from '@/utils/boolean';
|
||||
import { useQueryParamOrStorage } from '@/composable/queryParams';
|
||||
|
||||
const base = useQueryParamOrStorage({ name: 'base', storageName: 'txt-bin:base', defaultValue: '2' });
|
||||
const inputText = ref('');
|
||||
const binaryFromText = computed(() => convertTextToUtf8Binary(inputText.value, { base: Number(base.value) as EncodingBase }));
|
||||
const binaryFromText = computed(() => convertTextToAsciiBinary(inputText.value));
|
||||
const { copy: copyBinary } = useCopy({ source: binaryFromText });
|
||||
|
||||
const inputBinary = ref('');
|
||||
const textFromBinary = computed(() => withDefaultOnError(() => convertUtf8BinaryToText(inputBinary.value, { base: Number(base.value) as EncodingBase }), ''));
|
||||
const textFromBinary = computed(() => withDefaultOnError(() => convertAsciiBinaryToText(inputBinary.value), ''));
|
||||
const inputBinaryValidationRules = [
|
||||
{
|
||||
validator: (value: string) => isNotThrowing(() => convertUtf8BinaryToText(value)),
|
||||
message: 'Binary should be a valid UTF-8 binary string with multiples of 8 bits',
|
||||
validator: (value: string) => isNotThrowing(() => convertAsciiBinaryToText(value)),
|
||||
message: 'Binary should be a valid ASCII binary string with multiples of 8 bits',
|
||||
},
|
||||
];
|
||||
const { copy: copyText } = useCopy({ source: textFromBinary });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<c-select
|
||||
v-model:value="base"
|
||||
label="Conversion Base:"
|
||||
label-position="left"
|
||||
mb-2
|
||||
:options="[
|
||||
{ value: '2', label: 'Binary' },
|
||||
{ value: '8', label: 'Octal' },
|
||||
{ value: '10', label: 'Decimal' },
|
||||
{ value: '16', label: 'Hexadecimal' },
|
||||
]"
|
||||
/>
|
||||
<c-card title="Text to ASCII binary">
|
||||
<c-input-text v-model:value="inputText" multiline placeholder="e.g. 'Hello world'" label="Enter text to convert to binary" autosize autofocus raw-text test-id="text-to-binary-input" />
|
||||
<c-input-text v-model:value="binaryFromText" label="Binary from your text" multiline raw-text readonly mt-2 placeholder="The binary representation of your text will be here" test-id="text-to-binary-output" />
|
||||
<div mt-2 flex justify-center>
|
||||
<c-button :disabled="!binaryFromText" @click="copyBinary()">
|
||||
Copy binary to clipboard
|
||||
</c-button>
|
||||
</div>
|
||||
</c-card>
|
||||
|
||||
<c-card title="Text to UTF-8 binary">
|
||||
<c-input-text
|
||||
v-model:value="inputText"
|
||||
multiline
|
||||
placeholder="e.g. 'Hello world'"
|
||||
label="Enter text to convert to binary"
|
||||
autosize
|
||||
autofocus
|
||||
raw-text
|
||||
test-id="text-to-binary-input"
|
||||
/>
|
||||
<c-input-text
|
||||
v-model:value="binaryFromText"
|
||||
label="Binary from your text"
|
||||
multiline
|
||||
raw-text
|
||||
readonly
|
||||
mt-2
|
||||
placeholder="The binary representation of your text will be here"
|
||||
test-id="text-to-binary-output"
|
||||
/>
|
||||
<div mt-2 flex justify-center>
|
||||
<c-button :disabled="!binaryFromText" @click="copyBinary()">
|
||||
Copy binary to clipboard
|
||||
</c-button>
|
||||
</div>
|
||||
</c-card>
|
||||
|
||||
<c-card title="UTF-8 binary to text">
|
||||
<c-input-text
|
||||
v-model:value="inputBinary"
|
||||
multiline
|
||||
placeholder="e.g. '01001000 01100101 01101100 01101100 01101111'"
|
||||
label="Enter binary to convert to text"
|
||||
autosize
|
||||
raw-text
|
||||
:validation-rules="inputBinaryValidationRules"
|
||||
test-id="binary-to-text-input"
|
||||
/>
|
||||
<c-input-text
|
||||
v-model:value="textFromBinary"
|
||||
label="Text from your binary"
|
||||
multiline
|
||||
raw-text
|
||||
readonly
|
||||
mt-2
|
||||
placeholder="The text representation of your binary will be here"
|
||||
test-id="binary-to-text-output"
|
||||
/>
|
||||
<div mt-2 flex justify-center>
|
||||
<c-button :disabled="!textFromBinary" @click="copyText()">
|
||||
Copy text to clipboard
|
||||
</c-button>
|
||||
</div>
|
||||
</c-card>
|
||||
</div>
|
||||
<c-card title="ASCII binary to text">
|
||||
<c-input-text v-model:value="inputBinary" multiline placeholder="e.g. '01001000 01100101 01101100 01101100 01101111'" label="Enter binary to convert to text" autosize raw-text :validation-rules="inputBinaryValidationRules" test-id="binary-to-text-input" />
|
||||
<c-input-text v-model:value="textFromBinary" label="Text from your binary" multiline raw-text readonly mt-2 placeholder="The text representation of your binary will be here" test-id="binary-to-text-output" />
|
||||
<div mt-2 flex justify-center>
|
||||
<c-button :disabled="!textFromBinary" @click="copyText()">
|
||||
Copy text to clipboard
|
||||
</c-button>
|
||||
</div>
|
||||
</c-card>
|
||||
</template>
|
||||
@@ -10,18 +10,16 @@ test.describe('Tool - Text to Unicode', () => {
|
||||
});
|
||||
|
||||
test('Text to unicode conversion', async ({ page }) => {
|
||||
await page.getByTestId('text-to-unicode-input').fill('"it-tools" 文字');
|
||||
await page.getByTestId('text-to-unicode-input').fill('it-tools');
|
||||
const unicode = await page.getByTestId('text-to-unicode-output').inputValue();
|
||||
|
||||
// eslint-disable-next-line unicorn/escape-case
|
||||
expect(unicode).toEqual(String.raw`\u0022it-tools\u0022 \u6587\u5b57`);
|
||||
expect(unicode).toEqual('it-tools');
|
||||
});
|
||||
|
||||
test('Unicode to text conversion', async ({ page }) => {
|
||||
// eslint-disable-next-line unicorn/escape-case
|
||||
await page.getByTestId('unicode-to-text-input').fill(String.raw`\u0022it-tools\u0022 \u6587\u5b57`);
|
||||
await page.getByTestId('unicode-to-text-input').fill('it-tools');
|
||||
const text = await page.getByTestId('unicode-to-text-output').inputValue();
|
||||
|
||||
expect(text).toEqual('"it-tools" 文字');
|
||||
expect(text).toEqual('it-tools');
|
||||
});
|
||||
});
|
||||
@@ -5,18 +5,7 @@ describe('text-to-unicode', () => {
|
||||
describe('convertTextToUnicode', () => {
|
||||
it('a text string is converted to unicode representation', () => {
|
||||
expect(convertTextToUnicode('A')).toBe('A');
|
||||
expect(convertTextToUnicode('💩 AĀ')).toBe('💩 AĀ');
|
||||
expect(convertTextToUnicode('💩 AĀ', { encoding: 'antiuni' })).toBe('\\u1f4a9\\u20\\u41\\u0100');
|
||||
expect(convertTextToUnicode('💩 AĀ', { encoding: 'css' })).toBe('\\01f4a9\\000020\\000041\\000100');
|
||||
expect(convertTextToUnicode('💩 AĀ', { encoding: 'htmldec' })).toBe('💩 AĀ');
|
||||
expect(convertTextToUnicode('💩 AĀ', { encoding: 'htmlhex' })).toBe('💩 AĀ');
|
||||
expect(convertTextToUnicode('💩 AĀ', { encoding: 'uniplus' })).toBe('U+1f4a9 U+00020 U+00041 U+00100');
|
||||
expect(convertTextToUnicode('💩 AĀ', { encoding: 'python' })).toBe('\\U1f4a9\\x20\\x41\\u0100');
|
||||
expect(convertTextToUnicode('💩 AĀ', { encoding: 'js' })).toBe('\\u{1f4a9}\\u0020\\u0041\\u0100');
|
||||
expect(convertTextToUnicode('💩 AĀ', { encoding: 'utf16' })).toBe('\\ud83d\\udca9\\u0020\\u0041\\u0100');
|
||||
expect(convertTextToUnicode('💩 hello AĀ', { skipAscii: true })).toBe('💩 hello AĀ');
|
||||
expect(convertTextToUnicode('linke the string convert to unicode')).toBe(
|
||||
'linke the string convert to unicode');
|
||||
expect(convertTextToUnicode('linke the string convert to unicode')).toBe('linke the string convert to unicode');
|
||||
expect(convertTextToUnicode('')).toBe('');
|
||||
});
|
||||
});
|
||||
@@ -24,16 +13,6 @@ describe('text-to-unicode', () => {
|
||||
describe('convertUnicodeToText', () => {
|
||||
it('an unicode string is converted to its text representation', () => {
|
||||
expect(convertUnicodeToText('A')).toBe('A');
|
||||
expect(convertUnicodeToText('\\u1f4a9\\u20\\u41\\u0100')).toBe('💩 AĀ');
|
||||
expect(convertUnicodeToText('\\01f4a9\\000020\\000041\\000100')).toBe('💩 AĀ');
|
||||
expect(convertUnicodeToText('💩 AĀ')).toBe('💩 AĀ');
|
||||
expect(convertUnicodeToText('💩 AĀ')).toBe('💩 AĀ');
|
||||
expect(convertUnicodeToText('U+1f4a9 U+00020 U+00041 U+00100')).toBe('💩 AĀ');
|
||||
expect(convertUnicodeToText('\\U1f4a9\\x20\\x41\\u0100')).toBe('💩 AĀ');
|
||||
expect(convertUnicodeToText('\\u{1f4a9}\\u0020\\u0041\\u0100')).toBe('💩 AĀ');
|
||||
expect(convertUnicodeToText('\\ud83d\\udca9\\u0020\\u0041\\u0100')).toBe('💩 AĀ');
|
||||
expect(convertUnicodeToText('\\01f4a9 AĀ')).toBe('💩 AĀ');
|
||||
expect(convertUnicodeToText('💩 hello AĀ')).toBe('💩 hello AĀ');
|
||||
expect(convertUnicodeToText('linke the string convert to unicode')).toBe('linke the string convert to unicode');
|
||||
expect(convertUnicodeToText('')).toBe('');
|
||||
});
|
||||
|
||||
@@ -1,66 +1,9 @@
|
||||
export type Encoding = 'htmldec' | 'htmlhex' | 'uniplus' | 'antiuni' | 'css' | 'python' | 'js' | 'utf16';
|
||||
|
||||
const ALL_PRINTABLE_ASCII = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
|
||||
|
||||
function convertTextToUnicode(text: string, { encoding = 'htmldec', skipAscii = false }: { encoding?: Encoding; skipAscii?: boolean } = {}): string {
|
||||
let prefix: (value: number) => string;
|
||||
let suffix: (value: number) => string = () => '';
|
||||
let base = 16;
|
||||
let padding: (value: number) => number = () => 0;
|
||||
let separator = '';
|
||||
let codepoints = [...text];
|
||||
if (encoding === 'htmldec') {
|
||||
prefix = () => '&#';
|
||||
base = 10;
|
||||
suffix = () => ';';
|
||||
}
|
||||
else if (encoding === 'htmlhex') {
|
||||
prefix = () => '&#x';
|
||||
suffix = () => ';';
|
||||
}
|
||||
else if (encoding === 'uniplus') {
|
||||
prefix = () => 'U+';
|
||||
padding = () => 5;
|
||||
separator = ' ';
|
||||
}
|
||||
else if (encoding === 'antiuni') {
|
||||
prefix = () => '\\u';
|
||||
padding = (value: number) => value < 256 ? 2 : 4;
|
||||
}
|
||||
else if (encoding === 'utf16') {
|
||||
prefix = () => '\\u';
|
||||
padding = () => 4;
|
||||
codepoints = text.split('');
|
||||
}
|
||||
else if (encoding === 'python') {
|
||||
prefix = (value: number) => value < 256 ? '\\x' : (value < 65536 ? '\\u' : '\\U');
|
||||
padding = (value: number) => value < 256 ? 2 : 4;
|
||||
}
|
||||
else if (encoding === 'js') {
|
||||
prefix = (value: number) => value < 65536 ? '\\u' : '\\u{';
|
||||
suffix = (value: number) => value < 65536 ? '' : '}';
|
||||
padding = () => 4;
|
||||
}
|
||||
else if (encoding === 'css') {
|
||||
prefix = () => '\\';
|
||||
padding = () => 6;
|
||||
}
|
||||
|
||||
return codepoints.map((value) => {
|
||||
if (skipAscii && ALL_PRINTABLE_ASCII.includes(value)) {
|
||||
return value;
|
||||
}
|
||||
const charCode = value.codePointAt(0) || 0xFF;
|
||||
return `${prefix(charCode)}${charCode.toString(base).padStart(padding(charCode), '0')}${suffix(charCode)}`;
|
||||
}).join(separator);
|
||||
function convertTextToUnicode(text: string): string {
|
||||
return text.split('').map(value => `&#${value.charCodeAt(0)};`).join('');
|
||||
}
|
||||
|
||||
function convertUnicodeToText(unicodeStr: string): string {
|
||||
return unicodeStr
|
||||
.replace(/&#(\d+);/g, (_, dec) => String.fromCodePoint(dec))
|
||||
.replace(/&#[xX]([0-9A-Fa-f]+);/g, (_, hex) => String.fromCodePoint(Number.parseInt(hex, 16)))
|
||||
.replace(/\\u\{([0-9A-Fa-f]+)\}/g, (_, hex) => String.fromCodePoint(Number.parseInt(hex, 16)))
|
||||
.replace(/(?:\\[uUx]|\\|\s*U\+)([0-9A-Fa-f]+)/g, (match, hex) => String.fromCodePoint(Number.parseInt(hex, 16))); // NOSONAR
|
||||
return unicodeStr.replace(/&#(\d+);/g, (match, dec) => String.fromCharCode(dec));
|
||||
}
|
||||
|
||||
export { convertTextToUnicode, convertUnicodeToText };
|
||||
@@ -1,46 +1,18 @@
|
||||
<script setup lang="ts">
|
||||
import { type Encoding, convertTextToUnicode, convertUnicodeToText } from './text-to-unicode.service';
|
||||
import { useQueryParamOrStorage } from '@/composable/queryParams';
|
||||
import { convertTextToUnicode, convertUnicodeToText } from './text-to-unicode.service';
|
||||
import { useCopy } from '@/composable/copy';
|
||||
|
||||
const encoding = useQueryParamOrStorage({ name: 'enc', storageName: 'txt-uni:enc', defaultValue: 'htmldec' });
|
||||
const skipAscii = useQueryParamOrStorage({ name: 'skipAscii', storageName: 'txt-uni:asc', defaultValue: false });
|
||||
|
||||
const inputText = ref('');
|
||||
const unicodeFromText = computed(() => inputText.value.trim() === ''
|
||||
? ''
|
||||
: convertTextToUnicode(inputText.value, { encoding: encoding.value as Encoding, skipAscii: skipAscii.value }));
|
||||
const unicodeFromText = computed(() => inputText.value.trim() === '' ? '' : convertTextToUnicode(inputText.value));
|
||||
const { copy: copyUnicode } = useCopy({ source: unicodeFromText });
|
||||
|
||||
const inputUnicode = ref('');
|
||||
const textFromUnicode = computed(() => inputUnicode.value.trim() === ''
|
||||
? ''
|
||||
: convertUnicodeToText(inputUnicode.value));
|
||||
const textFromUnicode = computed(() => inputUnicode.value.trim() === '' ? '' : convertUnicodeToText(inputUnicode.value));
|
||||
const { copy: copyText } = useCopy({ source: textFromUnicode });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<c-card title="Text to Unicode">
|
||||
<c-select
|
||||
v-model:value="encoding"
|
||||
label="Unicode encoding"
|
||||
:options="[
|
||||
{ value: 'htmldec', label: 'HTML Decimal (&#160;)' },
|
||||
{ value: 'htmlhex', label: 'HTML Hexadecimal (&#xA0;)' },
|
||||
{ value: 'uniplus', label: 'U+00A0' },
|
||||
{ value: 'antiuni', label: '\\u00A0' },
|
||||
{ value: 'css', label: 'CSS (\\0000A0)' },
|
||||
{ value: 'python', label: 'Python (\\xA0, \\u00A0, \\U100A0)' },
|
||||
{ value: 'js', label: 'Python (\\u00A0; \\u{100A0})' },
|
||||
{ value: 'utf16', label: 'UTF 16 (with surrogates)' },
|
||||
]"
|
||||
/>
|
||||
<n-form-item>
|
||||
<n-checkbox v-model:checked="skipAscii">
|
||||
Skip Ascii characters
|
||||
</n-checkbox>
|
||||
</n-form-item>
|
||||
|
||||
<c-input-text v-model:value="inputText" multiline placeholder="e.g. 'Hello Avengers'" label="Enter text to convert to unicode" autosize autofocus raw-text test-id="text-to-unicode-input" />
|
||||
<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" />
|
||||
<div mt-2 flex justify-center>
|
||||
|
||||
@@ -151,7 +151,7 @@ function onSearchInput() {
|
||||
>
|
||||
<div flex-1 truncate>
|
||||
<slot name="displayed-value">
|
||||
<input v-if="searchable && isOpen" ref="searchInputRef" v-model="searchQuery" type="text" placeholder="Search..." class="search-input" w-full color-current lh-normal @input="onSearchInput">
|
||||
<input v-if="searchable && isOpen" ref="searchInputRef" v-model="searchQuery" type="text" placeholder="Search..." class="search-input" w-full lh-normal color-current @input="onSearchInput">
|
||||
<span v-else-if="selectedOption" lh-normal>
|
||||
{{ selectedOption.label }}
|
||||
</span>
|
||||
|
||||
@@ -39,7 +39,7 @@ const headers = computed(() => {
|
||||
<template>
|
||||
<div class="relative overflow-x-auto rounded">
|
||||
<table class="w-full border-collapse text-left text-sm text-gray-500 dark:text-gray-400" role="table" :aria-label="description">
|
||||
<thead v-if="!hideHeaders" class="bg-#ffffff text-gray-700 uppercase dark:bg-#333333 dark:text-gray-400" border-b="1px solid dark:transparent #efeff5">
|
||||
<thead v-if="!hideHeaders" class="bg-#ffffff uppercase text-gray-700 dark:bg-#333333 dark:text-gray-400" border-b="1px solid dark:transparent #efeff5">
|
||||
<tr>
|
||||
<th v-for="header in headers" :key="header.key" scope="col" class="px-6 py-3 text-xs">
|
||||
{{ header.label }}
|
||||
|
||||
Reference in new issue
Block a user