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

lining-tools

v1.0.3

Published

提供了JS开发工具函数

Downloads

3

Readme

JS开发工具函数

在我们日常开发的时候,可能会遇到各种各样的需求,但是自己一时半会儿可能找不到合适的办法去解决。书到用时方恨少,下面的这些JavaScript的工具函数可能就会派上大用场了,我们可以进行复用,提高我们的工作效率。
现包括正则校验check工具函数、浏览器存储相关storage工具函数、浏览器操作相关browser工具函数

安装

npm install lining-tools

引入

import { isNoWord } from 'lining-tools'

正则校验check工具函数

/**
 * 验证不能包含字母
 * @param { string } value
 */
 const isNoWord = value => /^[^A-Za-z]*$/g.test(value);

/**
 * 验证中文和数字
 * @param { string } value
 */
 const isCHNAndEN = value => /^((?:[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])|(\d))+$/g.test(value);

/**
* 验证邮政编码(中国)
* @param { string } value
*/
 const isPostcode = value => /^(0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[0-5]|8[013-6])\d{4}$/g.test(value);

/**
 * 验证微信号,6至20位,以字母开头,字母,数字,减号,下划线
 * @param { string } value
 */
 const isWeChatNum = value => /^[a-zA-Z][-_a-zA-Z0-9]{5,19}$/g.test(value);

/**
* 验证16进制颜色
* @param { string } value
*/
 const isColor16 = value => /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/g.test(value);

/**
 * 验证火车车次
 * @param { string } value
 */
 const isTrainNum = value => /^[GCDZTSPKXLY1-9]\d{1,4}$/g.test(value);

/**
* 验证手机机身码(IMEI)
*  @param { string } value
*/
 const isIMEI = value => /^\d{15,17}$/g.test(value);

/**
 * 验证必须带端口号的网址(或ip)
 * @param { string } value
 */
 const isHttpAndPort = value => /^((ht|f)tps?:\/\/)?[\w-]+(\.[\w-]+)+:\d{1,5}\/?$/g.test(value);

