@netease-beauty/js-util
v0.2.2
Published
美学工具包
Downloads
9
Keywords
Readme
美学 JS-Util
提供不依赖于特定全局环境的通用开发工具包
安装
通过yarn安装:
$ yarn add @netease-beauty/js-util
通过npm安装:
$ npm install @netease-beauty/js-util
引入
import beautyUtil from '@netease-beauty/js-util'
方法
将query条件对象转换为字符串
// 返回 "test.com?a=12&b=happy"
beautyUtil.convertObjToQueryStr('test.com', {
a: 12,
b: "happy"
});
函数节流
// 只执行一次 1s后在控制台打印数字1
beautyUtil.throttle(function(){console.log(1)}, this, 1000);
beautyUtil.throttle(function(){console.log(1)}, this, 1000);
计算字符串长度,汉字占2个字符
// length = 10
beautyUtil.getStrLength('123456哈哈');
字符串格式化
// 返回 "leave a space between lines"
beautyUtil.strFormat('leave a {0} between {1}', 'space', 'lines');
截取固定长度的字符串,汉字占两个字符
// 返回 "abc我"
beautyUtil.substr('abc我觉得可以', 5);
从最后开始截取固定长度的字符串,汉字占两个字符
// 返回 "以abc"
beautyUtil.substrReverse('我觉得可以abc', 5);
合并连续空白符,替换\n为空格
// 返回 "a b c d"
beautyUtil.mergeSpace('a \n\nb\nc \nd'),
变量是否已定义
// 返回 false
beautyUtil.isDef(null);
变量是否未定义
// 返回 true
beautyUtil.isUndef(null);
生成key属性
beautyUtil.generateId();// beauty-1
beautyUtil.generateId();// beauty-2
展示数字处理
/*
* 所有显示点赞、浏览、收藏数、评论数、粉丝数的地方,显示规则遵循:
* 不到10万显示真实数字。超过10万显示X万,数字向下取整
**/
// 返回 "10万"
beautyUtil.getDisplayCount(102400);
展示数字处理
/*
* 部分由于空间不足而无法显示6位数的地方,如心得、合辑详情页底部的button上。显示规则遵循:
* 超过999,显示999+
**/
// 返回 "999+"
beautyUtil.getShortDisplayCount(1000);
判断url是否来自nos
// 返回 true
beautyUtil.fromNos('/beauty.nosdn.127.net/test.jpg');
给url加上nos参数
// 返回 '/beauty.nosdn.127.net/test.jpg?imageView&quality=95&interlace=1&thumbnail=thumbnail'
beautyUtil.convertToNosUrl('/beauty.nosdn.127.net/test.jpg','thumbnail', {
noEnlarge: true
});
视频播放时长处理,显示规则遵循:hh:mm:ss
// 返回 '10:00:00'
beautyUtil.getVideoDuration(36000);
是否vip用户(非普通用户 && 非自己人)
// 0:普通用户,1:企业,2:明星,3:自己人,4:官方账号,5:美学家
beautyUtil.isVipUser(0); // false
获取带有展示宽高信息的图片对象
// 返回
/*
{
width: '1.000rem',
height: '1.000rem',
url: '/beauty.nosdn.127.net/test.jpg?imageView&quality=95&interlace=1&thumbnail=750x0'
}
*/
beautyUtil.getFormattedImage({
width: 75,
height: 75,
url: '/beauty.nosdn.127.net/test.jpg'
}, 150);
对象深拷贝
// {a: 1, b: 2, c: [3]}
beautyUtil.cloneDeep({a: 1, b: 2, c: [3]});
#### 检查Form中输入字符长度
```js
beautyUtil.checkLength("哈哈哈", (message) => {
// message: "不能超过2个汉字"
}, 2);
处理excel表格数据
// 返回处理过的编码
beautyUtil.fixData(buffer)
模板字符串标签函数
// 返回 "xxyyzzaa"
beautyUtil.strPattern`xxyyzz${0}`("aa");
获取两个数组的交集 只适用于字符串和数字
// 返回 [2]
beautyUtil.getIntersectionArray([1, 2], [2, 3]);
数组去重
// 返回 [1, 2, 3]
beautyUtil.uniq([1, 2, 2, 3]);