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

up-util

v2.0.1

Published

the help of basic common functions

Downloads

2

Readme

README.md

在日常工作中,有很多重复使用的一些方法、功能,这些方法功能很常见,也有很多框架对此进行了封装,但是其实有很多的方法又用不着,所以这里重复造了轮子,在编译的时候最终转化成es6的语法,这样的话,我可以随时随地进行引用操作,并能够轻松的使用tree-shaking去自动剔除不必要的函数、方法。

Array

对数组的常用操作

generateArr 生成数组

使用generateArr可以轻松的生成数组。

generateArr(num: number, generator: (index?: number, arr?: T[]) => T)

  • num 需要生成的数组的长度
  • generator 每个值的生成器

这个方法通过指定数组的大小和数组的值生成逻辑来生成数组

removeFromArrayByIndex 通过下标删除数组内容

removeFromArrayByIndex(arr: T[], index: number)

  • arr 需要操作的数组
  • index 需要删除的位置

removeFromArrayByIndexes 通过下标删除数组内容

removeFromArrayByIndex(arr: T[], indexes: number[])

  • arr 需要操作的数组
  • indexes 需要删除的位置

removeFromArrayByCondition 通过条件来移除数组中内容

removeFromArrayByCondition(arr: T[], condition: (item: T, index: number) => Boolean)

  • arr 需要操作的数组
  • condition 条件函数,类似一个过滤器

removeFromArrayByValue 通过某个值来移除数组内容

removeFromArrayByValue(arr: T[], value: T)

  • arr 需要操作数组
  • value 需要删掉的值

removeFromArrayByValues 通过某组值来移除数组内容

removeFromArrayByValues(arr: T[], values: T[] = [])

基本同 [ removeFromArrayByValue](removeFromArrayByValue 通过某个值来移除数组内容)

Date

对时间的一些常用函数

generateDate 生成日期

generateDate(year: number , month: number , day: number )

Date函数的新建日期似乎没那么好用,这里写了个函数用来更好的生成Date

generateDate(2018,2,1); // 新建一个日期,是2018年2月1号

getAllDaysOfMonth 获取一个月的天数

getAllDaysOfMonth(year: number | Date, month?: number): number

这里有两种选择

  1. 可以直接传入一个Date类型参数
getAllDaysOfMonth(new Date()); // 获取当前月有多少天
  1. 也可以直接传入yearmonth
getAllDaysOfMonth(2018,2); // 获取2018年2月有几天 返回结果: 28

getBeginWeekOfDate 获取某月的开始星期

getBeginWeekOfDate(year: number | Date, month?: number)

这里有两种选择

  1. 可以直接传入一个Date类型参数
getBeginWeekOfDate(new Date()); // 获取当前月第一天是周几
  1. 也可以直接传入yearmonth


getBeginWeekOfDate(2018,2); // 获取2018年2月第一天是周几  返回结果: 4

getEndWeekOfDate 获取某月的结束星期

getEndWeekOfDate(year: number | Date, month?: number)

这里的使用同[getBeginWeekOfDate ](getBeginWeekOfDate 获取某月的开始星期)

getFormatDate 获取格式化的日期

getFormatDate(date: Date = new Date(), sym: string = ''): string

获取一个日期的格式化字符串

  • date 需要格式化的日期
  • sym 连接符
getFormatDate(new Date(), ''); // 加入当前是2018年2月4号  那么返回结果是 20180204
getFormatDate(new Date(), '-'); // 加入当前是2018年2月4号  那么返回结果是 2018-02-04

getFormateDateByYearMonthDay 直接通过年月日获取格式化日期

getFormateDateByYearMonthDay(year: number, month: number, day: number, sym: string = '')

  • year 年
  • month 月
  • day 日
  • sym 连接符号
getFormateDateByYearMonthDay(2018, 3, 4); // 20180304

getFormatByMonth 获取格式化的月

getFormatByMonth(date: Date = new Date())

  • date 获取某个提起的格式化的月
getFormatByMonth(new Date('2018-02-03')); // 02

getFormatByDate 获取格式化的日

getFormatByDate(date: Date = new Date())

  • date 获取某个提起的格式化的月
getFormatByDate(new Date('2018-02-03')); // 03

getWeekDesc 获取日期的星期描述

getWeekDesc(week: number, prefix: string = '星期')

  • week 数值 0 1 2 3 4 5 6 7 分别表示从周日到周六
getWeekDesc(2); // 星期二

Object

merge 合并

merge(src: Object = {}, target: Object = {})

merge({
    a: 100,
    b: 10,
    c: {
        a: 12,
        b: 344
    }
}, {
    b: 22,
    c: {
        d: 22,
        a: 88
    }
});
/**
{
    a: 100,
    b: 22,
    c: {
        a: 88,
        b: 344,
        d: 22
	}
}*/

String

fillStr 填充字符串

fillStr(str: string, size: number = 2, option?: {

​ sym?: string, // 符号

​ left?: boolean, // 左侧还是右侧

​ slice?: boolean, // 是否截取

​ sliceFromEnd?: boolean // 是否保留右侧

}

  • str 需要填充的字符串
  • size 填充的长度 默认是2
  • option 填充选项
    • sym 填充符号 默认是空格
    • left 是否从左侧填充 默认true
    • slice 多余的是否截取 默认 true
    • sliceFromEnd 是否从右向左 默认true
 let str = 'qianzhixaing';
expect(fillStr(str, 3)).toBe('ing');
expect(fillStr(str, 3, {
    sliceFromEnd: false
})).toBe('qia');
str = 'a';
expect(fillStr(str, 3, {
    sym: '-'
})).toBe('--a');
expect(fillStr(str, 3)).toBe('  a');

str2Arr 将字符串转换成数组

str2Arr(str: string, filter: (str?: string, index?: number) => boolean = () => true)

  • str 需要转换的字符串
  • filter 过滤条件 不是所有的字符都要转换

removeDupliFromStr 去除重复的字符

removeDupliFromStr(str: string)

removeDupliFromStr('aaabbbccc'); // abc

Type

isNumber 是否是数字

isNumber(val: any)

isNumber(233); // true
isNumber(NaN); // false
isNumber('2'); // false

isString 是否是字符串

isString(val: any)

isArray 是否是数组

isArray(val: any)

isObject 是否是对象

isObject(val: any, includeArray: boolean = true)

  • val 值
  • includeArray 是否包含数组 默认包含
isObject({}); // true
isObject([]); // true
isObject([], false);// false

isBoolean是否是布尔对象

isBoolean(val: any)

isSymbol 是否是符号类型

isSymbol(val: any)