js-public-tools

1.0.9 • Public • Published

public-tools

Project setup

npm install

Compiles and hot-reloads for development

npm run serve

Compiles and minifies for production

npm run build

Lints and fixes files

npm run lint

Customize configuration

See Configuration Reference.

// 对象的深拷贝,传入要拷贝的对象,返回新对象 this.$public.deepClone(obj) // let newObj = this.$public.deepClone(obj)

// 对象深合并,传入要合并的对象,返回合并后的新对象
this.$public.deepMerge(obj1,obj2) // let mergeObj = this.$public.deepMerge(obj1,obj2)

// 数组乱序
this.$public.randomArray(array) // let newArr = this.$public.randomArray(array)
	
	// 数组乱序
this.$public.randomArray(array) // let newArr = this.$public.randomArray(array)

// 获取滚动条距离,返回值格式{ x:0, y:0 }
this.$public.getScrollOffset()

// 获取视口尺寸,返回值格式{ w:1920, h:1080 },兼容IE8及以下版本
this.$public.getViewportOffset()

// 去除空格 ,pos可选为:all(默认)-去除包括中间和两端的所有空格,
//both,去除两端空格,left-去除左边空格,right-去除右边空格,
this.$public.trim(str,pos='all')

// RGB转十六进制Hex
this.$public.rgbToHex('rgb(13, 145, 20)')

// 十六进制Hex转RGB
this.$public.hexToRgb('#0afdce')

// 随机数值,第一个参数最小值,第二个参数最大值
this.$public.random(1,10)//在1和10之间随机出

// 格式化数量,一般在数字过长的情况下使用,超过万位数开始格式化
this.$public.formatQuantity(1234567)//1234.5k
	
	// 字符串超出多少字显示省略号
this.$public.strOut(str,len)//str:要操作的字符串,len:字数长度
	
	// 将数字转换为大写金额
this.$public.changeToChinese(num)// 最高精确到分  1899.38=>壹仟捌佰玖拾玖圆叁角捌分	
	
	// 字符转换
this.$public.changeCase(str,type)//str:要处理的字符, type: 1:首字母大写 2:首字母小写 3:大小写转换 4:全部大写 5:全部小写
	
	// 格式化时间格式,第一个参数为时间戳,可以传入null则表示格式化当前时间
	// 第二个参数自定义格式化规则,自由搭配
	this.$public.timeFormat(1581170184,'yyyy-mm-dd hh:MM:ss')
	
	/**
	 * 时间戳转为多久之前
	 * @param String timestamp 时间戳
	 * @param String | Boolean format 如果为时间格式字符串,超出一定时间范围,返回固定的时间格式;
	 * 如果为布尔值false,无论什么时间,都返回多久以前的格式
	 */
	this.$public.timeFrom(timestamp = null, format = 'yyyy-mm-dd')	// yyyy-mm-dd hh:MM:ss
	
	/**
	 * 日期位于一年中的第几天
	 * @param {Date} date 日期
	 * @return {Number} 在一年中的第几天
	 */
	this.$public.dateOfYear(date)	
	
	/**
	 * 两个日期间相差多少天
	 * @param {Date} date1 日期1
	 * @param {Date} date2 日期2
	 * @return {Number} 相差天数
	 */
	this.$public.dateDiffer(date1, date2)
	
	/**
	 * 对象转url参数
	 * @param {*} data,对象
	 * @param {*} isPrefix,是否自动加上"?"
	 */
	this.$public.queryToParams(data = {}, isPrefix = true, arrayFormat = 'brackets') 
	
	/*
	 * 参数说明:
	 * number:要格式化的数字
	 * decimals:保留几位小数
	 * decimalPoint:小数点符号
	 * thousandsSeparator:千分位符号
	 * */
	this.$public.priceFormat(number, decimals = 0, decimalPoint = '.', thousandsSeparator = ',')
	
/**
* 全局唯一标识符
* @param {Number} len uuid的长度
* @param {Boolean} firstGt 将返回的首字母置为"gt"
* @param {Nubmer} radix 生成uuid的基数(意味着返回的字符串都是这个基数),
*2-二进制,8-八进制,10-    十进制,16-十六进制
* 注:v-for循环的时候,如果使用数组的index导致更新列表出现问题,
*在循环中使用 :key="$public.guid()"可以解决
*/
this.$public.guid(len = 32, firstGt = true, radix = null)

/**
* 节流
* @param {Function} func 触发回调执行的函数
* @param {Number} wait 时间间隔,单位ms
* @param {Boolean} immediate 在开始还是结束处触发,如果为true,在第一次触发回调
*/
this.$public.throttle(func, wait = 500, immediate = true)

/**
* 防抖
* @param {Function} func 触发回调执行的函数
* @param {Number} wait 时间间隔,单位ms
* @param {Boolean} immediate 在开始还是结束处触发,一般false
*/
this.$public.debounce(func, wait = 500, immediate = false)
	
	/*以下是正则验证方法封装,统一返回true或者false*/
	//是否数组
	this.$public.test.array(param)
	
	//是否Json字符串
	this.$public.test.jsonString(param)
	
	//是否对象
	this.$public.test.object(param)
	
	//是否函数
	this.$public.test.func(param)
	
	//是否Promise对象
	this.$public.test.promise(param)
	
	//是否图片格式
	this.$public.test.image(param)
	
	//是否视频格式
	this.$public.test.video(param)
	
	//是否正则对象
	this.$public.test.regExp(param)
	
	//是否邮箱
	this.$public.test.email(param)
	
	//是否整数
	this.$public.test.digits(param)
	
	//是否只含有字母
	this.$public.test.letter(param)
	
	//是否只含有数字和字母
	this.$public.test.enOrNum(param)
	
	//验证一个值范围 value:要验证的数字,ran:要验证的范围[min, max]
	this.$public.test.range(value,ran)
	
	//是否字符串
	this.$public.test.string(param)
	
	//验证身份证号
	this.$public.test.idCard(param)
	
	//是否为空(包含了undefined、空字符串、false、0、{}、[])返回true表示为空
	this.$public.test.isEmpty(param)
	
	//字符串中是否包含某一个子字符串,区分大小写,subStr:子字符串 
	this.$public.test.contains(str, subStr)
	
	//字符串长度是否在某个范围内,如"abc"长度为3,范围在"3-10"这个区间,结果返回true
	this.$public.test.rangeLength('abc', [3, 10])
	
	/****************↑↑↑↑↑↑↑↑↑↑正则验证 ↑↑↑↑↑↑↑↑↑↑*****************/

Readme

Keywords

none

Package Sidebar

Install

npm i js-public-tools

Weekly Downloads

0

Version

1.0.9

License

ISC

Unpacked Size

355 kB

Total Files

36

Last publish

Collaborators

  • yangchen_npm