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

js-utils-leo

v1.0.2

Published

js 常用工具封装

Readme

js-utils-leo

js 常用工具封装,将这些方法分类成arraydatenumberstoragestringtypeother这些类型。

使用

使用npm安装

npm i js-utils-leo

在项目中使用

import { install, TypeFn, OtherFn } from 'js-utils-leo';
install();

const typeFn = new TypeFn();
typeFn.isFalse(0);

调用install方法,直接将ArrayFnDateFnNumberFnStorageStringFn这些实例添加到对应的原型上。不需要手动初始化。

  Array.prototype._jsUtils = new ArrayFn();
  Number.prototype._jsUtils = new NumberFn();
  String.prototype._jsUtils = new StringFn();
  Date.prototype._jsUtils = new DateFn();
  Object.prototype._jsUtils = new TypeFn();
  Storage.prototype._jsUtils = new StorageFn({
    prefix,
    isEncrypt
  });

如果想在node中使用,请勿实例化StorageFn

array

处理数组相关。

has(arr: number, val: any)

判断数组中是否存在某个元素

unique(arr: number)

去重

remove(arr: number, val: any)

删除数组中的某个元素

max(arr: array)

获取数组中的最大值

min(arr: array)

获取数组中的最大值

sum(arr: array)

数组元素求和

average(arr: array)

数组元素平均值

form(arr: Object)

将类数组转换成数组

map(arr: array, fn: Function, context: any)

传入一个函数和数组并遍历将数组中得值作为函数参数,返回所有返回值组合成得数组

union(a: array, b: array)

求两个集合的并集

intersect(a: array, b: array)

求两个集合的交集

sort(arr: array, type: number)

数组排序

type: 排序类型

  • 1: 从小到大
  • 2: 从大到小

flatten(arr: array)

数组扁平化

shuffle(arr: array)

数组内元素随机交换

randomInarray(arr: array)

数组内元素随机生成

Date

处理日期相关。

formatTime(time: number | String, cFormat: String)

格式化时间,可传入格式,例如'{y}-{m}-{d} {h}:{i}:{s}'或者{y}/{m}/{d} {h}:{i}:{s} 星期{a},注意ymdhisa这些字符不能更改。

getMonths(time: number | String, len: number, direction: number)

返回指定长度的月份集合。

如果没有传入lendirection,那么默认返回当前日期的1月份到当前日期的月份数。

len: 要返回的月份个数;

direction: 选择月份的方向(往前、往后,以当前月份为轴线往前往后各取len/2个月)

  • 1: 往前
  • 2: 往后
  • 3: 以当前月份为轴线往前往后各取len/2个月

getDays(time: number | String, len: number, direction: number)

返回指定长度的天数集合。

如果没有传入lendirection,那么默认返回当前日期。

len: 要返回的天数;

direction: 选择天数的方向(往前、往后,以当前日期份为轴线往前往后各取len/2个天)

  • 1: 往前
  • 2: 往后
  • 3: 以当前日期为轴线往前往后各取len/2个天

formatHMS(s: number)

将秒数转换成 1h1m1s 格式

getMonthOfDay(time)

获取某月有多少天

getYearOfDay(time)

获取某年有多少天

getFirstDayOfYear(time)

获取某年的第一天

getLastDayOfYear(time)

获取某年的最后一天

getDayOfYear(time)

获取某个日期是当年中的第几天

getDayOfYearWeek(time)

获取某个日期在这一年的第几周

number

处理数字类型

random(min: number, max: number)

在范围内取一个随机值

formatInToThousand(number: number, separator: String)

千位分隔符

numberToChinese(num: number)

将阿拉伯数字翻译成中文的大写数字

changeToChinese(num: number)

将数字转换为大写金额

Storage

cookielocalStoragesessionStorage相关,在初始化实例可以传入参数

例如

const storageFn = new StorageFn({
  prefix: 'test',
  isEncrypt: true
})

prefix: key前面加的前缀,默认为空; isEncrypt: 是否对value进行加密,默认为false。(只对localStoragesessionStorage进行加密,cookie不加密)

注意:通过api获取的存储数据并不是加密的,只是在浏览器的控制台上显示的是加密的

setCookie(key: any, value: any, day: number)

设置cookie

daycookie保存天数,默认为0;

getCookie(key: any)

获取cookie

