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

tin-custom-utils

v1.3.1

Published

``` npm install tin-custom-utils --save // 安装工具包 impore { dayOfYear} from "tin-custom-utils" // 引入工具包 const dayNum = dayOfYear('2023-01-01') // dayNum = 1 ```

Downloads

12

Readme

自定义工具库

安装使用说明

npm install tin-custom-utils --save  // 安装工具包
impore { dayOfYear} from "tin-custom-utils" // 引入工具包
const dayNum = dayOfYear('2023-01-01') // dayNum = 1

date 类方法

  1. dayOfYear(date) —— 获取输入日期时间年第几天,不传默认当天 npm deprecate

  2. formatDateTime(date,type) —— 返回固定格式的日期,如果不传默认当前日期 2023-09-29 15:00:00

    date: '日期' type 格式有:y-m-d h:m:s | y/m/d h:m:s | y-m-d h:m | y.m.d h:m | m.d | d | h:m formatDateTime('2023-09-09 12:00:00','y/m/d h:m:s') => '2023/09/09 12:00:00'

  3. formatNumber(n: number | string) —— 格式化数字,2 位是否补 0

  4. getStayTimeStr(startTime?: number | string, endTime?: number | string): Object —— 计算两个时间差(天、时、分、秒)

  5. getPassedTime(startTime: number): string —— 格式化时间: 一分钟内>刚刚; 一小时内>xx 分钟前; 1-24 小时内>xx 小时前; 昨天发起>昨天 时:分; 前天发起>前天 时:分; 超过前天>年-月-日 时:分

number 类方法

  1. approximatelyEqual(num1: number, num2: number, epsilon?: number): boolean —— 判断两个数字是否近似相等

    num1: 数值 1 num2: 数值 2 epsilon: 精度 approximatelyEqual( 3.1415, 3.14, 0.01 ) // true > approximatelyEqual(3.15, 3.14, 0.01 ) // false > approximatelyEqual( 3.14, 3.16, 0.01 ) // false

  2. RandomNum(min: number, max: number): number —— 生成一个(min,max)范围内的随机数
  3. average(...nums: number[]): number —— 计算两个或多个数的平均数
  4. distance(pointA: TPoint, pointB: TPoint): number —— 计算两点之间的距离

    pointA = (x: 3,y:4)pointB = (x:0,y:0) 例: distance(pointA,pointB) // 5

  5. thousandNum(num: string) —— 格式化金钱"20,190,214"
  6. randomId = (n = 8): string —— 生成 n 位随机字符串
  7. randomColor = (): string —— 生成随机 HEX 色值(#FFFFFF)
  8. startScore = (rate: number): string —— 生成星级评分‘★★★☆☆’
  9. isValidJSON = (str: string): boolean —— 判断给定的字符串是否是 JSON 字符串
  10. getBytes(str: string): number —— 求一个字符串的字节长度,假设:一个英文字符占用一个字节,一个中文字符占用两个字节
  11. numToCapital(num: number | string | boolean, type: boolean = '') —— 阿拉伯数字转大写, type 为真是钱数大写

    num: 要转换的数字 type: 真为大写钱数,假为大写数字

Array 类方法

  1. allEqual(array: Array): boolean —— 判断数组中的元素是否都相等
  2. compact(arr: unknown[]) —— 移除数组中值为 false 的元素
  3. getValueByProp( array: any[], data: string | number | boolean, prop: string, value: string): T | false —— 根据属性名获取数组中相对应的值

    array:要获取数据的数组 data: 需要对比的数据 prop: 对比的属性名 value: 想要获取的属性名 getValueByProp([{label:'属性1',value:1},{label:'属性2‘,value:2}],2,'value','label') => '属性2' > getValueByProp([{label:'属性1',value:1},{label:'属性2‘,value:2}],'属性1,'label' ) => {label:'属性1', value: 1}

  4. deepClone(obj: any): any —— 深拷贝数据
  5. filteRepeat(arr: T[]) —— 数组去重

regexp 校验类方法

  1. getNameRegexp = (min = 1, max = 20) —— 获取名称通用正则表达式,仅支持汉字、数字、大小写字母、下划线
  2. checkPlateNumberFormat(plateNo: string) —— 校验车牌号

其他数据处理方法

  1. encipherData(data: string, type: EncipherType): string —— 手机号|邮箱|姓名|身份证加密

    EncipherType:'phone | email | name | card' encipherData('15552877777', 'phone') => 155****7777 > encipherData('[email protected]', 'email') => *12645622*****.com > encipherData('派大星', 'name') => *大星 > encipherData('512326199102115645', 'card') => 5123****5645

  2. debounce(this: any, func: Function, wait: number, immediate: boolean = false) —— 防抖函数,immediate 为 true 时为立即执行

    this: this 指向 func:需要执行的函数 wait: 几秒后执行函数 immediate: 立即执行:触发事件后函数会立即执行,n 秒内触发事件不会执行功能函数下一次调用,n 秒后再次触发才会再次执行功能函数 非立即执行:触发事件后 n 秒后才执行函数,如果在 n 秒内又触发了事件,则会重新计算函数执行时间。 // 创建一个防抖方法 let fun = this.debounce(function () {console.log(' 触发了:>> ')}, 3000, false) // 执行防抖方法 fun()

  3. throttle(this: any, func: Function, wait: number) —— 节流函数 指连续触发事件但是在 n 秒中只执行一次函数。**节流会稀释函数的执行频率。

    this: this 指向 func:需要执行的函数 wait: 几秒后执行函数 // 创建一个节流方法 let fun = this.throttle(function () {console.log(' 触发了:>> ')}, 3000) // 执行节流方法 fun()

  4. recursion( data: Array, val: any, arrItem: string = 'key', objItem: string = 'draftdepartment', arrValItem: string = 'title', objValItem: string = 'draftdepartmentName') —— 递归遍历(根据部门 id 获取部门名称)

    data: 遍历的数组 val: 查询要遍历的对象 arrItem: 数组中对应的遍历的属性名 objItem 对象中要遍历的属性名 arrValItem 数组中获取值的属性名 objValItem 对象中获取值的属性名 const arr = [{ label:'html',value:1,children:[{ label:'div',value:3,children:[{ label:'span',value:4 },{ label:'p',value:5 }] }] },{ label:'css',value:2 }] > let form = {id: 0, label:'p'} > recursion(arr,form,'label','label','value','value') > 返回: form = {id: 0, label:'p', id:5}