aboutsummaryrefslogtreecommitdiff
path: root/src/composable
diff options
context:
space:
mode:
authorGravatar Corentin Thomasset <corentin.thomasset74@gmail.com> 2022-07-29 10:56:04 +0200
committerGravatar Corentin Thomasset <corentin.thomasset74@gmail.com> 2022-08-03 17:13:33 +0200
commit750a76b00fb79c0e9c2851c112141158ee0ffab1 (patch)
tree5a39714faedf28e215561262a1bca06fcbaff621 /src/composable
parent5f03619ab44c0b35455c46698ec37d79e87555b5 (diff)
downloadit-tools-750a76b00fb79c0e9c2851c112141158ee0ffab1.tar.gz
it-tools-750a76b00fb79c0e9c2851c112141158ee0ffab1.tar.zst
it-tools-750a76b00fb79c0e9c2851c112141158ee0ffab1.zip
refactor(base64-to-file): clean validation to convert base64 to file
Diffstat (limited to 'src/composable')
-rw-r--r--src/composable/downloadBase64.ts29
-rw-r--r--src/composable/validation.test.ts29
-rw-r--r--src/composable/validation.ts43
3 files changed, 82 insertions, 19 deletions
diff --git a/src/composable/downloadBase64.ts b/src/composable/downloadBase64.ts
index d2ea576..9348363 100644
--- a/src/composable/downloadBase64.ts
+++ b/src/composable/downloadBase64.ts
@@ -1,16 +1,35 @@
import { extension as getExtensionFromMime } from 'mime-types';
import type { Ref } from 'vue';
+function getFileExtensionFromBase64({
+ base64String,
+ defaultExtension = 'txt',
+}: {
+ base64String: string;
+ defaultExtension?: string;
+}) {
+ const hasMimeType = base64String.match(/data:(.*?);base64/i);
+
+ if (hasMimeType) {
+ return getExtensionFromMime(hasMimeType[1]) || defaultExtension;
+ }
+
+ return defaultExtension;
+}
+
export function useDownloadFileFromBase64({ source, filename }: { source: Ref<string>; filename?: string }) {
return {
download() {
- const base64 = source.value;
- const mimeType = base64.match(/data:(.*?);base64/i)?.[1] ?? 'text/plain';
- console.log({ mimeType });
- const cleanFileName = filename ?? `file.${getExtensionFromMime(mimeType)}`;
+ const base64String = source.value;
+
+ if (base64String === '') {
+ throw new Error('Base64 string is empty');
+ }
+
+ const cleanFileName = filename ?? `file.${getFileExtensionFromBase64({ base64String })}`;
const a = document.createElement('a');
- a.href = source.value;
+ a.href = base64String;
a.download = cleanFileName;
a.click();
},
diff --git a/src/composable/validation.test.ts b/src/composable/validation.test.ts
new file mode 100644
index 0000000..0bcb51f
--- /dev/null
+++ b/src/composable/validation.test.ts
@@ -0,0 +1,29 @@
+/* eslint-disable @typescript-eslint/no-empty-function */
+import { describe, expect, it } from 'vitest';
+import { isFalsyOrHasThrown } from './validation';
+
+describe('useValidation', () => {
+ describe('isFalsyOrHasThrown', () => {
+ it('should return true if the callback return nil, false or throw', () => {
+ expect(isFalsyOrHasThrown(() => false)).toBe(true);
+ expect(isFalsyOrHasThrown(() => null)).toBe(true);
+ expect(isFalsyOrHasThrown(() => undefined)).toBe(true);
+ expect(isFalsyOrHasThrown(() => {})).toBe(true);
+ expect(
+ isFalsyOrHasThrown(() => {
+ throw new Error();
+ }),
+ ).toBe(true);
+ });
+
+ it('should return true for any truthy values and empty string and 0 values', () => {
+ expect(isFalsyOrHasThrown(() => true)).toBe(false);
+ expect(isFalsyOrHasThrown(() => 'string')).toBe(false);
+ expect(isFalsyOrHasThrown(() => 1)).toBe(false);
+ expect(isFalsyOrHasThrown(() => 0)).toBe(false);
+ expect(isFalsyOrHasThrown(() => '')).toBe(false);
+ expect(isFalsyOrHasThrown(() => [])).toBe(false);
+ expect(isFalsyOrHasThrown(() => ({}))).toBe(false);
+ });
+ });
+});
diff --git a/src/composable/validation.ts b/src/composable/validation.ts
index 40df887..b27f2db 100644
--- a/src/composable/validation.ts
+++ b/src/composable/validation.ts
@@ -1,13 +1,20 @@
+import _ from 'lodash';
import { reactive, watch, type Ref } from 'vue';
-type UseValidationRule<T> = {
- validator: (value: T) => boolean;
+type ValidatorReturnType = unknown;
+
+interface UseValidationRule<T> {
+ validator: (value: T) => ValidatorReturnType;
message: string;
-};
+}
-function isFalsyOrHasThrown(cb: () => boolean) {
+export function isFalsyOrHasThrown(cb: () => ValidatorReturnType): boolean {
try {
- return !cb();
+ const returnValue = cb();
+
+ if (_.isNil(returnValue)) return true;
+
+ return returnValue === false;
} catch (_) {
return true;
}
@@ -17,22 +24,30 @@ export function useValidation<T>({ source, rules }: { source: Ref<T>; rules: Use
const state = reactive<{
message: string;
status: undefined | 'error';
+ isValid: boolean;
}>({
message: '',
status: undefined,
+ isValid: false,
});
- watch([source], () => {
- state.message = '';
- state.status = undefined;
+ watch(
+ [source],
+ () => {
+ state.message = '';
+ state.status = undefined;
- for (const rule of rules) {
- if (isFalsyOrHasThrown(() => rule.validator(source.value))) {
- state.message = rule.message;
- state.status = 'error';
+ for (const rule of rules) {
+ if (isFalsyOrHasThrown(() => rule.validator(source.value))) {
+ state.message = rule.message;
+ state.status = 'error';
+ }
}
- }
- });
+
+ state.isValid = state.status !== 'error';
+ },
+ { immediate: true },
+ );
return state;
}