itw-utils

1.3.5 • Public • Published

[TOC]

安装

npm install itw-utils

导入

const itw = require('itw-utils')

示例

时间处理

格式化时间

// 调用 dateFormat 对时间进行格式化
const dtStr = itw.dateFormat(new Date())
// 结果  2020-04-03 17:20:58
console.log(dtStr)

日期格式化

//date --时间戳  第二参数不填 默认 {y}-{m}-{d} {h}:{i}:{s}
this.parseTime(date)
this.parseTime(date,'{yyyy}-{mm}-{dd}')
this.parseTime(date,'{y}年{m}月{d}日{h}时{i}分{s}秒')

添加日期范围

// queryParams 请求数据params;dateRange日期 v-model ; propName 时间节点名称:beginTime、endTime或者"begin" + propName、"end" + propName
this.addDateRange(this.queryParams, this.dateRange,this.propName)

获取当前日期,格式YYYY-MM-DD

let newDate=new Date()
this.getNowFormatDay(newDate)

取当前时间,格式YYYY-MM-DD HH:mm:ss

// 返回当前日期YYYY-MM-DD HH:mm:ss
this.getNowFormatTime()

获取最近3天日期 -今天 -昨天 -明天

/**
 *  getDay(day)  //day为数字类型,0代表今日,-1代表昨日,1代表明日,返回yyyy-mm-dd格式字符串,day不传默认代表今日。
 */
this.getDay(0)
this.getDay(-1)
this.getDay(1)

获取最近3周起止日期 -本周 -上周 -下周

/**
 *  getMonday(type,dates)  //type为字符串类型,有两种选择,"s"代表开始,"e"代表结束,dates为数字类型,不传或0代表本周,-1代表上周,1代表下周
 */
this.getMonday("s",1)  //得到下周一的yyyy-mm-dd格式日期
this.getMonday("e",1)  //得到下周日的yyyy-mm-dd格式日期

获取最近3月起止日期 -本月 -上月 -下月

/**
 *  getMonth({date,type,months})  //date日期可不填写,type为字符串类型,有两种选择,"s"代表开始,"e"代表结束,months为数字类型,不传或0代表本月,-1代表上月,1代表下月
 */
getMonth({date:date,type:"s",months:1})  //得到下月第一天的yyyy-mm-dd格式日期
getMonth({date:date,type:"e",months:1})  //得到下月最后一天的yyyy-mm-dd格式日期

获取最近3年起止日期 -本年 -上年 -下年

/**
 *  getYear(type,dates)  //type为字符串类型,有两种选择,"s"代表开始,"e"代表结束,dates为数字类型,不传或0代表今年,-1代表去年,1代表明年
 */
getYear("s",1)  //得到明年第一天的yyyy-mm-dd格式日期
getYear("e",1)  //得到明年最后一天的yyyy-mm-dd格式日期

获取两个日期相差天数

getDayNumByStartEnd('2021-11-08','2020-11-08')

数组处理

克隆数组

/**
 * 克隆数组
 * @param {Array} actual
 * @returns {Array}
 */
this.cleanArray(arr)

数组去重--数字字符串

console.log(unique([11,11,11,2,3,3,3,function a(){},{a:15}]))
// [ 11, 2, 3, [Function: a], { a: 15 } ]

数组去重--对象

console.log(reduceObjInArrUnique({a:15},{a:15}]))
// [{ a: 15 } ]

数组中找到对应的值

/**
 * 数组中找到某个key名称
 * @param {*} arr 原始数组
 * @param {*} by string 通过某个key比如说“id”
 * @param {*} key  传入的值,比如id
 * @param {*} value  返回的值对应的key,比如“label”
 * @returns  key 
 */

//findValueByKey(arr, by, key, value)
let arr=[{id:1,label:"返回1"},{id:2,label:"返回2"}]

findValueByKey(arr,"id",id,"label");

对象处理

对象转数组

/**
 * 对象转数组
 * @param {Object} json
 * @returns {Array}
 */
this.objectToArray(json)

url参数和obj互转的方法

this.param2Obj(url)
this.obj2Param(obj)

对象合并

/**
 * 对象合并
 * Merges two objects, giving the last one precedence
 * @param {Object} target
 * @param {(Object|Array)} source
 * @returns {Object}
 */
this.objectMerge(target, source) 

深克隆对象

/**
 * This is just a simple version of deep copy
 * Has a lot of edge cases bug
 * If you want to use a perfect deep copy, use lodash's _.cloneDeep
 * @param {Object} source
 * @returns {Object}
 */
this.deepClone(source)

字符串处理

字符串格式化

let template = '<h1>%d,%d!</h1>';
console.log(sprintf(template , 'Hello' , 'World'));
// Hello,World!

转换字符串

/**
 * val  undefined,null等转化为""
 */
this.praseStrEmpty(val)

判断是否是中文

