mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 21:06:42 +07:00
fixed eslint
This commit is contained in:
parent
8c448f2ab6
commit
42fbacdd66
9 changed files with 327 additions and 102 deletions
33
src/utils/uploader.ts
Normal file
33
src/utils/uploader.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
import { eventChannel, END } from 'redux-saga';
|
||||
import { VALIDATORS } from '~/utils/validators';
|
||||
|
||||
export const IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/jpg'];
|
||||
|
||||
export function createUploader<T extends {}, R extends {}>
|
||||
(callback: (args: any) => any, payload: R):
|
||||
[(args: T) => (args: T & { onProgress: (current: number, total: number) => void }) => any, EventChannel<any>] {
|
||||
let emit;
|
||||
|
||||
const chan = eventChannel(emitter => {
|
||||
emit = emitter;
|
||||
return () => null;
|
||||
});
|
||||
|
||||
const onProgress = (current: number, total: number): void => {
|
||||
emit(current >= total ? END : { ...payload, progress: parseFloat((current / total).toFixed(1)) });
|
||||
};
|
||||
|
||||
const wrappedCallback = args => callback({ ...args, onProgress });
|
||||
|
||||
return [wrappedCallback, chan];
|
||||
}
|
||||
|
||||
export const uploadGetThumb = async file => {
|
||||
if (!file.type || !VALIDATORS.IS_IMAGE_MIME(file.type)) return '';
|
||||
|
||||
return await new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onloadend = () => resolve(reader.result || '');
|
||||
reader.readAsDataURL(file);
|
||||
});
|
||||
};
|
39
src/utils/validators.ts
Normal file
39
src/utils/validators.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
import { IMAGE_MIME_TYPES } from '~/utils/uploader';
|
||||
import isValid from 'date-fns/isValid';
|
||||
import { IAddress } from '~/redux/types';
|
||||
|
||||
const isValidEmail = (email: string): boolean =>
|
||||
!!email &&
|
||||
String(email) &&
|
||||
!!String(email).match(
|
||||
/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/,
|
||||
);
|
||||
|
||||
const isLikeEmail = (email: string): boolean =>
|
||||
!!email && String(email) && !!String(email).match(/^([^\@]+)@([^\@]+)\.([^\@]+)$$/);
|
||||
|
||||
const isNonEmpty = (value: string): boolean => !!value && value.trim().length > 0;
|
||||
const isLikePhone = isNonEmpty;
|
||||
|
||||
const isAtLeast = (length: number, value: string): boolean =>
|
||||
!!value && value.trim().length >= length;
|
||||
|
||||
const isMimeOfImage = (mime): boolean => !!mime && IMAGE_MIME_TYPES.indexOf(mime) >= 0;
|
||||
|
||||
const isDate = (val: string): boolean => !!val && isValid(new Date(val));
|
||||
const isStringDate = (val: string): boolean => !!val && !!val.match(/^[\d]{2,4}\-[\d]{2}-[\d]{2}/);
|
||||
const isAddrWithRaw = ({ raw }: Partial<IAddress>): boolean => !!raw && isNonEmpty(raw);
|
||||
|
||||
export const VALIDATORS = {
|
||||
EMAIL: isValidEmail,
|
||||
LIKE_PHONE: isLikePhone,
|
||||
LIKE_EMAIL: isLikeEmail,
|
||||
NON_EMPTY: isNonEmpty,
|
||||
AT_LEAST: length => isAtLeast.bind(null, length),
|
||||
IS_IMAGE_MIME: isMimeOfImage,
|
||||
IS_DATE: isDate,
|
||||
IS_STRINGY_DATE: isStringDate,
|
||||
IS_ADDRESS_WITH_RAW: isAddrWithRaw,
|
||||
EVOLVE: (validator: (val: any) => boolean, error: string) => (val: any) =>
|
||||
!val || !validator(val) ? error : null,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue