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

@dlophin/libs

v1.3.0

Published

通用函数集合

Downloads

12

Readme

@dlophin/libs

通用函数集合

安装

# install dependencies
npm i -D @dlophin/libs

引用

import libs from '@dlophin/libs'

// 如果在typescript里 需要声明
declare namespace '@dlophin/libs'
// 在文件中引用
import * as libs from '@dlophin'

方法

getQueryString(query)

获取链接上的GET参数

使用方式如下:

// 获取链接上的id
// http://xxx.com?id=1
libs.getQuery('id')    //输入结果1

| 参数 | 说明 | 类型 | 默认值 | |:-|:-|:-|:-| | query | 需要获取的GET参数名称 | string | 无 |


paramsObj(obj)

将对象序列化

使用方式如下:

let a = { a:1, b:2 }
libs.paramsObj(a)    // a=1&b=2

| 参数 | 说明 | 类型 | 默认值 | |:-|:-|:-|:-| | obj | 需要序列化的对象 | object | 无 |

注意

  • obj 只能是一层并且不存在属性函数

date(format, timestamp)

将时间戳转换成对应格式

使用方式如下:

let timestamp = new Date()/1000

// 转换成 xxxx-xx-xx xx:xx:xx
libs.date('Y-m-d H:i:s', timestamp)
// 转换成 xxxx/xx/xx
libs.date('Y/m/d', timestamp)

| 参数 | 说明 | 类型 | 默认值 | |:-|:-|:-|:-| | format | 转换的格式 | string | 无 | | timestamp | 时间戳 | number | 无 |


dateDiff(format, timestamp1, timestamp2)

获取两个时间戳的时间差 并转换成对应格式

使用方式如下:


// 
libs.date('d天H时i分s秒', timestamp2, timestamp1)  // x天x时x分x秒

| 参数 | 说明 | 类型 | 默认值 | |:-|:-|:-|:-| | format | 转换的格式 | string | 无 | | timestamp1 | 终点时间戳 | number | 无 | | timestamp2 | 起始点时间戳 | number | 无 |


ObjectDeleteEmpty(obj)

删除对象中的空属性

使用方式如下:

let obj = { a:1,b:'', c: { d:'' } }

libs.ObjectDeleteEmpty(obj)  // { a:1, c: {} }

| 参数 | 说明 | 类型 | 默认值 | |:-|:-|:-|:-| | format | 转换的格式 | string | 无 | | timestamp1 | 终点时间戳 | number | 无 | | timestamp2 | 起始点时间戳 | number | 无 |


antiShake(time, fn)

防抖,应用场景如输入关键字后查询

使用方式如下:

// 输入关键字后 300毫秒 进行查询
let antiShake = libs.antiShake(300, function(value){
  // 进行请求
  //axios.get
})

input.addEventListener('change',function(){
  let value = this.value
  antiShake(value)
},false)

| 参数 | 说明 | 类型 | 默认值 | |:-|:-|:-|:-| | time | 多少毫秒后执行函数 | number | 无 | | fn | 反抖函数 | function | 无 |


throttle(time, fn)

节流,防止函数连续触发

使用方式如下:

// 输入关键字后 300毫秒 进行查询
let throttle = libs.throttle(300, function(value){
  console.log(1)
})

throttle()

| 参数 | 说明 | 类型 | 默认值 | |:-|:-|:-|:-| | time | 多少毫秒后执行函数 | number | 无 | | fn | 节流函数 | function | 无 |

注意

  • antiShakethrottle两个函数的区别在于 前者执行下次函数时,上次函数不会执行,而后者的每一次函数都会执行,只是延时

requireContext(file, fileType, ignore)

引入文件夹中指定的模块 搭配 require.context使用

使用方式如下:

// 文件分布
src
  app.js
  hello.js
  index.js

let modules = libs.requireContext(require.context('./src', true, /\.js$/),'js', ['index.ts']);

// modules 加载后的结果 忽略了index.js
// { app:_esModule:true, hello :_esModule:true,  }

| 参数 | 说明 | 类型 | 默认值 | |:-|:-|:-|:-| | file | 需要加载文件夹的路径 | string | 无 | | fileType | 匹配的文件后缀 | string | 无 | | ignore | 忽略的路径或文件 | array<string> | ['children'] |


replaceWord(word, start ,number, replaceType)

字符串替换

使用方式如下:

let phone = 1234567890

libs.replaceWord(phone, 3, 4 , '*')  // 123****890

| 参数 | 说明 | 类型 | 默认值 | |:-|:-|:-|:-| | word | 需要替换的字符串 | string | 无 | | start | 从哪个位置开始替换 | number | 0 | | number | 替换多少位 | number | 无 | | replaceType | 替换的字符 | string | * |