aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/tools/bip39-generator/bip39-generator.vue16
-rw-r--r--src/tools/url-encoder/url-encoder.vue19
-rw-r--r--src/tools/url-parser/url-parser.vue14
-rw-r--r--src/utils/boolean.test.ts15
-rw-r--r--src/utils/boolean.ts10
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;
+ }
+}