1.3.0 • Published 5 years ago

@dlophin/libs v1.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

@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\'children'

replaceWord(word, start ,number, replaceType)

字符串替换

使用方式如下:

let phone = 1234567890

libs.replaceWord(phone, 3, 4 , '*')  // 123****890
参数说明类型默认值
word需要替换的字符串string
start从哪个位置开始替换number0
number替换多少位number
replaceType替换的字符string*
1.3.0

5 years ago

1.2.8

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.9

5 years ago

1.1.8

6 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.1.56

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago