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

commonutils-shadow

v1.0.3

Published

常用的js函数封装,包含BOD,DOM

Downloads

8

Readme

安装

npm install commonutils-shadow

源码地址

使用对应函数可以进行解构后在使用

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) 格式字符串时间

formatTime(20211011151222, "-"); // '2021-10-11 15:12:22'
filterTime(20211011151222, "/"); //'2021/10/11 15:12:22'

secondsConversion(time) 秒转化为 小时-分钟-秒

secondsConversion(5000)  01h23min20s
secondsConversion(500)   08min20s
secondsConversion(50)   50s

getDateDiff (str,end,type)对比二个时间返回差

str 开始时间

end 结束时间

type( second 秒 minute 分 hour 小时 day 天) 对比类型

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 获取当前天的周一到周末的年月日

weekTime()[("2021/10/11", "2021/10/12", "2021/10/13", "2021/10/14", "2021/10/15", "2021/10/16", "2021/10/17")];

monday 获取本周周一年月日

monday() 2021-10-11

sunday 获取本周日 的时间

sunday() 2021-10-17

getCurrentMonthFirst 获取本月的第一天

getCurrentMonthFirst() 2021-10-01

getCurrentMonthLast 获取本月的最后一天

getCurrentMonthLast() 2021-10-31

timeFormatsss 格式化中国标准时间

timeFormatsss(new Date())  2021-10-11 00:00:00

functions (常用函数)

positionToCalculate(lat1, lng1, lat2, lng2) 计算二个经纬度的距离

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

debounce();

throttle(cb,tiem) 节流:规定在一个单位时间内,只能触发一次函数。如果这个单位时间内触发多次函数,只有一次生效

cb 回调函数 必填 tiem 防抖时间 默认 500

throttle();

isFunc(Func,objFunc) 判断当前是不是一个函数 如果是就执行,

Func | function 可选 objFunc | 参数任意,如何是函数就执行

isFunc();

MatchingProtocol(str) 匹配字符中是否存在多个 http 地址

str | String 必传 返回一个对象 true 为一个 false 为个

MatchingProtocol()  false

countDown(item = 60, running, finish) 倒计时

item | Number 必填 倒计时时间 running | function 必填 正在倒计时中得会执行得函数 finish |function 可选 倒计时结束执行得函数

返回倒计时时间 ID 用于 clearInterval 来停止

回调参数 stop, item stop 用于停止的 ID item 当前倒计时的秒

countDown(
    60,
    () => {
        stop, item;
    },
    (stop, item) => {}
);

isType 返回当前数据的数据类型

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 必填

hexadecimal("#CCCCCC");

randomCoding(max) 随机数返回

max |number 默认值 6

randomCoding()  '123DFg'

ToIdentify(ar) 伪类数组鉴别以及转换

ar |Arrar 必填 返回为真数组

ToIdentify()  false

navigator 浏览器相关的函数

debugging({ Noright = true, NoKeyDown = true, debug = true}) 获取 Cookie

Noright | Boolean 默认 true 禁止鼠标右键 NoKeyDown | Boolean 默认 true 禁禁止 F12 debug | Boolean 默认 true 反调试代码

debugging();

getCookie(name) 获取 Cookie

name | string 必填 返回对应得 key 值

getCookie('refreshToken') xxxx

isPc 判断是否是 pc

isPc()  false

iosSystem ios 系统

iosSystem()  true

androidSystem 安卓系统

androidSystem()  true

isWechatClient 安卓系统

isWechatClient()  true

getDeviceType(type) 获取当前设备类型

type : 默认值为 null 返回 PC 端 PCend || 移动端 mobile type 有值就返回具体的设备如'Android', 'iPhone', 'SymbianOS',

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 |

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 // 当前网络是否可用 }

const { getNetwork } = navigator;
getNetwork(); // 获取当前网络
getNetwork((net) => {
    // 网络变化时触发回调函数
    console.log(net);
});

downlinkhe和rtt返回值描述

isExpect(value) 判断是否为非 null,undefined 和空字符以外的值

return {Boolean}

uuid() 通过链接随机的十六进制数生成一个伪 GUID.

is(x, y) ES5 中使用全等===会出现以下情况 +0 === -0 //true NaN === NaN // false 此方法可以弥补这个缺陷

eg: (特殊例子)

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:

arrayOf(1, 2, 3); //[1,2,3]

includes(arr,value) 数组 arr 是否包含给定的值 value.

eg:

includes([1, 2, 3], 3); //true

contains(str,value) 判断一个字符串是否被包含在另一个字符串中.

eg:

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}