1.4.0 • Published 4 years ago

@thallo/front-end-utils v1.4.0

Weekly downloads
2
License
MIT
Repository
-
Last release
4 years ago

yarn add @thallo/front-end-utils

Usage

import BaseUtils from '@thallo/front-end-utils'
// 组织树结构
BaseUtils.getTree(data: TreeSourceData[]): TreeData[]
// 根据末尾节点UUID,获取所有父节点
BaseUtils.getTreeNodes(data: TreeSourceData[],tail: string): TreeSourceData[]
// 文本占位符转换
BaseUtils.getFormatString(str: string, values: Obj): string
// 英文句子第一个单词首字母大写,其余小写
BaseUtils.firstUpperCase(str: string): string
// 格式化pid,pid === "0"时转化为undefined
BaseUtils.formatPid(value: {pid: string;[prop: string]: any;}): Obj
// 删除空数据,如空字符、空数组、空对象、null、undefind
BaseUtils.delEmptyData(value: Obj): Obj
// 去除对象中字符串的空格,[isAll] 是否去除字符串所有空格
BaseUtils.removeSpaces(value: Obj, isAll?: boolean): Obj
// 对比两个对象的差异,返回差异值
BaseUtils.getDifference(oldData: Obj, newData: Obj): Obj
// 获取随机数/随机数数组
BaseUtils.getRandom(max: number, min: number, num?: number): number | number[]
// 文件名md5加密
BaseUtils.getFileNameMd5(value:{name: string;type: string;id: string;}): string
// 计算列表总数,最大页码为999
BaseUtils.getTotal(value: {size: number;total: number;}): number
// 将json配置转化为option数组,[optional]指定转化的key
BaseUtils.getOption(data: { [prop in number | string]: string },optional?: Array<number | string>): Array<{ value: number | string; label: string }>
// 根据对象key获取值
BaseUtils.getData(data: { [prop in number | string]: string }|undefined,value: number | string): number | string
// 指定位数,向前补0
BaseUtils.fillIn(num: string, length: number): string
// 解析路由参数
BaseUtils.getParams(url: string): Obj
// 解析路由参数
BaseUtils.matchValue<T>(values: T[], value: T, defaults: T): T
// 转换为数字
BaseUtils.toNumber(value: string | number | boolean): number
// 取整
BaseUtils.toParseInt(value: string): number
// 处理网关响应实体
BaseUtils.getResponse(error: AxiosResponse, isDebug?:boolean ): AxiosResponse
// 全局ajax响应错误处理
BaseUtils.requestError(error: AxiosError, isDebug?:boolean ): { error: GatewayResponse; type: errorType }

Types

interface TreeSourceData {
  id: string;
  pid?: string;
  order?: number;
  name: string;
  [propName: string]: any;
}
interface TreeData extends TreeSourceData {
  children?: TreeData[];
}
interface Obj {
  [key: string]: any;
}
export interface GatewayResponse {
  status?: number;
  headers?: {
    "x-ca-error-code"?: string;
  };
  data?: any;
  config?: AxiosRequestConfig;
}
type errorType = "401"  | "403"  | "500"  | "retry"  | "system"  | "timeout"  | "network"  | "other";

- "401" - JWT认证失败
- "403" - 访问权限/服务权限/操作权限被禁止
- "500" - 服务器/后端连接/后端解析错误
- "retry" - 请求头参数过期/限流/后端请求超时/网关请求超时
- "system" - 请求非法/请求头计算错误/订购过期/服务器配置错误
- "timeout" - 请求超时
- "network" - 网络连接错误
- "other" - 服务器响应的其他错误
1.4.0

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.3.0

4 years ago

0.2.3

4 years ago

0.2.4

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

5 years ago