1.0.32 • Published 1 year ago

@haluo/util v1.0.32

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

@haluo/util

哈罗摩托工具库

部署说明

npm publish // 发布包
npm unpublish --force // 强制删除
npm unpublish @haluo/util@0.0.0 // 删除指定版本的包

安装说明

npm install -D @haluo/util
yarn add -D @haluo/util
// npm install -D @types/@haluo/util # 使用ts构建时

使用说明

// 方式一
import { cookie, date, dom, match, number, tools } from '@haluo/util' // 全量引用

// 方式二
import tools from '@haluo/util/dist/modules/match'  // 局部引用

// 方式三
const date = require('@haluo/util').date  // 全量引用

// 方式四
import haluoUtil from '@haluo/util'  // 全量引用

// 方式五
import util from '@haluo/util'
util.install(Vue)
this.$cookie、this.$date、this.$dom、this.$match、this.$number、this.$tools

🚀 API 文档

date

import { date } from '@haluo/util';

  • format:格式化时间戳
date.format(new Date())  // 默认格式 'YYYY-MM-DD HH:mm:ss'
date.format(1586840260500)  // 默认格式,传参为linux时间
date.format(new Date(), 'YYYY:MM:DD')  // 自定义格式 'YYYY:MM:DD'
  • addDaysToDate 天数加减
addDaysToDate('2020-10-15', 10) // '2020-10-25'
addDaysToDate('2020-10-15', -10) // '2020-10-05'
  • remainTime 获取倒计时剩余时间
date.remainTime(new Date())  // 入参为 endTime,出参为:{dd: '天', hh: '时', mm: '分', ss: '秒'}
date.remainTime(new Date(), 1586840260500)  // 入参为 endTime、startTime,出参为:{dd: '天', hh: '时', mm: '分', ss: '秒'}
date.remainTime(1586840260500)  //  {dd: '天', hh: '时', mm: '分', ss: '秒'}
  • formatPassTime 格式化现在的已过时间
date.formatPassTime(new Date()) // *年前 *个月前 *天前 *小时前 *分钟前 刚刚
  • formatPassTimeForDetail 格式化时间 详情内容里的时间格式
formatPassTimeForDetail(1494141000*1000, 'YYYY-MM-DD', false) // *年*月*日 *月*日 刚刚(1-60秒) 1-60分钟前 1-24小时前 1-3天前

dom(仅限H5使用)

import { dom } from '@haluo/util';

  • createElement 创建一个子元素,并添加至父节点(可选)(仅限H5使用)
dom.createElement('div', '', 'body')
  • wrapperA 给url包一个a标签(仅限H5使用)
// "1234<a href=\"https://m.jddmoto.com/home-garage\">https://m.jddmoto.com/home-garage</a>你好"
dom.wrapperA('1234https://m.jddmoto.com/home-garage你好')
  • 对象转化为formdata(仅限H5使用)
getFormData({a: 1, b: 2}) // 返回 FormData

format

import { format } from '@haluo/util';

  • transformObjectNullVal 对于对象非数字与布尔值的value,当其为falsy时,转换成separator
format.transformObjectNullVal({ a: null, b: 0}, '23') // {a: "23", b: 0}

match

import { match } from '@haluo/util';

  • checkType 检测是否 ip、port、phone(手机号码)、email(邮箱)、IDCard(身份证)、url(网址)、number(数字)
match.checkType('10.120.33.11', 'ip') // true
match.checkType('13111111111', 'phone') // true
match.checkType('wanghui@jddmoto.com', 'email') // true

number

import { number } from '@haluo/util';

  • formatNumber 个位数前面补0
number.formatNumber(1) // 01
  • formatPhone 将手机号中间部分替换为星号
number.formatPhone(13111111111) // 131****1111
  • convertToWan 格式化数字 万
number.convertToWan(123000) // 12.3万
  • convertToThousand 格式化数字 k
number.convertToThousand(1200) // 1.2k
  • random 随机数,指定范围
number.random(1, 100) // 4
  • formatMoney 格式化金额
number.formatMoney(123456) // 123,456

tools

常用函数工具库(防抖、节流、正则类型检测、深浅拷贝等) import { tools } from '@haluo/util';

  • deepCopy 深拷贝,支持 普通对象、数组,但是未解决Function、Date、RegExp,且1M以上数据性能不好
tools.deepCopy({a: {b: 2}, c: {d: 3}}) // {a: {b: 2}, c: {d: 3}}
  • deepCopy2 支持 普通对象、数组和函数的深复制,但是未解决循环引用、Date、RegExp
tools.deepCopy2({a: {b: 2}, c: {d: 3}}) // {a: {b: 2}, c: {d: 3}}
  • debounce 防抖
tools.debounce(func, 300)(args)
  • throttle 节流
tools.throttle(func, 300)(args)
  • loadJs 动态加载脚本(仅限H5使用)
tools.loadJs('https://cdn.jsdelivr.net/npm/vant@2.10.1/lib/index.js').then()
  • loadCss 动态加载样式(仅限H5使用)
tools.loadCss('https://cdn.jsdelivr.net/npm/vant@2.10.1/lib/index.css').then()
  • startScroll 蒙层隐藏后,html开启滚动操作(仅限H5使用)
tools.startScroll()
  • stopScroll 蒙层显示后,html禁止滚动操作(仅限H5使用)
tools.stopScroll()
  • clipboard 字符串复制到剪贴板(仅限H5使用)
tools.clipboard('test')
  • firstUpperCase 首字母大写
tools.firstUpperCase('abcde') // Abcde
  • guid 生成guid
tools.guid() // a5a35c73-f516-9f9d-ec66-78163d092cd2
  • getBytesOfText 获取文本字节数(含中文)
tools.getBytesOfText('张三a1') // 6
  • objectToArray object 对象转 array 数组
tools.objectToArray({a: 1, b: 2})  // ["a=1", "b=2"]
  • convertKeyValueEnum 枚举键值互换
tools.convertKeyValueEnum({a: 1, b: 2}) // {1: "a", 2: "b"}
  • uniqueArr 数组去重
tools.uniqueArr[1, 2, 2, 3])  // [1, 2, 3]
  • swapArray 数组元素交换位置
tools.swapArray([1, 2, 3, 4], 2, 3)  // [1, 2, 4, 3]
  • filterEmoji 过滤表情符号
tools.filterEmoji() // string
  • containsEmoji 是否包含表情
tools.containsEmoji() // true or false
  • containsHanZi 是否包含表汉字
tools.containsHanZi('123哈哈456') // true
  • isEmpty 任意类型是否为空
tools.isEmpty() // true or false
  • isDefined 变量是否 undefined 或 null
tools.isDefined() // true or false
  • sensitiveField 字段脱敏处理
tools.sensitiveField(13111111111, 3, 4) // 131****1111

sentry

import { sentry } from '@haluo/util';

  • 初始化
 import { sentry } from '@haluo/util';
 const option = {
  dsn: http://753ce3bf82e94ab0aa7b5e62fae16d3c@sentry.***.com:9000/2
 }
 const Sentry = sentry.getInstance(Vue, option);
 Vue.prototype.$sentry = sentry;
  • 主动上报
 this.$sentry.log('test');
2.0.3

1 year ago

2.0.2

1 year ago

2.0.5

1 year ago

2.0.4

1 year ago

2.0.6

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.32

1 year ago

1.0.31

1 year ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.30

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.9

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

0.0.12

2 years ago

0.0.10

2 years ago

0.0.11

2 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago

0.0.0

3 years ago