Collection of handy online tools for developers, with great UX.
TypeScript converterdeveloper-productivitydeveloper-toolsfrontendjavascriptproductivitytooltoolstypescriptvuejswebsite
refactor(validation): simplified validation management with helpers
Corentin Thomassetf54223fb0aae
diff --git a/src/tools/bip39-generator/bip39-generator.vue b/src/tools/bip39-generator/bip39-generator.vueindex 54e8bc2b..26556cf8 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.vueindex d48b715c..edbb4623 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.vueindex 78b732af..3b18ca14 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:[email protected]: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.tsnew file mode 100644index 00000000..20b8e33a--- /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.tsnew file mode 100644index 00000000..9e842ea8--- /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;+ }+}