From 06cf48c4c5165f2f008f4410445f8057261ba31f Mon Sep 17 00:00:00 2001 From: neverland Date: Tue, 9 Jul 2019 16:41:20 +0800 Subject: [PATCH] [improvement] Uploader: utils ts (#3793) --- src/uploader/index.js | 6 +----- src/uploader/{utils.js => utils.ts} | 12 ++++++++---- 2 files changed, 9 insertions(+), 9 deletions(-) rename src/uploader/{utils.js => utils.ts} (53%) diff --git a/src/uploader/index.js b/src/uploader/index.js index 1869acc41..fa7b4cb58 100644 --- a/src/uploader/index.js +++ b/src/uploader/index.js @@ -1,15 +1,11 @@ import { createNamespace, addUnit } from '../utils'; -import { toArray, readFile, isOversize } from './utils'; +import { toArray, readFile, isOversize, isImageDataUrl } from './utils'; import Icon from '../icon'; import Image from '../image'; import ImagePreview from '../image-preview'; const [createComponent, bem] = createNamespace('uploader'); -function isImageDataUrl(dataUrl) { - return dataUrl.indexOf('data:image') === 0; -} - export default createComponent({ inheritAttrs: false, diff --git a/src/uploader/utils.js b/src/uploader/utils.ts similarity index 53% rename from src/uploader/utils.js rename to src/uploader/utils.ts index 532d54203..be085ebf4 100644 --- a/src/uploader/utils.js +++ b/src/uploader/utils.ts @@ -1,4 +1,4 @@ -export function toArray(item) { +export function toArray(item: T | T[]): T[] { if (Array.isArray(item)) { return item; } @@ -6,12 +6,12 @@ export function toArray(item) { return [item]; } -export function readFile(file, resultType) { +export function readFile(file: File, resultType: string) { return new Promise(resolve => { const reader = new FileReader(); reader.onload = event => { - resolve(event.target.result); + resolve((event.target as FileReader).result); }; if (resultType === 'dataUrl') { @@ -22,6 +22,10 @@ export function readFile(file, resultType) { }); } -export function isOversize(files, maxSize) { +export function isOversize(files: File | File[], maxSize: number): boolean { return toArray(files).some(file => file.size > maxSize); } + +export function isImageDataUrl(dataUrl: string): boolean { + return dataUrl.indexOf('data:image') === 0; +}