/**
* 验证网址(支持端口和"?+参数"和"#+参数)
*  @param { string } value
*/
 const isRightWebsite = value => /^(((ht|f)tps?):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?$/g.test(value);

/**
 * 验证统一社会信用代码
 *  @param { string } value
 */
 const isCreditCode = value => /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/g.test(value);

/**
 * 验证迅雷链接
 *  @param { string } value
 */
 const isThunderLink = value => /^thunderx?:\/\/[a-zA-Z\d]+=$/g.test(value);


/**
 * 验证ed2k链接(宽松匹配)
 *  @param { string } value
 */
 const ised2k = value => /^ed2k:\/\/\|file\|.+\|\/$/g.test(value);

/**
 * 验证磁力链接(宽松匹配)
 *  @param { string } value
 */
 const isMagnet = value => /^magnet:\?xt=urn:btih:[0-9a-fA-F]{40,}.*$/g.test(value);

/**
 * 验证子网掩码
 *  @param { string } value
 */
 const isSubnetMask = value => /^(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(?:\.(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}$/g.test(value);

/**
 * 验证linux"文件夹"路径
 *  @param { string } value
 */
 const isLinuxFolderPath = value => /^(\/[^/]+)+\/?$/g.test(value);

/**
* 验证linux"文件"路径
*  @param { string } value
*/
 const isLinuxFilePath = value => /^(\/[^/]+)+$/g.test(value);

/**
 * 验证window"文件夹"路径
 *  @param { string } value
 */
 const isWindowsFolderPath = value => /^[a-zA-Z]:\\(?:\w+\\?)*$/g.test(value);

/**
* 验证window下"文件"路径
*  @param { string } value
*/
 const isWindowsFilePath = value => /^[a-zA-Z]:\\(?:\w+\\)*\w+\.\w+$/g.test(value);

/**
 * 验证股票代码(A股)
 *  @param { string } value
 */
 const isAShare = value => /^(s[hz]|S[HZ])(000[\d]{3}|002[\d]{3}|300[\d]{3}|600[\d]{3}|60[\d]{4})$/g.test(value);

/**
 * 验证版本号格式必须为X.Y.Z
 *  @param { string } value
 */
 const isVersion = value => /^\d+(?:\.\d+){2}$/g.test(value);

/**
 * 验证视频链接地址(视频格式可按需增删)
 *  @param { string } value
 */
 const isVideoUrl = value => /^https?:\/\/(.+\/)+.+(\.(swf|avi|flv|mpg|rm|mov|wav|asf|3gp|mkv|rmvb|mp4))$/i.test(value);

/**
 * 验证图片链接地址(图片格式可按需增删)
 *  @param { string } value
 */
 const isImageUrl = value => /^https?:\/\/(.+\/)+.+(\.(gif|png|jpg|jpeg|webp|svg|psd|bmp|tif))$/i.test(value);

/**
 * 验证银行卡号(10到30位, 覆盖对公/私账户, 参考微信支付)
 * @param { string } value
 */
 const isAccountNumber = value => /^[1-9]\d{9,29}$/g.test(value);

/**
 * 验证中文姓名
 * @param { string } value
 */
 const isChineseName = value => /^(?:[\u4e00-\u9fa5·]{2,16})$/g.test(value);

/**
 * 验证英文姓名
 * @param { string } value
 */
 const isEnglishName = value => /(^[a-zA-Z]{1}[a-zA-Z\s]{0,20}[a-zA-Z]{1}$)/g.test(value);

/**
 * 验证车牌号(新能源)
 * @param { string } value
 */
 const isLicensePlateNumberNER = value => /[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领 A-Z]{1}[A-HJ-NP-Z]{1}(([0-9]{5}[DF])|([DF][A-HJ-NP-Z0-9][0-9]{4}))$/g.test(value);

/**
 * 验证车牌号(非新能源)
 * @param { string } value
 */
 const isLicensePlateNumberNNER = value => /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领 A-Z]{1}[A-HJ-NP-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/g.test(value);

/**
 * 验证车牌号(新能源+非新能源)
 * @param { string } value
 */
 const isLicensePlateNumber = value => /^(?:[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领 A-Z]{1}[A-HJ-NP-Z]{1}(?:(?:[0-9]{5}[DF])|(?:[DF](?:[A-HJ-NP-Z0-9])[0-9]{4})))|(?:[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领 A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9 挂学警港澳]{1})$/g.test(value);

/**
 * 验证手机号中国(严谨), 根据工信部2019年最新公布的手机号段
 * @param { string } value
 */
 const isMPStrict = value => /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-7|9])|(?:5[0-3|5-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1|8|9]))\d{8}$/g.test(value);

/**
 * 验证手机号中国(宽松), 只要是13,14,15,16,17,18,19开头即可
 * @param { string } value
 */
 const isMPRelaxed = value => /^(?:(?:\+|00)86)?1[3-9]\d{9}$/g.test(value);

/**
* 验证email(邮箱)
* @param { string } value
*/
 const isEmail = value => /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/g.test(value);

/**
 * 验证座机电话(国内),如: 0341-86091234
 * @param { string } value
 */
 const isLandlineTelephone = value => /\d{3}-\d{8}|\d{4}-\d{7}/g.test(value);

/**
 * 验证身份证号(1代,15位数字)
 * @param { string } value
 */
 const isIDCardOld = value => /^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$/g.test(value);

/**
* 验证身份证号(2代,18位数字),最后一位是校验位,可能为数字或字符X
* @param { string } value
*/
 const isIDCardNew = value => /^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}[\dXx]$/g.test(value);

