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

@zhg-x/ts-utils

v2.0.0

Published

js ts 常用方法库

Downloads

5

Readme

@zhg-x/ts-utils

npm version

常用的 JS/TS 方法工具包


安装

Using npm:

$ npm i @zhg-x/ts-utils

使用

  • 方式1:直接通过方法名称调用对应方法。
  • 方式2:通过类名.方法名称调用对应的方法。(部分方法不支持该种方式)
  • 方式3:方法链式调用(NumberChain、DateChain)。

注: 所有方法都支持通过方法名称直接调用。

示例

add(); // 1.直接通过方法名称调用
NumberUtils.add(); // 2.通过类名.方法名称调用
new NumberChain(0.1).numberAdd(0.2).numberMul(0.3); // 3.方法链式调用

具体方法

方法链式调用

V1.2.0 以下版本不支持该种方式。

DateChain

支持对日期进行链式操作

实例方法

  • getDateOfInterval(): 获取基础日期的间隔日期
  • dateFormat(): 日期格式化
const date = new Date();
const dateFmt = new DateChain(date).getDateOfInterval(10).dateFormat('YYYY-MM-dd HH:mm:ss');
console.log('dateFmt ==> ' + dateFmt);

NumberChain

支持对数值进行链式调用

实例方法

  • numberAdd():数值加法操作
  • numberSub():数值减法操作
  • numberMul():数值乘法操作
  • numberDiv():数值除法操作
  • numberFmt():十进制数值格式化
const valueFmt = new NumberChain(12345678910).numberFmt('2.4');
console.log(`金额格式化:${valueFmt}`); // 金额格式化:12,345,678,910.0000
const obj = new NumberChain(0.1).numberAdd(0.2).numberMul(0.3);
console.log(`四则运算结果= ${obj.value}`); // 四则运算结果= 0.09

仅支持通过方法名调用的方法

getVarType()

getVarType(value: any): string

获取变量类型

实现如下

Object.prototype.toString.call(value).slice(8, -1).toLowerCase()

isNull()

isNull(value: any): boolean

检查value值是否为null


isUndefined()

isUndefined(value: any): boolean

检查value值是否为undefined


isNumber()

isNumber(value: any): boolean

检查value的类型是否为number或者Number对象


isString()

isString(value: any): boolean

检查value的类型是否为string或者String对象


isBoolean()

isBoolean(value: any): boolean

检查value的类型是否为boolean或者Boolean对象


isSymbol()

isSymbol(value: any): boolean

检查value是否为symbol类型


isArray()

isArray(value: any): boolean

检查value类型是否为Array对象


isDate()

isDate(value: any): boolean

检查value值是否为 Date 类型


isMap()

isMap(value: any): boolean

检查 value 值是否为 Map 类型


isSet()

isSet(value: any): boolean

检查 value 值是否为 Set 类型


isFunction()

isFunction(value: any): boolean

检查value是否为Function


isRegExp()

isRegExp(value: any): boolean

检查 value 值是否为 RegExp 类型


isEmpty()

isEmpty(value: ValueType): boolean

判断 value 是否为空(null空字符串空对象空数组空map空Set)

示例如下:

isEmpty({})                      // true
isEmpty({id: '1'})               // false
isEmpty([])                      // true
isEmpty([1, 2, 3])               // false
isEmpty(new Set())               // true
isEmpty(new Set([]))             // true
isEmpty(new Set([10, 11, 12]))   // false

以下的方法支持两种方式调用。

ArrayUtils

数组相关方法

arrayCheck()

arrayCheck(arr: any): Array<any>

校验参数是否为数组,如果是,则原样返回,否则返回空数组。


DateUtils

日期相关方法

getDateOfInterval()

getDateOfInterval(interval: number, baseDate?: Date): Date

获取基础日期的间隔日期

间隔天数,正数则返回基础日期之后的日期,负数则返回基础日期之前的日期


getDatesFromDateRange()

getDatesFromDateRange(startDate: Date, endDate: Date): Date[]

获取时间范围内的日期,包含开始日期和结束日期。


dateFormat()

dateFormat(date: Date, fmt?: string): string

日期格式化,默认格式:YYYY-MM-dd

格式说明

  • y或者Y:年份,一般用yy表示两位年份,yyyy表示四位年份
  • M:一般用MM表示月份 (0 ~ 11)
  • d:一般用 dd 表示月份中的天数(1 ~ 31)
  • H或者h:一般用HH或者hh表示一天中的小时数 (0 ~ 23)
  • m:用mm表示分钟数 (0 ~ 59)
  • s: 用ss表示秒数 (0 ~ 59)

示例如下

const date = new Date();
date.setFullYear(2022, 1, 5);
dateFormat(date)                             // 返回 "2022-02-05"
dateFormat(date, 'YYYY-MM-dd hh:mm:ss')      // 返回 "2022-02-05 16:44:11"

NumberUtils

数值相关的工具方法

numberCheck()

numberCheck(arg: any, transFlag?: boolean): number

检查参数是否为数字或数字字符串,返回number类型的值。transFlag默认值为false

  1. 如果 transFlag 值为 true 且参数值转换为Number类型后是 NaN ,则将参数值转换为数字 0 ,否则不做转换;
  2. 判断上一步处理后的参数值是否为正数、负数、小数,如果是则将参数返回,否则抛出 TypeError

add()

add(arg1: any, arg2: any): number

两个数字相加


calcAdd()

calcAdd(args: any[]): number

多参数 加法运算


calcSub()

calcSub(arg1: any, arg2: any): number

两个数字相减


calcMul()

calcMul(arg1: any, arg2: any): number

两数求积


calcDiv()

calcDiv(arg1: any, arg2: any): number

两数求商


ObjectUtils

对象相关的工具方法

getTag()

getTag(value: any): string

获取变量类型,返回值为string类型(全部小写字母)


isObject()

isObject(value: any): boolean

检查value值是否为对象类型(Object、Array、...)


isObj()

isObj(value: any): boolean

检查value是否为object对象


cloneDeep()

cloneDeep(value: any): any

深拷贝对象自身的属性

  1. 支持深拷贝的数据类型有number、string、boolean、null、undefined、Number、String、Boolean、Map、Set、Object、Array、RegExp、Date。
  2. Int8Array、Uint8Array、Int16Array等对象数据只会进行简单的复制,不会进行深拷贝。

StringUtils

字符串相关的工具方法

convertToStr()

convertToStr(value: any): string

将变量转换成对应的字符串形式

只支持 numberstringbooleannullundefinedDateobject类型的转换。

示例

convertToStr(123);               // 返回'123'
convertToStr(new Number(456));   // 返回'456'
convertToStr('');                // 返回''
convertToStr(true);              // 返回'true'
convertToStr(null);              // 返回"null"
convertToStr(undefined);         // 返回'undefined'
convertToStr({name: 'test'});    // 返回'{"name":"test"}'
convertToStr([]);                // 返回[]

strIsEqual()

strIsEqual(arg1: any, arg2: any): boolean

比较两个参数转换成字符串后是否相等

该方法会将numbernullundefinedobject类型的参数值转换为字符串类型后再进行比较。

示例

strIsEqual(123, '123');         // true
strIsEqual(null, undefined);    // false

strIsEmpty()

strIsEmpty(value: string): boolean

判断字符串是否为 null空字符串


strIsBlank()

strIsBlank(value: string): boolean

检验字符串是否为 null、空串或者空格等


Bugs and Issues

Email:[email protected]