diff options
author | 2022-08-04 22:57:24 +0200 | |
---|---|---|
committer | 2022-08-04 23:14:32 +0200 | |
commit | f6cd9b76d38800e1a1f63d07152fc96cda562795 (patch) | |
tree | 2e794a084bacab1fbbf62b52b7c94e955a7e8463 | |
parent | 208a373fd08ac550778745eb6e4536bf02537da7 (diff) | |
download | it-tools-f6cd9b76d38800e1a1f63d07152fc96cda562795.tar.gz it-tools-f6cd9b76d38800e1a1f63d07152fc96cda562795.tar.zst it-tools-f6cd9b76d38800e1a1f63d07152fc96cda562795.zip |
refactor(dry): mutualised duplicated code with withDefaultOnError
Diffstat (limited to '')
-rw-r--r-- | src/tools/bip39-generator/bip39-generator.vue | 8 | ||||
-rw-r--r-- | src/tools/math-evaluator/math-evaluator.vue | 9 | ||||
-rw-r--r-- | src/tools/url-encoder/url-encoder.vue | 18 | ||||
-rw-r--r-- | src/tools/url-parser/url-parser.vue | 10 |
4 files changed, 10 insertions, 35 deletions
diff --git a/src/tools/bip39-generator/bip39-generator.vue b/src/tools/bip39-generator/bip39-generator.vue index 93f6add..54e8bc2 100644 --- a/src/tools/bip39-generator/bip39-generator.vue +++ b/src/tools/bip39-generator/bip39-generator.vue @@ -60,6 +60,7 @@ <script setup lang="ts"> import { useCopy } from '@/composable/copy'; import { useValidation } from '@/composable/validation'; +import { withDefaultOnError } from '@/utils/defaults'; import { chineseSimplifiedWordList, chineseTraditionalWordList, @@ -105,12 +106,7 @@ const passphrase = computed({ }, set(value: string) { passphraseInput.value = value; - - try { - entropy.value = mnemonicToEntropy(value, languages[language.value]); - } catch (_) { - entropy.value = ''; - } + entropy.value = withDefaultOnError(() => mnemonicToEntropy(value, languages[language.value]), ''); }, }); diff --git a/src/tools/math-evaluator/math-evaluator.vue b/src/tools/math-evaluator/math-evaluator.vue index 26fb6b1..59754de 100644 --- a/src/tools/math-evaluator/math-evaluator.vue +++ b/src/tools/math-evaluator/math-evaluator.vue @@ -21,18 +21,13 @@ </template> <script setup lang="ts"> +import { withDefaultOnError } from '@/utils/defaults'; import { evaluate } from 'mathjs'; import { computed, ref } from 'vue'; const expression = ref(''); -const result = computed(() => { - try { - return evaluate(expression.value) ?? ''; - } catch (_) { - return ''; - } -}); +const result = computed(() => withDefaultOnError(() => evaluate(expression.value) ?? '', '')); </script> <style lang="less" scoped></style> diff --git a/src/tools/url-encoder/url-encoder.vue b/src/tools/url-encoder/url-encoder.vue index 5615791..d48b715 100644 --- a/src/tools/url-encoder/url-encoder.vue +++ b/src/tools/url-encoder/url-encoder.vue @@ -60,16 +60,11 @@ <script setup lang="ts"> import { useCopy } from '@/composable/copy'; import { useValidation } from '@/composable/validation'; +import { withDefaultOnError } from '@/utils/defaults'; import { computed, ref } from 'vue'; const encodeInput = ref('Hello world :)'); -const encodeOutput = computed(() => { - try { - return encodeURIComponent(encodeInput.value); - } catch (_) { - return ''; - } -}); +const encodeOutput = computed(() => withDefaultOnError(() => encodeURIComponent(encodeInput.value), '')); const encodedValidation = useValidation({ source: encodeInput, @@ -91,14 +86,7 @@ const encodedValidation = useValidation({ const { copy: copyEncoded } = useCopy({ source: encodeOutput, text: 'Encoded string copied to the clipboard' }); const decodeInput = ref('Hello%20world%20%3A)'); - -const decodeOutput = computed(() => { - try { - return decodeURIComponent(decodeInput.value); - } catch (_) { - return ''; - } -}); +const decodeOutput = computed(() => withDefaultOnError(() => decodeURIComponent(decodeInput.value), '')); const decodeValidation = useValidation({ source: encodeInput, diff --git a/src/tools/url-parser/url-parser.vue b/src/tools/url-parser/url-parser.vue index 1978e14..78b732a 100644 --- a/src/tools/url-parser/url-parser.vue +++ b/src/tools/url-parser/url-parser.vue @@ -30,16 +30,12 @@ import { computed, ref } from 'vue'; import { SubdirectoryArrowRightRound } from '@vicons/material'; import { useValidation } from '@/composable/validation'; +import { withDefaultOnError } from '@/utils/defaults'; 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 urlParsed = computed<URL | undefined>(() => { - try { - return new URL(urlToParse.value); - } catch (_) { - return undefined; - } -}); + +const urlParsed = computed(() => withDefaultOnError(() => new URL(urlToParse.value), undefined)); const validation = useValidation({ source: urlToParse, rules: [ |