说明
-
价格开发中可能会经常用的到一些方法进行了统一的封装
-
无任何第三方依赖
-
后续会持续更新。。。
使用
yarm add project-util 或者 npm install project-util
V1.0.7 版本修改
- 新增 request 基于XMLHttpRequest封装
- 新增 requestContext 来对请求做一些全局配置
- StorageUtils 新增Cookie的相关操作
- 新增 Util
V1.0.6 版本修改
- 新增 PlatformUtils
- 新增常用的正则 RegularUtils
V1.0.5 版本修改
- 添加泛型支持
- 新增MathUtils 工具类
Util
/*** @description 生成一个树型结构的数据 注意:一级结构的 pid 必须为false* 并且记录 每一个 children 的子级数量* _count 不包含有的 children 这一项的数量* _allCount 下面的所有的子集数量* _childrenCount 拥有的一级子集数量*/getTreeDataparams: Tree: T /*** 防抖* @param fun 要执行的方法* @param time*/antiShakefun: Function, time: number = 500/*** 节流* @param fun 要执行的方法* @param time*/throttlefun: Function, time: number = 500 /*** 获取 URL 上的参数* @param params 不传 默认回去全部参数*/getURLParamsparams?: : T
request:
;
// 支持传入泛型 默认返回 Promise<Response> 类型requestrequestParams: RequestOptions: Promise<T> // 请求的参数的配置项 // 以下是 requestContext 上的一些方法 可用来做一些全局配置 // 请求失败requestFailxhr: XMLHttpRequest: void// 设置BaseURLsetBaseURLbaseURL: string: void// 设置全局公共参数setGlobalParamsparams: object: void// 设置全剧请求头setRequestHeadersheader: Header: void// 设置超时时间setTimeouttimeout: number: void// 请求超时onTimeout: any// 请求出错onError: any// 文件上传的进度onprogressparams: any: any abort: void
RegularUtils:
;
// 匹配手机号checkPhonephone: string | number: boolean // 匹配邮箱checkEmailemail: string: boolean // 匹配URLcheckUrlurl: string: boolean // 匹配身份证号码checkIdNumbernumber: number | string: boolean // 匹配邮编checkZipCodezipCode: string: boolean
PlatformUtils
;
// 获取当前的运行平台 (不支持node环境)getPlatform: Platform// 是否是微信环境isWeChat: boolean
MathUtils:
;
/*** 获取指定区间的随机数* @param min* @param max*/randommin: number, max: number: number; /** * 获取指定区间的随机整数 * @param min * @param max */randomIntmin: number, max: number: number; /** * 浮点运算 可返回精确的浮点运算结果 * @param num1 * @param num2 * @param type 运算的类型 (例如加法:FloatOperationType.ADD) */floatOperationnum1: number, num2: number, type: FloatOperationType: number;
DateUtils:
;
//获取当前或指定日期getDatetemp?: number | string, type?: string string //获取年月日getYearMonthDaytemp?: number | string, type?: string string //获取时分秒getHoursMinutesSeconds temp?: number | string string //获取两个年月日的差值getYearMonthDayDiffstart: number | string, end: number | string string //时分秒差值getHoursMinutesSecondsDiffstart: number | string, end: number | string string
ArrayUtils:
;
// 所有的数组操作不贵改变原数组 //返回两个数组的差值 返回新的数组diffarray1: T, array2: T T //去掉重复元素 返回新的数组removeRepeatarray: T T //删除指定元素 返回新的数组deleteItemarray: T, start: number, count?: number T //指定位置新增元素 返回新的数组addItemarray: T, start: number, newItem: T T/** * 排序 * @param flag: 升序 或者 降序 * @param key: 根据数组的某个字段排序 */sortarray: T, flag: boolean, key?: string T
ObjectUtils:
;
//深度拷贝一个对象, 返回新的对象deepCloneobj: T T
StorageUtils:
;
// 获取一个储存的值getStoragekey: string, isJson?: boolean T // 设置值setStorage key: string, value: any, json?: boolean void // 删除值 deleteStorage key: string boolean // 设置cookiesetCookiekey: string, value: string, expires?): void // 删除removeCookiekey: string: void// 获取getCookiekey: string: string