mirror of
				https://github.com/CorentinTh/it-tools.git
				synced 2025-11-04 02:47:00 -05:00 
			
		
		
		
	refactor(validation): simplified validation management with helpers
This commit is contained in:
		
							parent
							
								
									b38ab82d05
								
							
						
					
					
						commit
						f54223fb0a
					
				@ -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 { isNotThrowing } from '@/utils/boolean';
 | 
				
			||||||
import { withDefaultOnError } from '@/utils/defaults';
 | 
					import { withDefaultOnError } from '@/utils/defaults';
 | 
				
			||||||
import {
 | 
					import {
 | 
				
			||||||
  chineseSimplifiedWordList,
 | 
					  chineseSimplifiedWordList,
 | 
				
			||||||
@ -98,11 +99,7 @@ const passphraseInput = ref('');
 | 
				
			|||||||
const language = ref<keyof typeof languages>('English');
 | 
					const language = ref<keyof typeof languages>('English');
 | 
				
			||||||
const passphrase = computed({
 | 
					const passphrase = computed({
 | 
				
			||||||
  get() {
 | 
					  get() {
 | 
				
			||||||
    try {
 | 
					    return withDefaultOnError(() => entropyToMnemonic(entropy.value, languages[language.value]), passphraseInput.value);
 | 
				
			||||||
      return entropyToMnemonic(entropy.value, languages[language.value]);
 | 
					 | 
				
			||||||
    } catch (_) {
 | 
					 | 
				
			||||||
      return passphraseInput.value;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  set(value: string) {
 | 
					  set(value: string) {
 | 
				
			||||||
    passphraseInput.value = value;
 | 
					    passphraseInput.value = value;
 | 
				
			||||||
@ -128,14 +125,7 @@ const mnemonicValidation = useValidation({
 | 
				
			|||||||
  source: passphrase,
 | 
					  source: passphrase,
 | 
				
			||||||
  rules: [
 | 
					  rules: [
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
      validator: (value) => {
 | 
					      validator: (value) => isNotThrowing(() => mnemonicToEntropy(value, languages[language.value])),
 | 
				
			||||||
        try {
 | 
					 | 
				
			||||||
          mnemonicToEntropy(value, languages[language.value]);
 | 
					 | 
				
			||||||
          return true;
 | 
					 | 
				
			||||||
        } catch (_) {
 | 
					 | 
				
			||||||
          return false;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      },
 | 
					 | 
				
			||||||
      message: 'Invalid mnemonic',
 | 
					      message: 'Invalid mnemonic',
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
  ],
 | 
					  ],
 | 
				
			||||||
 | 
				
			|||||||
@ -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 { isNotThrowing } from '@/utils/boolean';
 | 
				
			||||||
import { withDefaultOnError } from '@/utils/defaults';
 | 
					import { withDefaultOnError } from '@/utils/defaults';
 | 
				
			||||||
import { computed, ref } from 'vue';
 | 
					import { computed, ref } from 'vue';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -70,14 +71,7 @@ const encodedValidation = useValidation({
 | 
				
			|||||||
  source: encodeInput,
 | 
					  source: encodeInput,
 | 
				
			||||||
  rules: [
 | 
					  rules: [
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
      validator: (value) => {
 | 
					      validator: (value) => isNotThrowing(() => encodeURIComponent(value)),
 | 
				
			||||||
        try {
 | 
					 | 
				
			||||||
          encodeURIComponent(value);
 | 
					 | 
				
			||||||
          return true;
 | 
					 | 
				
			||||||
        } catch (_) {
 | 
					 | 
				
			||||||
          return false;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      },
 | 
					 | 
				
			||||||
      message: 'Impossible to parse this string',
 | 
					      message: 'Impossible to parse this string',
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
  ],
 | 
					  ],
 | 
				
			||||||
@ -92,14 +86,7 @@ const decodeValidation = useValidation({
 | 
				
			|||||||
  source: encodeInput,
 | 
					  source: encodeInput,
 | 
				
			||||||
  rules: [
 | 
					  rules: [
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
      validator: (value) => {
 | 
					      validator: (value) => isNotThrowing(() => decodeURIComponent(value)),
 | 
				
			||||||
        try {
 | 
					 | 
				
			||||||
          decodeURIComponent(value);
 | 
					 | 
				
			||||||
          return true;
 | 
					 | 
				
			||||||
        } catch (_) {
 | 
					 | 
				
			||||||
          return false;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      },
 | 
					 | 
				
			||||||
      message: 'Impossible to parse this string',
 | 
					      message: 'Impossible to parse this string',
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
  ],
 | 
					  ],
 | 
				
			||||||
 | 
				
			|||||||
@ -27,10 +27,11 @@
 | 
				
			|||||||
</template>
 | 
					</template>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script setup lang="ts">
 | 
					<script setup lang="ts">
 | 
				
			||||||
import { computed, ref } from 'vue';
 | 
					 | 
				
			||||||
import { SubdirectoryArrowRightRound } from '@vicons/material';
 | 
					 | 
				
			||||||
import { useValidation } from '@/composable/validation';
 | 
					import { useValidation } from '@/composable/validation';
 | 
				
			||||||
 | 
					import { isNotThrowing } from '@/utils/boolean';
 | 
				
			||||||
import { withDefaultOnError } from '@/utils/defaults';
 | 
					import { withDefaultOnError } from '@/utils/defaults';
 | 
				
			||||||
 | 
					import { SubdirectoryArrowRightRound } from '@vicons/material';
 | 
				
			||||||
 | 
					import { computed, ref } from 'vue';
 | 
				
			||||||
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');
 | 
				
			||||||
@ -40,14 +41,7 @@ const validation = useValidation({
 | 
				
			|||||||
  source: urlToParse,
 | 
					  source: urlToParse,
 | 
				
			||||||
  rules: [
 | 
					  rules: [
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
      validator: (value) => {
 | 
					      validator: (value) => isNotThrowing(() => new URL(value)),
 | 
				
			||||||
        try {
 | 
					 | 
				
			||||||
          new URL(value);
 | 
					 | 
				
			||||||
          return true;
 | 
					 | 
				
			||||||
        } catch (_) {
 | 
					 | 
				
			||||||
          return false;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      },
 | 
					 | 
				
			||||||
      message: 'Invalid url',
 | 
					      message: 'Invalid url',
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
  ],
 | 
					  ],
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										15
									
								
								src/utils/boolean.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								src/utils/boolean.test.ts
									
									
									
									
									
										Normal file
									
								
							@ -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);
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
							
								
								
									
										10
									
								
								src/utils/boolean.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/utils/boolean.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					export { isNotThrowing };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function isNotThrowing(cb: () => unknown): boolean {
 | 
				
			||||||
 | 
					  try {
 | 
				
			||||||
 | 
					    cb();
 | 
				
			||||||
 | 
					    return true;
 | 
				
			||||||
 | 
					  } catch (_) {
 | 
				
			||||||
 | 
					    return false;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user