# common-tools-gyo **Repository Path**: geyaomanong/common-tools-gyo ## Basic Information - **Project Name**: common-tools-gyo - **Description**: - 🍒 寻找一个前端工具包? - 🍑 学习发布一个npm包? - 🍐 尝试使用ts? - 🍎 集成前端各种常用方法? 🍉🍉🍉 这里都有!!! - **Primary Language**: TypeScript - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-06-23 - **Last Updated**: 2022-07-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: npm, TypeScript, JavaScript ## README # montjs-fe ## 一.介绍 > 用typescript写的常用工具类,由于不同项目中会使用到相关的方法,所以将这些方法统一规整到一个包中,源码已分享(源码中包含部分单元测试),大家可以查看或者提需求。 ## 二.如何使用 ``` yarn add montjs-fe / npm install montjs-fe -D // 所有方法全部引入 import * as util from 'montjs-fe'; // 局部引入 import { strCheck } from 'montjs-fe'; ``` ## 三.API ## 1.string > 1.1 strCheck 验证 ``` util.strCheck(str:string, type: StrEnum) => boolean enum StrEnum { PHONE = 'phone', //校验手机号 TEL = 'tel', //检验电话 CARD = 'card', //检验身份证 PWD = 'pwd', // 密码以字母开头,长度在6~18之间,只能包含字母、数字和下划线 POSTAL = 'postal', // 邮政编码 QQ = 'qq', // QQ号 EMAIL = 'email', // 邮箱 MONEY = 'money', // 金额(小数点2位) URL = 'URL', // 网址 IP = 'IP', // IP DATE = 'date', // 日期时间 NUMBER ='number', // 数字 ENGLISH = 'english', // 英文 CHINESE = 'chinese', // 中文 LOWER= 'lower', // 小写 UPPER = 'upper', // 大写 HTML = 'HTML' // HTML标记 } // example util.strCheck('18268100000', 'phone') => true ``` > 1.2去除字符串空格 ```trim util.trim(str: string, type: number) => str type 1-所有空格 2-前后空格 3-前空格 4-后空格(默认type为1) ``` > 1.3根据内容超过指定长度显示... ```sliceContent util.sliceContent(cont: string, len: number = 8) ``` > 1.4 手机号脱敏 ```phoneDes util.phoneDes(phone: string ) ``` > 1.5 身份证号码脱敏 ```phoneDes util.identifynumberDes(phone: string ) ``` 1.2 strTransformName 两个字名字中间加空格 ``` util.strTransformName(name: string) => cname // example util.strTransformName('王五') => '王 五' ``` ## 2.object 2.1 objIsNull 对象判空 ``` util.objIsNull(obj: object) => boolean // example const obj = {} util.objIsNull(obj) => false // 传参不需要判空,代码里已判空 ``` ## 3.array 3.1 arrIsNull 数组判空 ``` util.arrIsNull(arr: any[]) => boolean // example const arr = [] util.arrIsNull(arr) => false // 传参不需要判空,代码里已判空 ``` ## 4.store 4.1 storeCookieSet 设置某个cookie ``` util.storeCookieSet(str: string, value: string) => void // example util.storeCookieSet('token', 'token') ``` 4.2 storeCookieGet 获得coookie中某个值 ``` util.storeCookieGet(str: string) => string // example document.cookie="token=token"; util.storeCookieGet('token') => 'token' ``` 4.3 storeCookieDelete 删除单条cookie ``` util.storeCookieDelete(str: string) => void ``` 4.4 storeCookieRemove 删除所有cookie ``` util.storeCookieRemove() => void ``` 4.5 storeLocalStorageSet 设置localStorage 已经JSON.stringify ``` util.storeLocalStorageSet(name: string, value: any) => void ``` 4.6 storeLocalStorageGet 获取localStorage 已经JSON.parse ``` util.storeLocalStorageGet(str: string) => any ``` ## 5.uri 5.1 uriGetParam 获取uri上的某个参数 ``` util.uriGetParam(str: string) => string // example www.xxx.com?a=1&b=2 util.uriGetParam('a') => '1' ``` ## 6.浏览器 6.1 browserGetType 获取浏览器类型 ``` util.browserGetType() => string // example util.browserGetType() => 'Opera' || 'IE' || 'Edge' || 'Firefox' || 'Safari' || 'Chrome' || 'OverIE10' ``` 6.1 browserIsNew 是否是现代浏览器(IE11及以上) ``` util.browserIsNew() => boolean // example // 如果是IE11及以上,返回true util.browserIsNew() => true ``` ### 7. timeUtils 7.1 timeFix 根据时间显示问候语 ``` util.timeFix() => string // examle util.timeFix() => '上午好' ``` 7.2 timeAgo 返回多久时间之前(入参为时间戳) ``` util.timeAgo() => string // examle util.timeAgo(1657028474581) => '1 minutes' ``` 7.3 getNowTime 获取当前格式话时间(入参可参照TimeTypes枚举对象) ``` util.getNowTime() => string // examle util.getNowTime() => '2022-07-05 12:12:12' ``` 7.4 formatTime 获取当前格式话时间(入参可参照TimeTypes枚举对象) ``` util.formatTime(Date, type) => string // examle util.getNowTime(new Date(), type) => '2022-07-05 12:12:12' ``` 7.5 dateArray 根据开始与结束时间输出该日渐范围所有日期 ``` util.dateArray(Date, type) => array // examle util.dateArray('2022-02-08', '2022-02-09') => ['2022-02-08','2022-02-09'] ```