UNPKG

@iimm/shared

Version:

shared utils on browser and react env

85 lines (61 loc) 2.52 kB
# @iimm/shared [![NPM version](https://img.shields.io/npm/v/@iimm/shared.svg?style=flat)](https://npmjs.org/package/@iimm/shared) [![NPM downloads](http://img.shields.io/npm/dm/@iimm/shared.svg?style=flat)](https://npmjs.org/package/@iimm/shared) ## Install ```bash npm install ``` ## 导出内容 ### nanoid ```typescript const nanoid: (size?: number = 21) => string const customNanoidAlphabet: (alpha: string, defaultSize?: number = 21) => ((size?: number = defaultSize) => string) ``` #### file ```typescript // ===== convert ===== const fileToBase64 = (file: File) => Promise<string>; const fileToArrayBuffer = (file: File) => Promise<ArrayBuffer>; const fileToBinaryString = (file: File) => Promise<string>; const fileToText = (file: File, encoding = 'utf-8') => Promise<string>; // ==== info ===== const getMimeTypeFromBase64 = (base64Str: string) => mimeType:string; // ==== download ==== const generateFileDownload = (file: InputFile, fileName = '', extension = '', onDownloadSuccess?: (file: InputFile, fileName: string) => void, onDownloadFail?: (file: InputFile, fileName: string) => void) => void; ``` #### image ```typescript const isImageMimeType = (type: string) => type.indexOf('image/') === 0; ``` #### url ```typescript const getFileNameFromUrl = (url = '') => string; const getFileExtensionFromUrl = (url = '') => string; ``` ### isX ```typescript const isEqual = (a?: any, b?: any) => [ 'number', 'string' ].includes(typeof (a ?? 0)) ? a === b : JSON.stringify(a) === JSON.stringify(b); const isInArray = (value: any, array: any[]) => { if (!array || !Array.isArray(array)) return false; return [ 'number', 'string' ].includes(typeof (value ?? 0)) ? array.includes(value) : array.some(item => isEqual(value, item)); }; const isObject = (value: any) => value !== null && typeof value === 'object'; const isFunction = (value: any) => typeof value === 'function'; const isString = (value: any) => typeof value === 'string'; const isNumber = (value: any) => typeof value === 'number'; const isUndefined = (value: any) => typeof value === 'undefined'; const isBrowser = !!(typeof window !== 'undefined' && window.document && window.document.createElement); /** 字符串是否是Base64,可以有data前缀,也可以无 */ const isBase64 = (str: string) => { if (!str) return false; const re = /^(data:([a-z0-9/+.-])+;base64,)?([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/; return re.test(str); }; ``` TODO ## LICENSE MIT