feat(i18n): url parser

This commit is contained in:
halfcoke 2023-11-25 15:06:34 +08:00
parent 7a70dbbe0c
commit cc5874232e
4 changed files with 43 additions and 13 deletions

View file

@ -6,7 +6,7 @@ type ValidatorReturnType = unknown;
export interface UseValidationRule<T> {
validator: (value: T) => ValidatorReturnType
message: string
message: any
}
export function isFalsyOrHasThrown(cb: () => ValidatorReturnType): boolean {
@ -56,7 +56,6 @@ export function useValidation<T>({
watch(
[source, ...watchRefs],
() => {
state.message = '';
state.status = undefined;
for (const rule of get(rules)) {

View file

@ -0,0 +1,15 @@
tools:
url-parser:
title: 'Url parser'
description: 'Parse an url string to get all the different parts (protocol, origin, params, port, username-password, ...)'
your-url: 'Your url to parse:'
protocol: 'Protocol'
username: 'Username'
password: 'Password'
hostname: 'Hostname'
port: 'Port'
path: 'Path'
params: 'Params'
placeholder:
your-url: 'Your url to parse...'
invalid-url: 'Invalid url'

View file

@ -0,0 +1,15 @@
tools:
url-parser:
title: 'URL解析'
description: '解析URL字符串以获取所有不同的部分协议、来源、参数、端口、用户名、密码等。'
your-url: '准备解析的URL:'
protocol: '协议'
username: '用户名'
password: '密码'
hostname: '主机名'
port: '端口'
path: '路径'
params: '参数'
placeholder:
your-url: '准备解析的URL...'
invalid-url: 'URL无效'

View file

@ -3,24 +3,25 @@ import InputCopyable from '../../components/InputCopyable.vue';
import { isNotThrowing } from '@/utils/boolean';
import { withDefaultOnError } from '@/utils/defaults';
const { t } = useI18n();
const urlToParse = ref('https://me:pwd@it-tools.tech:3000/url-parser?key1=value&key2=value2#the-hash');
const urlParsed = computed(() => withDefaultOnError(() => new URL(urlToParse.value), undefined));
const urlValidationRules = [
{
validator: (value: string) => isNotThrowing(() => new URL(value)),
message: 'Invalid url',
message: computed(() => t('tools.url-parser.invalid-url')),
},
];
const properties: { title: string; key: keyof URL }[] = [
{ title: 'Protocol', key: 'protocol' },
{ title: 'Username', key: 'username' },
{ title: 'Password', key: 'password' },
{ title: 'Hostname', key: 'hostname' },
{ title: 'Port', key: 'port' },
{ title: 'Path', key: 'pathname' },
{ title: 'Params', key: 'search' },
const properties: { title: ComputedRef<string>; key: keyof URL }[] = [
{ title: computed(() => t('tools.url-parser.protocol')), key: 'protocol' },
{ title: computed(() => t('tools.url-parser.username')), key: 'username' },
{ title: computed(() => t('tools.url-parser.password')), key: 'password' },
{ title: computed(() => t('tools.url-parser.hostname')), key: 'hostname' },
{ title: computed(() => t('tools.url-parser.port')), key: 'port' },
{ title: computed(() => t('tools.url-parser.path')), key: 'pathname' },
{ title: computed(() => t('tools.url-parser.params')), key: 'search' },
];
</script>
@ -28,8 +29,8 @@ const properties: { title: string; key: keyof URL }[] = [
<c-card>
<c-input-text
v-model:value="urlToParse"
label="Your url to parse:"
placeholder="Your url to parse..."
:label="t('tools.url-parser.your-url')"
:placeholder="t('tools.url-parser.placeholder.your-url')"
raw-text
:validation-rules="urlValidationRules"
/>