# commonUtils **Repository Path**: shadowDDD/common-utils ## Basic Information - **Project Name**: commonUtils - **Description**: 常用函数工具,可使用npm下载 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-11-22 - **Last Updated**: 2021-11-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 安装 ```shell npm install commonutils-shadow ``` # [源码地址](https://gitee.com/shadowDDD/common-utils) 使用对应函数可以进行解构后在使用 ```js import { timeToDate, navigator, functions } from "commonUtils"; const { getDateDiff } = timeToDate; const { getCookie } = navigator; const { isType, randomCoding } = functions; getDateDiff("2021-10-10 10:00", "2021-10-10 12:00:02", "minute"); getCookie("token"); //xxxxx isType({}); // 'Objext' randomCoding(6); // 195658 ``` | 函数名称 | 使用环境 | 函数描述 | | ---------- | --------------- | --------------------------------------------------------------------------------------------- | | timeToDate | Node.JS,浏览器 | 获取时间,获取当前时间,获当前周,时间相关获取函数 | | navigator | 浏览器 | 只能在浏览器运行的函数,比如 DOM,判断当前网络是否可用,sessionStorage,localStorage 等函数封装 | | functions | Node.JS,浏览器 | 常用函数,节流防抖 | # timeToDate(日期时间相关函数) ## getData(optional) 获取当前时间 返回格式 '2021-10-11 14:58:02' optional(string) 可选参数 date 返回当前年月日 tiem 返回当前时分秒 ## formatTime(_time_,_Division_) 格式字符串时间 ```javascript formatTime(20211011151222, "-"); // '2021-10-11 15:12:22' filterTime(20211011151222, "/"); //'2021/10/11 15:12:22' ``` ## secondsConversion(time) 秒转化为 小时-分钟-秒 ```js secondsConversion(5000) 01h23min20s secondsConversion(500) 08min20s secondsConversion(50) 50s ``` ## getDateDiff (str,end,type)对比二个时间返回差 str 开始时间 end 结束时间 type( second 秒 minute 分 hour 小时 day 天) 对比类型 ```js getDateDiff('2021-10-10 10:00', '2021-10-11', 'hour') 14 getDateDiff('2021-10-10 10:00', '2021-10-10 12:00:02', 'second') 7202 getDateDiff('2021-10-10 10:00', '2021-10-10 12:00', 'second') 720 getDateDiff('2021-10-10 10:00', '2021-10-10 12:00:02', 'minute') 120 ``` ## getWeekDate 获取当前周几 ``` getWeekDate() //星期三 ``` ## weekTime 获取当前天的周一到周末的年月日 ```js weekTime()[("2021/10/11", "2021/10/12", "2021/10/13", "2021/10/14", "2021/10/15", "2021/10/16", "2021/10/17")]; ``` ## monday 获取本周周一年月日 ```js monday() 2021-10-11 ``` ## sunday 获取本周日 的时间 ```js sunday() 2021-10-17 ``` ## getCurrentMonthFirst 获取本月的第一天 ```js getCurrentMonthFirst() 2021-10-01 ``` ## getCurrentMonthLast 获取本月的最后一天 ```js getCurrentMonthLast() 2021-10-31 ``` ## timeFormatsss 格式化中国标准时间 ```js timeFormatsss(new Date()) 2021-10-11 00:00:00 ``` # functions (常用函数) ## positionToCalculate(_lat1_, _lng1_, _lat2_, _lng2_) 计算二个经纬度的距离 ```js positionToCalculate(106.233366, 29.602296, 106.452697, 29.61134) 24.417公里 positionToCalculate(106.589688, 29.565987, 106.592563, 29.569765) 342米 ``` ## debounce(cb,tiem) 防抖函数 cb 回调函数 必填 tiem 防抖时间 默认 500 ```js debounce(); ``` ## throttle(cb,tiem) 节流:规定在一个单位时间内,只能触发一次函数。如果这个单位时间内触发多次函数,只有一次生效 cb 回调函数 必填 tiem 防抖时间 默认 500 ```js throttle(); ``` ## isFunc(Func,objFunc) 判断当前是不是一个函数 如果是就执行, Func | function 可选 objFunc | 参数任意,如何是函数就执行 ```js isFunc(); ``` ## MatchingProtocol(str) 匹配字符中是否存在多个 http 地址 str | String 必传 返回一个对象 true 为一个 false 为个 ```js MatchingProtocol() false ``` ## countDown(item = 60, running, finish) 倒计时 item | Number 必填 倒计时时间 running | function 必填 正在倒计时中得会执行得函数 finish |function 可选 倒计时结束执行得函数 返回倒计时时间 ID 用于 clearInterval 来停止 回调参数 stop, item stop 用于停止的 ID item 当前倒计时的秒 ```js countDown( 60, () => { stop, item; }, (stop, item) => {} ); ``` ## isType 返回当前数据的数据类型 ```js isType(1) 'Number' isType(false) 'Boolean' isType({}) 'Object' isType(new Date()) 'Date' isType(()=>{}) 'Function' isType(undefined) 'Undefined' isType(null) 'Null' ``` ## hexadecimal(color) 判断是否是 16 进制颜色 color | string 必填 ```js hexadecimal("#CCCCCC"); ``` ## randomCoding(max) 随机数返回 max |number 默认值 6 ```js randomCoding() '123DFg' ``` ## ToIdentify(ar) 伪类数组鉴别以及转换 ar |Arrar 必填 返回为真数组 ```js ToIdentify() false ``` # navigator 浏览器相关的函数 ## debugging({ Noright = true, NoKeyDown = true, debug = true}) 获取 Cookie Noright | Boolean 默认 true 禁止鼠标右键 NoKeyDown | Boolean 默认 true 禁禁止 F12 debug | Boolean 默认 true 反调试代码 ```js debugging(); ``` ## getCookie(name) 获取 Cookie _name_ | string 必填 返回对应得 key 值 ```js getCookie('refreshToken') xxxx ``` ## isPc 判断是否是 pc ```js isPc() false ``` ## iosSystem ios 系统 ```js iosSystem() true ``` ## androidSystem 安卓系统 ```js androidSystem() true ``` ## isWechatClient 安卓系统 ```js isWechatClient() true ``` ## getDeviceType(type) 获取当前设备类型 type : 默认值为 null 返回 PC 端 PCend || 移动端 mobile type 有值就返回具体的设备如'Android', 'iPhone', 'SymbianOS', ```js getDeviceType() mobile getDeviceType('type') Android ``` ## storage(params = {key = true, data = true, type = 'session'}) 存储 **params 可选** key key 是否加密 data data 是否加密 type 存储的数据类 localStorage 还是 sessionStorage 默认 sessionStorage 如 | type 类型 | 对应存储 | | --------- | -------------- | | local | localStorage | | session | sessionStorage | ```js const { storage } = navigator; //sessionStorage // key 和 data 值都不加密 storage({ key: false, data: false }).setSession("123", 123); console.log(storage({ key: false, data: false }).getSession("123")); // 数据加密存储 因为 storage 里面默认参数是加密的 storage().setSession("data", { name: "slefsummer", sex: "男🚹" }); // 获取数据 console.log(storage().getSession("data")); // 只加密 data 数据 storage({ key: false }).setSession("user", { toke: "自夏", time: "2021-10-12 10:40:58" }); console.log(storage({ key: false }).getSession("user")); // 只加密 key 数据 storage({ key: true, data: false }).setSession("userinfo", { toke: "自夏", job: ["JavaScript", "HTML", "CSS"] }); console.log(storage({ key: true, data: false }).getSession("userinfo")); //删除对应的值 storage({ key: false }).sessionClear("123"); storage({ key: false }).sessionClear("user"); // 删除所有,不需要任何参数 只需要指定type session 还是 local 默认为session storage().sessionClearAll(); ``` ## getNetwork(cb) 获取当网络 cb : 当前网络变化时出发回调 返回值 { Details: { downlink // 下载速度 effectiveType // 网络类型 rtt 连接预估往返时间 值越小网速越快 }, BeforeType // 切换前的 CurrentType // 切换后的 Available // 当前网络是否可用 } ```js const { getNetwork } = navigator; getNetwork(); // 获取当前网络 getNetwork((net) => { // 网络变化时触发回调函数 console.log(net); }); ``` ![downlinkhe和rtt返回值描述](https://static01.imgkr.com/temp/4e4f6f5cff024c46b74548ff1fec6a84.jpg) ## isExpect(value) 判断是否为非 null,undefined 和空字符以外的值 return {Boolean} ## uuid() 通过链接随机的十六进制数生成一个伪 GUID. ## is(x, y) ES5 中使用全等===会出现以下情况 +0 === -0 //true NaN === NaN // false 此方法可以弥补这个缺陷 eg: (特殊例子) ```javascript is(-0, +0); //false is(NaN, NaN); //true ``` ## isInteger(value) 判断是否为整数 return {Boolean} 下同 ## isNumber(value) 判断是否为数字 ## isNaN(value) 判断是否为 NaN ## isString(value) 判断是否为字符串 ## isDom(obj) 判断一个对象是否为 Dom 对象 ## copyObject(obj) 复制对象(浅拷贝,并且 undefined 属性不会被复制) ## clone(source) 对一个 object 进行深度拷贝,会将原型上的继承属性也拷贝 param {} source 需要进行拷贝的对象 return {} 拷贝后的新对象 ## merge(a,b,c,...rest) 合并对象,同样属性会覆盖 param {Object} a {Object} b {Object} c return {Object} 合并后的对象 ## arrayOf(a,b,c,...rest) 将一组值转换为数组(将参数转为数组) eg: ```javascript arrayOf(1, 2, 3); //[1,2,3] ``` ## includes(arr,value) 数组 arr 是否包含给定的值 value. eg: ```javascript includes([1, 2, 3], 3); //true ``` ## contains(str,value) 判断一个字符串是否被包含在另一个字符串中. eg: ```javascript contains("girl", "g"); //true ``` ## inherits(clazz, baseClazz) 构造类继承关系(clazz 继承于 baseClazz) ## extend(des, source) 源对象 source 的所有属性复制到目标对象 des(undefined 属性不会被复制) ## isArray(arr) 判断 arr 是否为数组 ## ToIdentify(arr) 伪类数组鉴别以及转换 param {Object} arr return {true/false} ## compact(arr) 去除数组中假值元素,比如 undefined,null,0,"",NaN 都是假值 param {Array} arr return {Array}