作者: 谭辉
通过包管理器安装:
npm install @dist/utils
或者 yarn add @dist/utils
使用: import { xxx } from @dist/utils
支持 tree-shaking
支持按需引入
需要使用 babel 插件babel-plugin-import
:
// babel.config.js
module.exports = {
plugins: [
[
'import',
{
libraryName: '@dist/utils',
style: false,
camel2DashComponentName: false,
customName(name, file) {
name = name[0].toLowerCase() + name.substr(1);
return `@dist/utils/dist/es/${name}`;
}
}
]
]
};
深度查找,用法如Array.find
,支持嵌套数组的查找。采用深度优先遍历进行查找。
deepFind(source: any[], predicate: (value, index, arr) => boolean)
例:deepFind(['1',['2',['3_1']],'3_2','4'], (val) => val.startsWith('3'))
, 查找结果为: '3_1'
对象遍历,支持树状结构遍历, 采用深度遍历顺序。
traverse(target: Object, childrenkey?='children', callback: (node, parentNode) => any)
例:
const obj = {
name: parent,
children: [{ name: 'child' }]
};
traverse(obj, (node, parent) => {
console.log(node.name);
}); // parent, child
单位转换工具,目前支持长度单位(米、千米),面积单位(平方米、平方千米、公顷、亩、万亩),当输入的单位不支持,或者单位间无法互相转换,返回 null。
covertUnit(input: number, from: string, to: string)
例:convertUnit(1000, '米', '千米') === 1
保留目标位数的数字小数位 ,不进行四舍五入。
fixNumber(num: number, precesion: number)
例:fixNumber(3.14159, 4) === 3.1415
将数字转换为千分位字符串表示。
toThousands(num: number): string
例:
toThousands(12345678); // 12,345,678
对 URL 中的查询参数进行编码,如www.baidu.com?q=工具&k=c#
编码为www.baidu.com?q=%E5%B7%A5%E5%85%B7&k=c%23
encodeURL(url: string, appendParams?: {[p:string]: string|number})
appendParams: 可选,追加到 URL 中的查询参数
// www.baidu.com?q=1&k=2
encodeURL('www.baidu.com?q=1', { k: 2 });
获取 URL 中的查询参数,返回值为包含查询参数的键值对的对象。
getSearchParams(url: string)
例:
getSearchParams('www.baidu.com?q=工具&k=c#'); //{ q: '工具', k: 'c#' }
// 编码后的URL也可以解析
getSearchParams('www.baidu.com?q=%E5%B7%A5%E5%85%B7&k=c%23'); //{ q: '工具', k: 'c#' }
对localStorage
和 sessionStorage
的增强,其setItem
和getItem
方法将自动对值进行 JSON 序列化和反序列化。
Storage.local
: 即localStorage
,用法与原生一致
Storage.session
: 即sessionStorage
,用法与原生一致
Storage.setItem('user', { name: 1, age: 2 });
Storage.getItem('user'); // {name: 1, age: 2}
将 blob 类型数据下载至本地。支持 IE 浏览器
blobToFile(bolb: Blob, filename?:string)
创建 a 标签对给出的 URL 进行资源下载。支持 IE 浏览器
downloadByURL(url: string, filename: string)
判断元素是否具有 children 属性。
hasChildren(o: object, childrenKey?: string)
childrenKey
默认值为:children
判断值是否为undefined
或null
isDef(v: any)
判断值是否不为undefined
或null
isUndef(v: any)
判断process.env.NODE_ENV === 'developmen'
isDev(): boolean
判断是否是 IE 浏览器
isIE(): boolean
判断是否是微前端环境(qiankun)
isMicroApp(): boolean
判断是否是移动端环境
isMobile(): boolean
用于加载在线 js 资源,通过 script 标签的方式加载。返回一个Promise
loadScript(url: string, attributes?: object, onSuccess?: function, onError?: function): Promise
可以通过回调的方式跟踪加载状态,也可以直接通过返回的 Promise 获取加载状态。
loadScript('cdn.com/deliver.js', ()=>console.log(success), () => console.log('error'))
// 可传递attributes给script标签
loadScript('cdn.com/deliver.js', {autoLogin: true}, () => console.log('success'), () => console.log('error'))
// 使用promise特性
loadScript('cdn.com/deliver.js').then((e)=>{ //todo }, (error) => {// todo })
轮询,Polling
为 class 构造函数。
new Polling(callback: function, time: number, immediate: boolean)
start
方法开始轮询返回polling
实例, 实例具有以下方法:
判断数据类型。其内部使用Object.prototype.toString
方法
rawTypeof(v:any): string
返回的类型值为全小写字符
例:
rawTypeof([])
- arrayrawTypeof(1)
- objectrawTypeof({})
- objectrawTypeof(/\w/)
- regexp注:为了从老版本平滑升级,以下标注删除线的函数仍然包含在新版 utils 中,但不推荐继续使用,后期也不再维护。
旧版中cachedFn
并未正确实现函数缓存功能,建议使用lodash.memoize
代替
旧版中对axios
的封装过于简单,且axios
本身适用性非常高,无需一个统一的封装层,由使用者自行实现。新版不再维护,不推荐继续使用。
注:如需继续使用此导出,需要自行安装 axios,此版本中 axios 作为 peerDependencies 引入
与新版使用完全一致
与新版使用完全一致
与新版使用完全一致
使用新版Storage.local
使用新版Storage.session
针对项目具体接口数据的处理,目前主要在基础信息平台中使用,不建议继续使用
时间格式化。建议使用dayjs
进行处理
针对微前端体系中主应用与微应用获取publicPath
的方法