mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-20 14:56:17 -04:00
refactor(dry): mutualised duplicated code with withDefaultOnError
This commit is contained in:
parent
208a373fd0
commit
f6cd9b76d3
4 changed files with 10 additions and 35 deletions
|
@ -60,6 +60,7 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { useCopy } from '@/composable/copy';
|
import { useCopy } from '@/composable/copy';
|
||||||
import { useValidation } from '@/composable/validation';
|
import { useValidation } from '@/composable/validation';
|
||||||
|
import { withDefaultOnError } from '@/utils/defaults';
|
||||||
import {
|
import {
|
||||||
chineseSimplifiedWordList,
|
chineseSimplifiedWordList,
|
||||||
chineseTraditionalWordList,
|
chineseTraditionalWordList,
|
||||||
|
@ -105,12 +106,7 @@ const passphrase = computed({
|
||||||
},
|
},
|
||||||
set(value: string) {
|
set(value: string) {
|
||||||
passphraseInput.value = value;
|
passphraseInput.value = value;
|
||||||
|
entropy.value = withDefaultOnError(() => mnemonicToEntropy(value, languages[language.value]), '');
|
||||||
try {
|
|
||||||
entropy.value = mnemonicToEntropy(value, languages[language.value]);
|
|
||||||
} catch (_) {
|
|
||||||
entropy.value = '';
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -21,18 +21,13 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import { withDefaultOnError } from '@/utils/defaults';
|
||||||
import { evaluate } from 'mathjs';
|
import { evaluate } from 'mathjs';
|
||||||
import { computed, ref } from 'vue';
|
import { computed, ref } from 'vue';
|
||||||
|
|
||||||
const expression = ref('');
|
const expression = ref('');
|
||||||
|
|
||||||
const result = computed(() => {
|
const result = computed(() => withDefaultOnError(() => evaluate(expression.value) ?? '', ''));
|
||||||
try {
|
|
||||||
return evaluate(expression.value) ?? '';
|
|
||||||
} catch (_) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="less" scoped></style>
|
<style lang="less" scoped></style>
|
||||||
|
|
|
@ -60,16 +60,11 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { useCopy } from '@/composable/copy';
|
import { useCopy } from '@/composable/copy';
|
||||||
import { useValidation } from '@/composable/validation';
|
import { useValidation } from '@/composable/validation';
|
||||||
|
import { withDefaultOnError } from '@/utils/defaults';
|
||||||
import { computed, ref } from 'vue';
|
import { computed, ref } from 'vue';
|
||||||
|
|
||||||
const encodeInput = ref('Hello world :)');
|
const encodeInput = ref('Hello world :)');
|
||||||
const encodeOutput = computed(() => {
|
const encodeOutput = computed(() => withDefaultOnError(() => encodeURIComponent(encodeInput.value), ''));
|
||||||
try {
|
|
||||||
return encodeURIComponent(encodeInput.value);
|
|
||||||
} catch (_) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const encodedValidation = useValidation({
|
const encodedValidation = useValidation({
|
||||||
source: encodeInput,
|
source: encodeInput,
|
||||||
|
@ -91,14 +86,7 @@ const encodedValidation = useValidation({
|
||||||
const { copy: copyEncoded } = useCopy({ source: encodeOutput, text: 'Encoded string copied to the clipboard' });
|
const { copy: copyEncoded } = useCopy({ source: encodeOutput, text: 'Encoded string copied to the clipboard' });
|
||||||
|
|
||||||
const decodeInput = ref('Hello%20world%20%3A)');
|
const decodeInput = ref('Hello%20world%20%3A)');
|
||||||
|
const decodeOutput = computed(() => withDefaultOnError(() => decodeURIComponent(decodeInput.value), ''));
|
||||||
const decodeOutput = computed(() => {
|
|
||||||
try {
|
|
||||||
return decodeURIComponent(decodeInput.value);
|
|
||||||
} catch (_) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const decodeValidation = useValidation({
|
const decodeValidation = useValidation({
|
||||||
source: encodeInput,
|
source: encodeInput,
|
||||||
|
|
|
@ -30,16 +30,12 @@
|
||||||
import { computed, ref } from 'vue';
|
import { computed, ref } from 'vue';
|
||||||
import { SubdirectoryArrowRightRound } from '@vicons/material';
|
import { SubdirectoryArrowRightRound } from '@vicons/material';
|
||||||
import { useValidation } from '@/composable/validation';
|
import { useValidation } from '@/composable/validation';
|
||||||
|
import { withDefaultOnError } from '@/utils/defaults';
|
||||||
import InputCopyable from '../../components/InputCopyable.vue';
|
import InputCopyable from '../../components/InputCopyable.vue';
|
||||||
|
|
||||||
const urlToParse = ref('https://me:pwd@it-tools.tech:3000/url-parser?key1=value&key2=value2#the-hash');
|
const urlToParse = ref('https://me:pwd@it-tools.tech:3000/url-parser?key1=value&key2=value2#the-hash');
|
||||||
const urlParsed = computed<URL | undefined>(() => {
|
|
||||||
try {
|
const urlParsed = computed(() => withDefaultOnError(() => new URL(urlToParse.value), undefined));
|
||||||
return new URL(urlToParse.value);
|
|
||||||
} catch (_) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const validation = useValidation({
|
const validation = useValidation({
|
||||||
source: urlToParse,
|
source: urlToParse,
|
||||||
rules: [
|
rules: [
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue