npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@haluo/util

v2.1.5

Published

摩托范工具库

Downloads

562

Readme

@haluo/util

摩托范工具库

部署说明

npm publish // 发布包
npm unpublish --force // 强制删除
npm unpublish @haluo/[email protected] // 删除指定版本的包

安装说明

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('[email protected]', '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
  • formatPhoneNumber 格式化手机号码
number.formatPhoneNumber(13111111111) // 131 1111 1111

openApp

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

  • export default new openApp(env, postJddTrack, trackId, openAppState)

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/[email protected]/lib/index.js').then()
  • loadCss 动态加载样式(仅限H5使用)
tools.loadCss('https://cdn.jsdelivr.net/npm/[email protected]/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');