/**
 * 验证身份证号, 支持1/2代(15位/18位数字)
 * @param { string } value
 */
 const isIDCard = value => /(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/g.test(value);

/**
 * 验证护照(包含香港、澳门)
 * @param { string } value
 */
 const isPassport = value => /(^[EeKkGgDdSsPpHh]\d{8}$)|(^(([Ee][a-fA-F])|([DdSsPp][Ee])|([Kk][Jj])|([Mm][Aa])|(1[45]))\d{7}$)/g.test(value);

/**
 * 验证帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线组合
 * @param { string } value
 */
 const isWebAccount = value => /^[a-zA-Z]\w{4,15}$/g.test(value);

/**
* 验证中文/汉字
* @param { string } value
*/
 const isChineseCharacter = value => /^(?:[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])+$/g.test(value);

/**
 * 验证小数
 * @param { string } value
 */
  const isDecimal = value => /^\d+\.\d+$/g.test(value);

/**
 * 验证数字
 * @param { string } value
 */
  const isNumber = value => /^\d{1,}$/g.test(value);

/**
 * 验证qq号格式
 * @param { string } value
 */
  const isQQNum = value => /^[1-9][0-9]{4,10}$/g.test(value);

/**
 * 验证数字和字母组成
 * @param { string } value
 */
  const isNumAndStr = value => /^[A-Za-z0-9]+$/g.test(value);

/**
 * 验证英文字母
 * @param { string } value
 */
  const isEnglish = value => /^[a-zA-Z]+$/g.test(value);

/**
 * 验证大写英文字母
 * @param { string } value
 */
  const isCapital = value => /^[A-Z]+$/g.test(value);

/**
 * 验证小写英文字母
 * @param { string } value
 */
  const isLowercase = value => /^[a-z]+$/g.test(value);

浏览器操作相关browser工具函数

/**
 * 返回当前url
 */
 const currentURL = () => window.location.href;

/**
 * 获取url参数(第一种)
 * @param {*} name
 * @param {*} origin
 */
  function getUrlParam(name, origin = null) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    let r = null;
    if (origin == null) {
        r = window.location.search.substr(1).match(reg);
    } else {
        r = origin.substr(1).match(reg);
    }
    if (r != null) return decodeURIComponent(r[2]);
    return null;
  }

/**
 * 获取url参数(第二种)
 * @param {*} name
 * @param {*} origin
 */
  function getUrlParams(name, origin = null) {
    let url = location.href;
    let temp1 = url.split('?');
    let pram = temp1[1];
    let keyValue = pram.split('&');
    let obj = {};
    for (let i = 0; i < keyValue.length; i++) {
      let item = keyValue[i].split('=');
      let key = item[0];
      let value = item[1];
      obj[key] = value;
    }
    return obj[name];
  }

/**
 * 修改url中的参数
 * @param { string } paramName
 * @param { string } replaceWith
 */
 function replaceParamVal(paramName,replaceWith) {
	var oUrl = location.href.toString();
	var re=eval('/('+ paramName+'=)([^&]*)/gi');
	location.href = oUrl.replace(re,paramName+'='+replaceWith);
	return location.href;
}