/**
 * 判断是否是中文
 * @param {*} str
 * @returns
 */
this.checkCh(str)

计算字节长度utf8

this.byteLength(str)

随机唯一字符串

/**
 * @param {number} num 字符串长度
 * 获取随机唯一字符串
 * @returns {string}
 */
this.createUniqueString(5)

数字处理

将数字转换成万、亿、万亿

let num =1000000
this.numberFormat(num)

判断是否是数字

this.isNumberStr(str)

数据处理

构造树型结构数据

/**
 * @param {*} data 数据源 一维数组
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 * @param {*} rootId 根Id 默认 0
 */
this.handleTree(data, id, parentId, children, rootId);

获取网址url上拼的参数

/**
 * 获取网址url上拼的参数
 * @param {string} url
 * @returns {Object}
 */
let ='http://www.w3school.com.cn/tiy/t.asp?f=js_library_jquery'
this.getQueryObject(url)
//Object {f: "js_library_jquery"}

字典处理

回显数据字典

/** 
 * datas  数组对象 [{dictValue:'string',dictLabel:'string'}]
 * value 字典中需要查找相同的dictValue
 * dictValue 字典对象的value或者key --不填写就默认是dictValue
 * dictLabel 字典对象的文字 --不填写就默认是dictLabel
*/

this.selectDictLabel(datas, value, dictValue, dictLabel)

回显数据字典(字符串数组)

/**
 * datas 数组对象 [{dictValue:'string',dictLabel:'string'}]
 * value 字典中需要查找相同的dictValue
 * separator  value字段的分离器 value.split(currentSeparator)
 */
this.selectDictLabels(datas, value, separator)

单位处理

存储单位换算成字节长度

/**
 *
 * @param {*} value  字符串 单位大写 例如:10KB或者10K
 * @returns 返回数字
 */
this.sizeToByte(value)

存储单位大小转化

/**
 *
 * @param {*} limit  数字 例如:1024
 * @returns 1KB
 */
this.byteToSize(limit)

html处理

转义 HTML 中的特殊字符

// 带转换的 HTML 字符串
const htmlStr = '<h1 title="abc">这是h1标签<span>123&nbsp;</span></h1>'
// 调用 htmlEscape 方法进行转换
const str = itw.htmlEscape(htmlStr)
// 转换的结果 &lt;h1 title=&quot;abc&quot;&gt;这是h1标签&lt;span&gt;123&amp;nbsp;&lt;/span&gt;&lt;/h1&gt;
console.log(str)

还原 HTML 中的特殊字符

// 待还原的 HTML 字符串
const str2 = itw.htmlUnEscape(str)
// 输出的结果 <h1 title="abc">这是h1标签<span>123&nbsp;</span></h1>
console.log(str2)

html转text

/*
 * @param {string} val
 * @returns {string}
 */
this.html2Text(val)

切换class名称

/**
 * 切换class,没有class就是新增
 * @param {HTMLElement} element
 * @param {string} className
 */
this.toggleClass(element,className)

判断是否有该class

/**
 * 判断是否有该class
 * Check if an element has a class
 * @param {HTMLElement} elm
 * @param {string} cls
 * @returns {boolean}
 */
this.hasClass(element,className)

添加class

/**
 * Add class to element
 * @param {HTMLElement} elm
 * @param {string} cls
 */
this.addClass(element,className)

删除class

/**
 * Remove class from element
 * @param {HTMLElement} elm
 * @param {string} cls
 */
this.removeClass(element,className)

判断标签是原生标签还是用户自定义的组件

// vue源码中的makeMap用在很多地方,主要是判断标签是原生标签还是用户自定义的组件

// 但是标签很多,如果每判断一次都执行一次循环,累计下来,性能损耗还是很大的

// makeMap就是解决这个问题出现的

let tags = `div,p,a,img,ul,li`.split(",");
let isHTMLTag = this.makeMap(tags);

表单

表单重置

//refs-this.$refs
this.resetForm(refs,"form");

图片处理

压缩图片

/**
 * 压缩图片
 *@param img 被压缩的img对象
 * @param type 压缩后转换的文件类型
 * @param mx 触发压缩的图片最大宽度限制
 * @param mh 触发压缩的图片最大高度限制
 */
this.compressImg(img, type, mx, mh)

压缩前将file转换成img对象

this.readImg(file)

其他

定义一个补零的函数

// 小于10的数据补零9---09
this.padZero(val);

通用下载方法

//通用通过下载链接下载文件
this.currencyDownload(url);

首字母大写

//通用通过下载链接下载文件
this.titleCase("ssss");
//Ssss

开源协议

ISC

Readme

Keywords

Package Sidebar

Install

npm i itw-utils

Weekly Downloads

0

Version

1.3.5

License

ISC

Unpacked Size

38.1 kB

Total Files

16

Last publish

Collaborators

  • wangmaoming