前言
在现在的前端开发中,很多工作都需要使用到不同的工具和库。其中,npm 是一个非常重要的工具,可以方便地管理和使用各种开源的前端库。而 @profimedica/ajuro-tools 就是一个非常好用的 npm 包,它提供了一些常见的工具函数,可以帮助我们更方便地开发。
安装
安装最新版本:
npm install @profimedica/ajuro-tools
工具函数
getUrlParams
:获取 URL 中的参数对象setUrlParams
:设置 URL 中的参数removeUrlParams
:移除 URL 中的参数debounce
:防抖函数,用于优化函数性能throttle
:节流函数,用于优化函数性能deepClone
:深度克隆一个对象cookie
:操作 cookie 的工具函数storage
:操作 localStorage/sessionStorage 的工具函数getDeviceType
:获取设备类型getQueryString
:获取 queryStringisEmptyObject
:判断一个对象是否为空对象isEmail
:判断一个字符串是否是邮箱格式isPhoneNum
:判断一个字符串是否是手机号码格式isUrl
:判断一个字符串是否是 URL 地址格式timeStampToDate
:将时间戳转换为日期字符串dateToTimeStamp
:将日期字符串转换为时间戳
使用示例
getUrlParams
import { getUrlParams } from "@profimedica/ajuro-tools"; console.log(getUrlParams()); //{} console.log(getUrlParams("https://www.baidu.com?key1=value1&key2=value2")); //{ key1: "value1", key2: "value2" }
setUrlParams
import { setUrlParams } from "@profimedica/ajuro-tools"; let url = "https://www.baidu.com?key1=value1"; let newObj = { key2: "value2", key3: "value3" }; console.log(setUrlParams(url, newObj)); //https://www.baidu.com?key1=value1&key2=value2&key3=value3
removeUrlParams
import { removeUrlParams } from "@profimedica/ajuro-tools"; let url = "https://www.baidu.com?key1=value1&key2=value2&key3=value3"; let arr = ["key2", "key3"]; console.log(removeUrlParams(url, arr)); //https://www.baidu.com?key1=value1
debounce
import { debounce } from "@profimedica/ajuro-tools"; function test() { console.log("防抖成功"); } const deObj = debounce(test, 300); deObj();
throttle
import { throttle } from "@profimedica/ajuro-tools"; function test() { console.log("节流成功"); } const throttObj = throttle(test, 300); throttObj();
deepClone
import { deepClone } from "@profimedica/ajuro-tools"; let obj = { name: "yun", age: 20 }; let obj2 = deepClone(obj); console.log(obj === obj2);//false
cookie
import { cookie } from "@profimedica/ajuro-tools"; cookie.set("name", "yun", 7); cookie.get("name");//yun cookie.del("name");
storage
import { storage } from "@profimedica/ajuro-tools"; storage.set("name", "yun"); storage.get("name");//yun storage.del("name");
getDeviceType
import { getDeviceType } from "@profimedica/ajuro-tools"; console.log(getDeviceType()); // PC
getQueryString
import { getQueryString } from "@profimedica/ajuro-tools"; let str = "?name=yun&age=20"; console.log(getQueryString(str, "name"));//yun
isEmptyObject
import { isEmptyObject } from "@profimedica/ajuro-tools"; console.log(isEmptyObject({}));//true console.log(isEmptyObject({ name: "yun" }));//false
isEmail
import { isEmail } from "@profimedica/ajuro-tools"; console.log(isEmail("123@abc.com"));//true console.log(isEmail("123@@abc.com"));//false
isPhoneNum
import { isPhoneNum } from "@profimedica/ajuro-tools"; console.log(isPhoneNum("18012341234"));//true console.log(isPhoneNum("18012abc789"));//false
isUrl
import { isUrl } from "@profimedica/ajuro-tools"; console.log(isUrl("https://www.baidu.com"));//true console.log(isUrl("www.baidu.com"));//false
timeStampToDate
import { timeStampToDate } from "@profimedica/ajuro-tools"; console.log(timeStampToDate(1618740670294));//2021-04-18 09:44:30
dateToTimeStamp
import { dateToTimeStamp } from "@profimedica/ajuro-tools"; console.log(dateToTimeStamp("2021-04-18 09:44:30"));//1618740670000
结语
以上就是 @profimedica/ajuro-tools 这个 npm 包的一些常用方法,它们都是非常实用的工具函数,通过学习这些方法,相信能够帮助大家更好地开发。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/6005667f81e8991b448e2901