/**
 * 删除url中指定的参数
 * @param { string } name
 */
 function funcUrlDel(name){
	var loca =location;
	var baseUrl = loca.origin + loca.pathname + "?";
	var query = loca.search.substr(1);
	if (query.indexOf(name)>-1) {
    var obj = {};
    var arr = query.split("&");
    for (var i = 0; i < arr.length; i++) {
      arr[i] = arr[i].split("=");
      obj[arr[i][0]] = arr[i][1];
    }
    delete obj[name];
    var url = baseUrl + JSON.stringify(obj).replace(/[\"\{\}]/g,"").replace(/\:/g,"=").replace(/\,/g,"&");
    return url
	}
}

/**
 * 获取窗口可视范围的高度
 * @returns clientHeight
 * 
 */
 function getClientHeight() {
	let clientHeight = 0;
	if (document.body.clientHeight && document.documentElement.clientHeight) {
    clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
	}
	else {
    clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
	}
	return clientHeight;
}

/**
 * 获取窗口可视范围宽度
 * @returns a.clientWidth
 * 
 */
 function getPageViewWidth() {
	let d = document,
			a = d.compatMode == "BackCompat" ? d.body : d.documentElement;
	return a.clientWidth;
}

/**
 * 获取窗口宽度
 * @returns clientWidth
 * 
 */
 function getPageWidth() {
	let g = document,
			a = g.body,
			f = g.documentElement,
			d = g.compatMode == "BackCompat" ? a : g.documentElement;
	return Math.max(f.scrollWidth, a.scrollWidth, d.clientWidth);
}

/**
 * 获取窗口尺寸
 * @returns {w, h}
 * 
 */
 function getViewportOffset() {
	if (window.innerWidth) {
    return {
      w: window.innerWidth,
      h: window.innerHeight
    }
	} else {
    // ie8及其以下
    if (document.compatMode === "BackCompat") {
      // 怪异模式
      return {
        w: document.body.clientWidth,
        h: document.body.clientHeight
      }
    } else {
      // 标准模式
      return {
        w: document.documentElement.clientWidth,
        h: document.documentElement.clientHeight
      }
    }
	}
}

/**
 * 获取滚动条距顶部高度
 * @returns scrollTop
 * 
 */
 function getPageScrollTop() {
	let a = document;
	return a.documentElement.scrollTop || a.body.scrollTop;
}

/**
 * 获取滚动条距左边距
 * @returns scrollLeft
 * 
 */
 function getPageScrollLeft() {
	let a = document;
	return a.documentElement.scrollLeft || a.body.scrollLeft;
}

/**
 * 开启全屏
 * @param {*} element
 */
  function launchFullscreen(element) {
	if (element.requestFullscreen) {
    element.requestFullscreen()
	} else if (element.mozRequestFullScreen) {
    element.mozRequestFullScreen()
	} else if (element.msRequestFullscreen) {
    element.msRequestFullscreen()
	} else if (element.webkitRequestFullscreen) {
    element.webkitRequestFullScreen()
	}
}

/**
 * 关闭全屏
 * @param {*} element
 */
 function exitFullscreen() {
	if (document.exitFullscreen) {
    document.exitFullscreen()
	} else if (document.msExitFullscreen) {
    document.msExitFullscreen()
	} else if (document.mozCancelFullScreen) {
    document.mozCancelFullScreen()
	} else if (document.webkitExitFullscreen) {
    document.webkitExitFullscreen()
	}
}

/**
 * 返回当前滚动条位置
 * @param {*} element
 */
 const getScrollPosition = (el = window) => ({
	x: el.pageXOffset !== undefined ? el.pageXOffset : el.scrollLeft,
	y: el.pageYOffset !== undefined ? el.pageYOffset : el.scrollTop
});

/**
 * 滚动到指定元素区域
 * @param {*} element
 */
 const smoothScroll = element =>{
	document.querySelector(element).scrollIntoView({
    behavior: 'smooth'
	});
};

/**
 * 平滑滚动到页面顶部
 */
 const scrollToTop = () => {
	const c = document.documentElement.scrollTop || document.body.scrollTop;
	if (c > 0) {
    window.requestAnimationFrame(scrollToTop);
    window.scrollTo(0, c - c / 8);
	}
};

/**
 * http跳转https
 * 
*/
 const httpsRedirect = () => {
	if (location.protocol !== 'https:') location.replace('https://' + location.href.split('//')[1]);
};

/**
 * 检查页面底部是否可见
 * 
*/
 const bottomVisible = () =>{
	return document.documentElement.clientHeight + window.scrollY >=
		(document.documentElement.scrollHeight || document.documentElement.clientHeight);
};

/**
 * 打开一个窗口
 * @param { string } url
 * @param { string } windowName
 * @param { number } width
 * @param { number } height
 */
 function openWindow(url, windowName, width, height) {
	var x = parseInt(screen.width / 2.0) - width / 2.0;
	var y = parseInt(screen.height / 2.0) - height / 2.0;
	var isMSIE = navigator.appName == "Microsoft Internet Explorer";
	if (isMSIE) {
    var p = "resizable=1,location=no,scrollbars=no,width=";
    p = p + width;
    p = p + ",height=";
    p = p + height;
    p = p + ",left=";
    p = p + x;
    p = p + ",top=";
    p = p + y;
    window.open(url, windowName, p);
	} else {
    var win = window.open(
      url,
      "ZyiisPopup",
      "top=" +
      y +
      ",left=" +
      x +
      ",scrollbars=" +
      scrollbars +
      ",dialog=yes,modal=yes,width=" +
      width +
      ",height=" +
      height +
      ",resizable=no"
    );
    eval("try { win.resizeTo(width, height); } catch(e) { }");
    win.focus();
	}
}

/**
 * 自适应页面(rem)
 * @param { number } width
 */
 function AutoResponse(width = 750) {
	const target = document.documentElement;
	target.clientWidth >= 600
    ? (target.style.fontSize = "80px")
    : (target.style.fontSize = target.clientWidth / width * 100 + "px");
}

浏览器存储相关storage工具函数

/**
 * localStorage 存贮
 * 目前对象值如果是函数 、RegExp等特殊对象存贮会被忽略
 * @param { String } key  属性
 * @param { string } value 值
 */
 const localStorageSet = (key, value) => {
  if (typeof (value) === 'object') value = JSON.stringify(value);
  localStorage.setItem(key, value)
};

/**
 * localStorage 获取
 * @param {String} key  属性
 */
 const localStorageGet = (key) => {
  return localStorage.getItem(key)
};

/**
 * localStorage 移除
 * @param {String} key  属性
 */
 const localStorageRemove = (key) => {
  localStorage.removeItem(key)
};

/**
 * localStorage 存贮某一段时间失效
 * @param {String} key  属性
 * @param {*} value 存贮值
 * @param { number } expire 过期时间,毫秒数
 */
 const localStorageSetExpire = (key, value, expire) => {
  if (typeof (value) === 'object') value = JSON.stringify(value);
  localStorage.setItem(key, value);
  setTimeout(() => {
      localStorage.removeItem(key)
  }, expire)
};

/**
 * sessionStorage 存贮
 * @param {String} key  属性
 * @param {*} value 值
 */
 const sessionStorageSet = (key, value) => {
  if (typeof (value) === 'object') value = JSON.stringify(value);
  sessionStorage.setItem(key, value)
};

/**
 * sessionStorage 获取
 * @param {String} key  属性
 */
 const sessionStorageGet = (key) => {
  return sessionStorage.getItem(key)
};

/**
 * sessionStorage 删除
 * @param {String} key  属性
 */
  const sessionStorageRemove = (key) => {
  sessionStorage.removeItem(key)
};

/**
 * sessionStorage 存贮某一段时间失效
 * @param {String} key  属性
 * @param {*} value 存贮值
 * @param {String} expire 过期时间,毫秒数
 */
  const sessionStorageSetExpire = (key, value, expire) => {
  if (typeof (value) === 'object') value = JSON.stringify(value);
  sessionStorage.setItem(key, value);
  setTimeout(() => {
      sessionStorage.removeItem(key)
  }, expire)
};

/**
 * cookie 存贮
 * @param {String} key  属性
 * @param {*} value  值
 * @param { String } expire  过期时间,单位天
 */
  const cookieSet = (key, value, expire) => {
  const d = new Date();
  d.setDate(d.getDate() + expire);
  document.cookie = `${key}=${value};expires=${d.toUTCString()}`
};

/**
 * cookie 获取
 * @param {String} key  属性
 */
  const cookieGet = (key) => {
  const cookieStr = unescape(document.cookie);
  const arr = cookieStr.split('; ');
  let cookieValue = '';
  for (let i = 0; i < arr.length; i++) {
    const temp = arr[i].split('=');
    if (temp[0] === key) {
      cookieValue = temp[1];
      break
    }
  }
  return cookieValue
};

/**
 * cookie 删除
 * @param {String} key  属性
 */
 const cookieRemove = (key) => {
  document.cookie = `${encodeURIComponent(key)}=;expires=${new Date()}`
};

开源协议

ISC