removeCookie(key: any)

移除cookie

setLocal(key: any, value: any, expire: any)

设置localStorage,可以通过设置expire对数据进行过期判断.

expire: 保存天数,默认为0,不会自动过期

getLocal(key: any)

根据key获取对应的localStorage值,如果之前设置了过期时间并且已过期,返回null

hasLocal(key: any)

key是否已经在localStorage中被设置了

getLocalKeys()

获取localStorage中所有的key

getAllLocal()

获取全部localStroage

getLocalLength()

获取localStorage长度

removeLocal(key: any)

根据key移除对应的localStorage

clearLocal()

移除所有localStorage

setSession(key: any, value: any, expire: any)

设置sessionStorage,可以通过设置expire对数据进行过期判断.

expire: 保存天数,默认为0,不会自动过期

getSession(key: any)

根据key获取对应的sessionStorage值,如果之前设置了过期时间并且已过期,返回null

hasSession(key: any)

key是否已经在sessionStorage中被设置了

getSessionKeys()

获取sessionStorage中所有的key

getAllSession()

获取全部sessionStroage

getSessionLength()

获取sessionStorage长度

removeSession(key: any)

根据key移除对应的sessionStorage

clearSession()

移除所有sessionStorage

String

处理字符串

trim(str: string, type: number)

清除字符串中的空格.

type: 清除类型

  • 1: 所有空格
  • 2: 前后空格
  • 3: 前空格
  • 4: 后空格

changeCase(str: string, type: number)

改变字符大小写

type

  • 1: 首字母大写
  • 2:首页母小写
  • 3:大小写转换
  • 4:全部大写
  • 5:全部小写

randomString(length: number)

生成随机字符串

convertToCamelCase(str: string)

将短横线格式转换成驼峰式

Type

判断数据类型

isString(obj: any)

是否是字符串类型

isNumber(obj: any)

是否是数字类型

isNumberOnly(obj: any)

字符串是否只包含数字

assertNumber(obj: any)

将值转换成数字类型,如果转换后为NaN,则为0;

isBoolean(obj: any)

是否是布尔类型

isFunction(obj: any)

是否函数类型

isNull(obj: any)

是否是null类型

isUndefined(obj: any)

是否是undefined类型

isObject(obj: any)

是否是对象类型

isArray(obj: any)

是否是数组类型

isDate(obj: any)

是否是日期类型

isRegExp(obj: any)

是否是正则类型

isError(obj: any)

是否是Error类型

isSymbol(obj: any)

是否Symbol类型

isPromise(obj: any)

是否Promise类型

isString(obj: any)

是否是字符串类型

isSet(obj: any)

是否set类型

isFalse(obj: any)

是否为false

'', undefined, null'null', 'undefined', 0, false, NaN均为false

isTrue(obj: any)

是否为true!isFalse

isIOS()

设备是否为ios

isPC()

设备是否为pc

browserType()

浏览器类型

isPhone(str: string)

手机号验证

isTel(str: string)

座机号码验证

isPostal(str: string)

邮政编码验证

isQQ(str: string)

qq号码验证

isEmail(str: string)

邮箱验证

isPassword(str: string)

密码验证,密码以字母开头,长度在6~18之间,只能包含字母、数字和下划线

isUrl(str: string)

网址验证

isIP(str: string)

是否为IP

isDateFormat(str: string)

是否是日期时间 xxxx-xx-xx xx:xx:xx 或者 xxxx-xx-xx

isLowercase(str: string)

是否是小写

isUppercase(str: string)

是否是大写

isEnglish(str: string)

是否是英文

isChinese(str: string)

是否是中文

isHTML(str: string)

是否是html

isIPv6(str: string)

是否是ipv6格式

isCardID(str: string)

验证身份证号

Other

deepClone(obj: any)

深克隆

debounce(fn, delay, immediate)

防抖

delay: 多少秒之后执行 immediate: 是否立即执行

throttle(func, delay)

节流

delay: 多少秒之内执行一次

encrypt(data: any, key?: string, iv?: string)

数据加密

key: 密钥,默认使用十六位十六进制数作为密钥 iv: 密钥偏移量,默认使用十六位十六进制数作为密钥偏移量

decrypt(data: any, key?: string, iv?: string)

数据解密,解密的密钥跟密钥偏移量要跟加密时的保持一致