diff options
author | 2022-08-04 21:59:48 +0200 | |
---|---|---|
committer | 2022-08-04 21:59:48 +0200 | |
commit | f54223fb0aaedbd101b5d3dc4176053533bb936a (patch) | |
tree | 7c34fb3c3aff23a88ef03052d66d1507f33cf7f0 | |
parent | b38ab82d05147b3c7452e79c6edb07e2ced18267 (diff) | |
download | it-tools-f54223fb0aaedbd101b5d3dc4176053533bb936a.tar.gz it-tools-f54223fb0aaedbd101b5d3dc4176053533bb936a.tar.zst it-tools-f54223fb0aaedbd101b5d3dc4176053533bb936a.zip |
refactor(validation): simplified validation management with helpers
Diffstat (limited to '')
-rw-r--r-- | src/tools/bip39-generator/bip39-generator.vue | 16 | ||||
-rw-r--r-- | src/tools/url-encoder/url-encoder.vue | 19 | ||||
-rw-r--r-- | src/tools/url-parser/url-parser.vue | 14 | ||||
-rw-r--r-- | src/utils/boolean.test.ts | 15 | ||||
-rw-r--r-- | src/utils/boolean.ts | 10 |
5 files changed, 35 insertions, 39 deletions
diff --git a/src/tools/bip39-generator/bip39-generator.vue b/src/tools/bip39-generator/bip39-generator.vue index 54e8bc2..26556cf 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 { isNotThrowing } from '@/utils/boolean'; import { withDefaultOnError } from '@/utils/defaults'; import { chineseSimplifiedWordList, @@ -98,11 +99,7 @@ const passphraseInput = ref(''); const language = ref<keyof typeof languages>('English'); const passphrase = computed({ get() { - try { - return entropyToMnemonic(entropy.value, languages[language.value]); - } catch (_) { - return passphraseInput.value; - } + return withDefaultOnError(() => entropyToMnemonic(entropy.value, languages[language.value]), passphraseInput.value); }, set(value: string) { passphraseInput.value = value; @@ -128,14 +125,7 @@ const mnemonicValidation = useValidation({ source: passphrase, rules: [ { - validator: (value) => { - try { - mnemonicToEntropy(value, languages[language.value]); - return true; - } catch (_) { - return false; - } - }, + validator: (value) => isNotThrowing(() => mnemonicToEntropy(value, languages[language.value])), message: 'Invalid mnemonic', }, ], diff --git a/src/tools/url-encoder/url-encoder.vue b/src/tools/url-encoder/url-encoder.vue index d48b715..edbb462 100644 --- a/src/tools/url-encoder/url-encoder.vue +++ b/src/tools/url-encoder/url-encoder.vue @@ -60,6 +60,7 @@ <script setup lang="ts"> import { useCopy } from '@/composable/copy'; import { useValidation } from '@/composable/validation'; +import { isNotThrowing } from '@/utils/boolean'; import { withDefaultOnError } from '@/utils/defaults'; import { computed, ref } from 'vue'; @@ -70,14 +71,7 @@ const encodedValidation = useValidation({ source: encodeInput, rules: [ { - validator: (value) => { - try { - encodeURIComponent(value); - return true; - } catch (_) { - return false; - } - }, + validator: (value) => isNotThrowing(() => encodeURIComponent(value)), message: 'Impossible to parse this string', }, ], @@ -92,14 +86,7 @@ const decodeValidation = useValidation({ source: encodeInput, rules: [ { - validator: (value) => { - try { - decodeURIComponent(value); - return true; - } catch (_) { - return false; - } - }, + validator: (value) => isNotThrowing(() => decodeURIComponent(value)), message: 'Impossible to parse this string', }, ], diff --git a/src/tools/url-parser/url-parser.vue b/src/tools/url-parser/url-parser.vue index 78b732a..3b18ca1 100644 --- a/src/tools/url-parser/url-parser.vue +++ b/src/tools/url-parser/url-parser.vue @@ -27,10 +27,11 @@ </template> <script setup lang="ts"> -import { computed, ref } from 'vue'; -import { SubdirectoryArrowRightRound } from '@vicons/material'; import { useValidation } from '@/composable/validation'; +import { isNotThrowing } from '@/utils/boolean'; import { withDefaultOnError } from '@/utils/defaults'; +import { SubdirectoryArrowRightRound } from '@vicons/material'; +import { computed, ref } from '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'); @@ -40,14 +41,7 @@ const validation = useValidation({ source: urlToParse, rules: [ { - validator: (value) => { - try { - new URL(value); - return true; - } catch (_) { - return false; - } - }, + validator: (value) => isNotThrowing(() => new URL(value)), message: 'Invalid url', }, ], diff --git a/src/utils/boolean.test.ts b/src/utils/boolean.test.ts new file mode 100644 index 0000000..20b8e33 --- /dev/null +++ b/src/utils/boolean.test.ts @@ -0,0 +1,15 @@ +import { describe, expect, it } from 'vitest'; +import { isNotThrowing } from './boolean'; + +describe('boolean utils', () => { + describe('isNotThrowing', () => { + it('should return if the call throws or false otherwise', () => { + expect(isNotThrowing(() => {})).to.eql(true); + expect( + isNotThrowing(() => { + throw new Error(); + }), + ).to.eql(false); + }); + }); +}); diff --git a/src/utils/boolean.ts b/src/utils/boolean.ts new file mode 100644 index 0000000..9e842ea --- /dev/null +++ b/src/utils/boolean.ts @@ -0,0 +1,10 @@ +export { isNotThrowing }; + +function isNotThrowing(cb: () => unknown): boolean { + try { + cb(); + return true; + } catch (_) { + return false; + } +} |