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 🙏

© 2024 – Pkg Stats / Ryan Hefner

realize-utils

v2.0.8

Published

前端函数工具库

Downloads

72

Readme

realize-utils

realize-utils

npm version license

前端业务代码工具库,支持 UMD、CJS、ESM 多模式和 Tree-Shaking。

目的:高效率完成前端业务代码

业务开发过程中,会经常用 到日期格式化url参数转对象浏览器类型判断节流函数等常用函数,为避免 不同项目多次复制粘贴的麻烦,这里统一封装,并发布到 npm,以提高开发效率。如果你也 有常用的代码,欢迎为本项目提交 pr

安装和使用 :wrench:

CDN

直接下载 lib 目录下的 realize-utils.browser.js 使用,支持 CDN 方式,直接在 html 文件中引入使用。

<script src="./js/realize-utils.js"></script>
<script>
  const newArr = utils.uniqueArray([1, 3, 5, 1, 2, 3, 5]);
  console.log(newArr); // [1, 3, 5, 2]

  const bFlag = utils.equalityArray([1, 2, 3], [1, 2, 3]);
  console.log(bFlag); // true
</script>

ESM

使用 npm 安装。支持 Tree-shaking。

npm i realize-utils -D
import { setStorage, getStorage, uniqueArray } from 'realize-utils';
setStorage('name', 'wen');
let name = getStorage('name');
console.log(name); // wen

let arr = [1, 3, 5, 1, 2, 3, 5];
let uniqueArr = uniqueArray(arr);
console.log(uniqueArr); // [1, 3, 5, 2]

CJS

直接下载 lib 目录下的 realize-utils.cjs.js 使用,在 node 环境中引入使用。

npm 链接 :link:

realize-utils

API 文档 :package:

Array

Class

Cookie

Device

Dom

  • getScrollTop 获取滚动条距顶部的距离
  • offset 获取一个元素的距离文档(document)的位置,类似 JQ 中的 offset() ele.offset()
  • scrollTo 在${duration}时间内,滚动条平滑滚动到${to}指定位置 scrollTo(to, duration)
  • setScrollTop 设置滚动条距顶部的距离
  • windowResize H5 软键盘缩回、弹起回调

Function

Keycode

Number

  • standardAmount 将数字(整数逢三一断)含小数转换成标准的金额模式,最多保留三位小数 。standardAmount(999999999.9991) => '999,999,999.999'
  • standardIntegerAmount 将 "整数" 数字(整数逢三一断)。standardIntegerAmount(99999999999) => '99,999,999,999'

Object

Random

Regexp

  • isColor 判断是否为 16 进制颜色,rgb 或 rgba
  • isEmail 判断是否为邮箱地址
  • isIdCard 判断是否为身份证号
  • isPhoneNum 判断是否为手机号
  • isUrl 判断是否为 URL 地址

Screen

Storage

String

  • digitUppercase 现金额转大写 digitUppercase(1314521) => '壹佰叁拾壹万肆仟伍佰贰拾壹元整'

Supprot

  • isSupportWebP 判断浏览器是否支持 webP 格式图片
  • downloadFile base64 数据导出文件,文件下载 downloadFile(filename, data)

Time

  • dateFormater 格式化时间 dateFormater('YYYY-MM-DD HH:mm') => '2022-08-07 14:40' dateFormater('YYYY-MM-DD', '2020.11.29') => '2020-11-29' dateFormater('YYYYMMDDHHmm', '2020-11-29 18:10:07') => '202011291810'
  • dateStrFormat 将指定字符串由一种时间格式转化为另一种。dateStrForma('20220807', 'YYYYMMDD', 'YYYY 年 MM 月 DD 日') ==> 2022 年 08 月 07 日 dateStrForma('2022 年 08 月 07 日', 'YYYY 年 MM 月 DD 日', 'YYYYMMDD') ==> 20220807
  • formatRemainTime 计算一个时间到现在过去了多久 '451 天 15 小时 17 分钟 25 秒'
  • formatPassTime 计算一个时间到现在过去了多久 '1 年前' '6 个月前' '45 分钟前'
  • isLeapYear 判断是否为闰年 isLeapYear(2020) => true isLeapYear(2021) => false isLeapYear(2022) => false
  • isSameDay 判断是否为同一天 isSameDay('2022-08-06', '2022-08-06') => true
  • monthDays(month) 获取指定日期月份的总天数 monthDays('2022-08') => 31 monthDays('2022-06') => 30
  • timeLeft(startTime, endTime) 计算${startTime - endTime}的剩余时间 timeLeft('2022-08-06 10:10:10', '2022-08-10 11:15:15') => {d: 4, h: 1, m: 5, s: 5} // 剩余 4 天 1 小时 5 分 5 秒
  • twoDateBetweenAllDay(startDay, endDay) 根据指定的两个日期,计算并返回中间的所有日期 。twoDateBetweenAllDay('2022-06-01', '2022-06-09') // ['2022-06-01', '2022-06-02', '2022-06-03', '2022-06-04', '2022-06-05', '2022-06-06', '2022-06-07', '2022-06-08', '2022-06-09']
  • twoDaysBetweenNum(startDay, endDay) 计算两个日期之间的天数。 使用场景:距今天已有 N 天 。twoDaysBetweenNum('2022-06-16', '2022-06-20') // 4

Url

  • parseQueryString url 参数转对象 parseQueryString('?a=1&b=2&c=3') => {a: '1', b: '2', c: '3'}
  • stringifyQueryString 对象序列化 stringifyQueryString({a: '1', b: '2', c: '3'}) => 'a=1&b=2&c=3'