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

t-tools-use

v1.8.1

Published

自定义工具函数库

Downloads

8

Readme

unilts

介绍

自定义工具函数库

安装教程

npm i t-tools-use

使用说明

import untils from 't-tools-use'

方法说明

  1. call (改变函数运行时的 this 值并且得到函数执行结果)
    call(Fn, obj, ...args)

    Fn: 需要执行的函数
    obj: 函数运行时this指向的对象
    args: 函数运行时的参数


  1. apply (改变函数运行时的 this 值并且得到函数执行结果)
    apply(Fn, obj, args)

    Fn: 需要执行的函数
    obj: 函数运行时this指向的对象
    args: 函数运行时的参数


  1. bind (改变函数运行时的 this 值并且返回一个函数)
    bind(Fn, obj, ...args)

    Fn: 需要执行的函数
    obj: 函数运行时this指向的对象
    args: 函数运行时的参数


  1. throttle (函数节流)
    throttle(callback, wait)

    callback: 回调函数
    wait: 等待间隔


  1. debounce (函数防抖)
    debounce(callback, time)

    callback: 回调函数
    time: 等待间隔


  1. map (与数组的map方法一致)
    map(arr, callback)

    arr: 数组
    callback: 回调函数


  1. reduce (与数组的reduce方法一致)
    reduce(arr, callback, initValue)

    arr: 数组
    callback: 回调函数
    initValue: 初始值


  1. filter (与数组的filter方法一致)
    filter(arr, callback)

    arr: 数组
    callback: 回调函数


  1. find (与数组的find方法一致)
    find(arr, callback)

    arr: 数组
    callback: 回调函数


  1. findIndex (与数组的findIndex方法一致)
    findIndex(arr, callback)

arr: 数组
callback: 回调函数


  1. every (与数组的every方法一致)
    every(arr, callback)

arr: 数组
callback: 回调函数


  1. some (与数组的some方法一致)
    some(arr, callback)

arr: 数组
callback: 回调函数


  1. unique (数组去重)
    unique(arr)

arr: 需要去重的数组


  1. concat (与数组的concat方法一致)
    concat(arr, ...args)

arr: 数组
args: 需要合并的新数组或者值


  1. slice (与数组的slice方法一致)
    slice(arr, begin, end)

arr: 数组
begin: 开始下标
end: 结束下标


  1. flatten (数组扁平化)
    flatten(arr)

arr: 数组


  1. chunk (数组分快)
    chunk(arr, size)

arr: 数组
size: 每块的数量


  1. difference (数组差集)
    difference(arr1, arr2)

arr1: 原数组
arr2: 比较数组


  1. pull (删除数组某些元素-会改变原数组)
    pull(arr, ...args)

arr: 被删除的数组
args: 需要删除的元素


  1. pullAll (删除数组某些元素-会改变原数组)
    pullAll(arr, args)

arr: 被删除的数组
args: 需要删除的元素的数组


  1. drop (得到当前数组过滤掉左边size个后剩余元素组成的数组)
    drop(arr, size)

arr: 被删除的数组
size: 需要过滤的个数


  1. dropRight (得到当前数组过滤掉右边size个后剩余元素组成的数组)
    dropRight(arr, size)

arr: 被删除的数组
size: 需要过滤的个数


  1. newInstance (创建Fn构造函数的实例对象)
    newInstance(Fn, ...args)

Fn: 构造函数
args: 参数

   function Person(name, age) {
     this.name = name;
     this.age = age;
   }
   let obj = newInstance(Person, "张三", 18);
   结果:{
            name: '张山',
            age: 18
         }

  1. myInstanceOf (与对象方法instanceOf一致)
    myInstanceOf(obj, Fn)

obj: 某个实例对象
Fn: 某个构造函数


  1. mergeObject (合并多个对象, 返回一个合并后对象)
    mergeObject(...objs)

objs: 需要合并的对象


  1. clone (浅拷贝)
    clone(target)

target: 需要拷贝的数据


  1. deepClone (深拷贝)
    deepClone(target)

target: 需要拷贝的数据


  1. reverseString (生成一个倒序的字符串)
    reverseString(str)

str: 字符串


  1. palindrome (字符串是否为回文)
    palindrome(str)

str: 字符串


  1. truncate (截取字符串, 最后以 ... 结束)
    truncate(str, size)

str: 字符串
size: 截取长度


  1. addEventListener (事件监听(委托))
    addEventListener(el, type, Fn, selector)

el: 父级元素选择器
type: 绑定事件类型
Fn: 事件处理程序
selector: 绑定事件元素的选择器


  1. eventBus (事件总线)
   // 绑定事件
   eventBus.on("login", (data) => {
     console.log(data + "用户已经登录");
   });
   eventBus.on("login", (data) => {
     console.log(data + "用户数据已经写入");
   });

   //触发事件
   eventBus.emit('login', '张三')

   //解绑事件
   eventBus.off('login') // 删除指定事件
   eventBus.off() // 删除所有事件

  1. PubSub (消息订阅与发布)
   // 订阅消息
   let id1 = PubSub.subscribe('pay', data =>{
     console.log('商家接到订单,准备开始制作')
   })
   let id2 = PubSub.subscribe('pay', data =>{
     console.log('棋手接到了订单,准备开始去取餐')
   })
   let id3 = PubSub.subscribe('cancal', data =>{
     console.log('取消订单')
   })

   //取消消息
   PubSub.unsubscribe()
   PubSub.unsubscribe('pay')
   PubSub.unsubscribe(id1)

   //发布消息
  PubSub.publish('pay', {
     title: '预想收入'
   })

  1. axios (自定义axios, 与axios方法一致)
    axios({ method, url, params, data })
   axios({
     method: "POST",
     url: "http://192.168.10.183:8080/",
     params: { //参数
       a: 100,
       b: 200,
     },
     data: { // 请求体
       c: 50,
       d: 20,
     },
   }).then((res) => {
     console.log(res);
   });

   axios
     .get("http://192.168.10.183:8080/", {
       params: {
         a: 100,
         b: 1000,
       },
     })
     .then((res) => {
       console.log(res);
     });

  1. getFloat (保留N为小数)
    getFloat(number, n, r)

number: 需要处理的数据
n: 需要保留的位数,默认为 0 r
r: 是否四舍五入。默认为是


  1. getFractional (输入数值并保留N位小数)
    getFractional(val, maxNumber, number)

val: 传入的值
maxNumber: 最大数值限制
number: 保留小数位数,默认两位,0为整


  1. parseTime (将时间解析为字符串)
    parseTime(time, cFormat)

time: 传入的时间
cFormat: 字符串模板,如:{y}-{m}-{d} {h}:{i}:{s}


  1. formatTime (将时间戳解析为时间过去了多久)
    formatTime(time, option)

time: 传入的时间戳
option: 时间太长是字符串展示还是年月日展示


  1. param2Obj (将url中的参数结构出来)
    param2Obj(url)

url: 地址


  1. getExcelTimeConversion (excel时间格式转换)
    getExcelTimeConversion(numb, optio)

numb: excel时间
optio: 字符串模板


  1. validatenull (判断是否为空)
    validatenull(val)

val: 需要判断的数据,为空则返回true