@iimm/shared

v2.5.0

Published

shared utils on browser and react env

Downloads

55

Readme

@iimm/shared

NPM version NPM downloads

Install

npm install

导出内容

nanoid

const nanoid: (size?: number = 21) => string
const customNanoidAlphabet: (alpha: string, defaultSize?: number = 21) => ((size?: number = defaultSize) => string)

file

// ===== 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

const isImageMimeType = (type: string) => type.indexOf('image/') === 0;

url

const getFileNameFromUrl = (url = '') => string;
const getFileExtensionFromUrl = (url = '') => string;

isX

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