# itw-utils **Repository Path**: wangmaoming/itw-utils ## Basic Information - **Project Name**: itw-utils - **Description**: 用于发布npm包,通常使用的utils工具 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-06-25 - **Last Updated**: 2023-03-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 安装 ``` npm install itw-utils ``` # 导入 ```js const itw = require('itw-utils') ``` # 示例 ## 时间处理 ### 格式化时间 ```js // 调用 dateFormat 对时间进行格式化 const dtStr = itw.dateFormat(new Date()) // 结果 2020-04-03 17:20:58 console.log(dtStr) ``` ### 日期格式化 ```js this.parseTime(date) this.parseTime(date,'{y}年{m}月{d}日{h}时{i}分{s}秒') ``` ### 添加日期范围 ```js // queryParams 请求数据params;dateRange日期 v-model ; propName 时间节点名称:beginTime、endTime或者"begin" + propName、"end" + propName this.addDateRange(this.queryParams, this.dateRange,this.propName) ``` ### 获取当前日期,格式YYYY-MM-DD ```js let newDate=new Date() this.getNowFormatDay(newDate) ``` ### 取当前时间,格式YYYY-MM-DD HH:mm:ss ```js // 返回当前日期YYYY-MM-DD HH:mm:ss this.getNowFormatTime() ``` ### 获取最近3天日期 -今天 -昨天 -明天 ```js /** * getDay(day) //day为数字类型,0代表今日,-1代表昨日,1代表明日,返回yyyy-mm-dd格式字符串,day不传默认代表今日。 */ this.getDay(0) this.getDay(-1) this.getDay(1) ``` ### 获取最近3周起止日期 -本周 -上周 -下周 ```js /** * 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月起止日期 -本月 -上月 -下月 ```js /** * getMonth(type,months) //type为字符串类型,有两种选择,"s"代表开始,"e"代表结束,months为数字类型,不传或0代表本月,-1代表上月,1代表下月 */ getMonth("s",1) //得到下月第一天的yyyy-mm-dd格式日期 getMonth("e",1) //得到下月最后一天的yyyy-mm-dd格式日期 ``` ### 获取最近3年起止日期 -本年 -上年 -下年 ```js /** * getYear(type,dates) //type为字符串类型,有两种选择,"s"代表开始,"e"代表结束,dates为数字类型,不传或0代表今年,-1代表去年,1代表明年 */ getYear("s",1) //得到明年第一天的yyyy-mm-dd格式日期 getYear("e",1) //得到明年最后一天的yyyy-mm-dd格式日期 ``` ## 数组处理 ### 克隆数组 ```js /** * 克隆数组 * @param {Array} actual * @returns {Array} */ this.cleanArray(arr) ``` ### 数组去重 ```js console.log(unique([11,11,11,2,3,3,3,function a(){},{a:15}])) // [ 11, 2, 3, [Function: a], { a: 15 } ] ``` ## 对象处理 ### 对象转数组 ```js /** * 对象转数组 * @param {Object} json * @returns {Array} */ this.objectToArray(json) ``` ### url参数和obj互转的方法 ```js this.param2Obj(url) this.obj2Param(obj) ``` ### 对象合并 ```js /** * 对象合并 * Merges two objects, giving the last one precedence * @param {Object} target * @param {(Object|Array)} source * @returns {Object} */ this.objectMerge(target, source) ``` ### 深克隆对象 ```js /** * 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) ``` ## 字符串处理 ### 字符串格式化 ```js let template = '

%d,%d!

'; console.log(sprintf(template , 'Hello' , 'World')); // Hello,World! ``` ### 转换字符串 ```js /** * val undefined,null等转化为"" */ this.praseStrEmpty(val) ``` ### 判断是否是中文 ```js /** * 判断是否是中文 * @param {*} str * @returns */ this.checkCh(str) ``` ### 计算字节长度utf8 ```js this.byteLength(str) ``` ### 随机唯一字符串 ```js /** * @param {number} num 字符串长度 * 获取随机唯一字符串 * @returns {string} */ this.createUniqueString(5) ``` ## 数字处理 ### 将数字转换成万、亿、万亿 ```js let num =1000000 this.numberFormat(num) ``` ### 判断是否是数字 ```js this.isNumberStr(str) ``` ## 数据处理 ### 构造树型结构数据 ```js /** * @param {*} data 数据源 一维数组 * @param {*} id id字段 默认 'id' * @param {*} parentId 父节点字段 默认 'parentId' * @param {*} children 孩子节点字段 默认 'children' * @param {*} rootId 根Id 默认 0 */ this.handleTree(data, id, parentId, children, rootId); ``` ### 获取网址url上拼的参数 ```js /** * 获取网址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"} ``` ## 字典处理 ### 回显数据字典 ```js /** * datas 数组对象 [{dictValue:'string',dictLabel:'string'}] * value 字典中需要查找相同的dictValue * dictValue 字典对象的value或者key * dictLabel 字典对象的文字 */ this.selectDictLabel(datas, value, dictValue, dictLabel) ``` ### 回显数据字典(字符串数组) ```js /** * datas 数组对象 [{dictValue:'string',dictLabel:'string'}] * value 字典中需要查找相同的dictValue * separator value字段的分离器 value.split(currentSeparator) */ this.selectDictLabels(datas, value, separator) ``` ## 单位处理 ### 存储单位换算成字节长度 ```js /** * * @param {*} value 字符串 单位大写 例如:10KB或者10K * @returns 返回数字 */ this.sizeToByte(value) ``` ### 存储单位大小转化 ```js /** * * @param {*} limit 数字 例如:1024 * @returns 1KB */ this.byteToSize(limit) ``` ## html处理 ### 转义 HTML 中的特殊字符 ```js // 带转换的 HTML 字符串 const htmlStr = '

这是h1标签123 

' // 调用 htmlEscape 方法进行转换 const str = itw.htmlEscape(htmlStr) // 转换的结果 <h1 title="abc">这是h1标签<span>123&nbsp;</span></h1> console.log(str) ``` ### 还原 HTML 中的特殊字符 ```js // 待还原的 HTML 字符串 const str2 = itw.htmlUnEscape(str) // 输出的结果

这是h1标签123 

console.log(str2) ``` ### html转text ```js /* * @param {string} val * @returns {string} */ this.html2Text(val) ``` ### 切换class名称 ```js /** * 切换class,没有class就是新增 * @param {HTMLElement} element * @param {string} className */ this.toggleClass(element,className) ``` ### 判断是否有该class ```js /** * 判断是否有该class * Check if an element has a class * @param {HTMLElement} elm * @param {string} cls * @returns {boolean} */ this.hasClass(element,className) ``` ### 添加class ```js /** * Add class to element * @param {HTMLElement} elm * @param {string} cls */ this.addClass(element,className) ``` ### 删除class ```js /** * Remove class from element * @param {HTMLElement} elm * @param {string} cls */ this.removeClass(element,className) ``` ### 判断标签是原生标签还是用户自定义的组件 ```js // vue源码中的makeMap用在很多地方,主要是判断标签是原生标签还是用户自定义的组件 // 但是标签很多,如果每判断一次都执行一次循环,累计下来,性能损耗还是很大的 // makeMap就是解决这个问题出现的 let tags = `div,p,a,img,ul,li`.split(","); let isHTMLTag = this.makeMap(tags); ``` ## 表单 ### 表单重置 ```js this.resetForm("form"); ``` ## 其他 ### 定义一个补零的函数 ```js this.padZero(val); ``` ### 通用下载方法 ```js this.currencyDownload(url); ``` ## 开源协议 ISC