From 2d3a4cd7a2ad9c34b4af8181bcbbfe7d846c0b9c Mon Sep 17 00:00:00 2001 From: saqqdy Date: Fri, 3 Sep 2021 09:39:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84typescript=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- es/getAppVersion.js | 15 ++++++++++----- src/getAppVersion.ts | 6 +----- src/getCHSLength.ts | 2 +- src/getCache.ts | 2 +- src/getCookie.ts | 2 +- src/getDirParam.ts | 9 +++++++-- src/getFileType.ts | 16 ++++++++++++---- src/getIsAppVersionLastest.ts | 19 ++++++++++--------- src/getNumber.ts | 7 ++++++- src/getOsVersion.ts | 7 ++++++- 10 files changed, 55 insertions(+), 30 deletions(-) diff --git a/es/getAppVersion.js b/es/getAppVersion.js index 46270078..4d038ab4 100644 --- a/es/getAppVersion.js +++ b/es/getAppVersion.js @@ -1,8 +1,13 @@ -'use strict'; - +'use strict'; + /** * 获取APP版本号 * + * @example + * ``` + * getAppVersion('iPhone', true) // 'iPhone/13.2.3' + * getAppVersion('iPhone', true) // 'iPhone/13.2.3' + * ``` * @param appName - app名称 * @param withosstr - 是否需要带上名称 * @param userAgent - ua,可不传,默认取navigator.appVersion @@ -39,6 +44,6 @@ function getAppVersion(appName, withappstr, userAgent) { return null; } } -} - -module.exports = getAppVersion; +} + +module.exports = getAppVersion; diff --git a/src/getAppVersion.ts b/src/getAppVersion.ts index 8d89ebc0..e85818fd 100644 --- a/src/getAppVersion.ts +++ b/src/getAppVersion.ts @@ -6,15 +6,11 @@ * @param userAgent - ua,可不传,默认取navigator.appVersion * @return null/true/false */ -function getAppVersion(appName: string, withappstr: boolean, userAgent: string): string | boolean | null { - // console.log(getAppVersion("Chrome")); - // const userAgent = navigator.userAgent; +function getAppVersion(appName: string, withappstr?: boolean, userAgent?: string): string | boolean | null { userAgent = userAgent || navigator.appVersion var reg = eval('/' + appName + '\\/([\\d\\.]+)/i') var isApp = userAgent.includes(appName) var ver = userAgent.match(reg) - // console.log(userAgent) - // console.log(ver) // withappstr = typeof(withappstr) != "undefined" ? withappstr : false; if (ver) { if (withappstr) { diff --git a/src/getCHSLength.ts b/src/getCHSLength.ts index 1d5fb035..079463e6 100644 --- a/src/getCHSLength.ts +++ b/src/getCHSLength.ts @@ -20,7 +20,7 @@ * @param str - 字符串 * @returns 返回长度 */ -function getCHSLength(str: string) { +function getCHSLength(str: string): number { return str.replace(/[^\x00-\xff]/g, '**').length } diff --git a/src/getCache.ts b/src/getCache.ts index 3488a5c6..7d81c6ae 100644 --- a/src/getCache.ts +++ b/src/getCache.ts @@ -4,7 +4,7 @@ * @param name - 缓存名称 * @returns 返回数据,存的如果是对象,取出的也是对象 */ -function getCache(name: string) { +function getCache(name: string): any { let str = localStorage.getItem(name), exp = new Date(), o diff --git a/src/getCookie.ts b/src/getCookie.ts index 55e385eb..5c049a0d 100644 --- a/src/getCookie.ts +++ b/src/getCookie.ts @@ -4,7 +4,7 @@ * @param name - cookie名称 * @returns 返回cookie字符串 */ -function getCookie(name: string) { +function getCookie(name: string): any { var arr, reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)') arr = document.cookie.match(reg) diff --git a/src/getDirParam.ts b/src/getDirParam.ts index 89efde48..3c9ebbfa 100644 --- a/src/getDirParam.ts +++ b/src/getDirParam.ts @@ -1,13 +1,18 @@ +export interface DirParamType { + path?: string[] + host?: string +} + /** * 获取目录形式URL参数 * * @param url - 传入url地址 * @returns 返回参数对象 */ -function getDirParam(url: string) { +function getDirParam(url: string): DirParamType { var urlStr = url !== '' && typeof url !== 'undefined' ? url.replace(/^http[s]?:\/\/[^\/]+([\s\S]*)/, '$1') : location.pathname // 获取url中域名后的字串:/post/0703/a1.html urlStr = urlStr.replace(/^\//, '') - var dirParam: { [prop: string]: any } = {} + var dirParam: DirParamType = { path: [], host: '' } // 获取域名,包含http:// if (url !== '' && typeof url !== 'undefined') { var match = url.match(/^http[s]?:\/\/[^\/]+/) diff --git a/src/getFileType.ts b/src/getFileType.ts index b2d9e9c4..dce3c90a 100644 --- a/src/getFileType.ts +++ b/src/getFileType.ts @@ -1,15 +1,23 @@ /** * 文件后缀名 * + * @example + * ```js + * getFileType('http://www.saqqdy.com/test.jpg'); // .jpg; + * ``` + * @example + * ```js + * getFileType('http://www.saqqdy.com/test.JPEG'); // .jpeg; + * ``` * @param url - 文件名 * @returns 返回文件后缀 */ -function getFileType(url: string): string | null { +function getFileType(url: string): string { if (typeof url != 'string' || url == '') { - return null + return '' } - var type = /\.[^\.]+$/.exec(url) //[".docx", index: 31, input: "http://192.168.2.243:7005/doc/2.docx"] - return type ? type[0].toLowerCase() : null + var type = /\.[^\.]+$/.exec(url) // [".docx", index: 31, input: "http://192.168.2.243:7005/doc/2.docx"] + return type ? type[0].toLowerCase() : '' } export default getFileType diff --git a/src/getIsAppVersionLastest.ts b/src/getIsAppVersionLastest.ts index a02cfceb..29e93977 100644 --- a/src/getIsAppVersionLastest.ts +++ b/src/getIsAppVersionLastest.ts @@ -3,31 +3,32 @@ import getAppVersion from './getAppVersion' /** * 版本号大小对比 * + * @example + * ```js + * // navigator.appVersion = "5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" + * getIsAppVersionLastest('Chrome', '90.0.4515.159'); // true; + * getIsAppVersionLastest('Chrome', '94.10.4515.159', navigator.appVersion); // false; + * ``` * @param appName - app名称 * @param compareVer - 必传 需要对比的版本号 * @param userAgent - ua,可不传,默认取navigator.appVersion * @return null/true/false */ -function getIsAppVersionLastest(appName: string, compareVer: string, userAgent: string) { - //console.log(getIsAppVersionLastest("Chrome","5.1")); +function getIsAppVersionLastest(appName: string, compareVer: string, userAgent?: string): boolean | null { userAgent = userAgent || navigator.appVersion - var basicVer = appName.indexOf('.') > 0 ? appName : getAppVersion(appName, false, userAgent) //兼容getIsAppVersionLastest("1.2.2","1.2.3")直接传入版本号的对比 - // var basicVer = "5.1."; + var basicVer = appName.indexOf('.') > 0 ? appName : getAppVersion(appName, false, userAgent) // 兼容getIsAppVersionLastest("1.2.2","1.2.3")直接传入版本号的对比 if (basicVer === null) { return null - } //不是指定客户端 + } // 不是指定客户端 if (basicVer === false) { return false - } //是指定客户端但是版本号未知 + } // 是指定客户端但是版本号未知 basicVer = basicVer + '.' compareVer = compareVer + '.' var bStr = parseFloat(basicVer) var cStr = parseFloat(compareVer) var bStrNext = parseFloat(basicVer.replace(bStr + '.', '')) || 0 var cStrNext = parseFloat(compareVer.replace(cStr + '.', '')) || 0 - // console.log(bStr + "-" + cStr) - // console.log(basicVer.replace(bStr + ".","") + "-" + compareVer.replace(cStr + ".","")) - // console.log(bStrNext + "-" + cStrNext) if (cStr > bStr) { return false } else if (cStr < bStr) { diff --git a/src/getNumber.ts b/src/getNumber.ts index 88d1851e..da0c8fd0 100644 --- a/src/getNumber.ts +++ b/src/getNumber.ts @@ -1,10 +1,15 @@ /** * 获取字符串中的数字 * + * @example + * ```js + * getNumber('Chrome123.33'); // '123.33'; + * getNumber('234test.88'); // '234.88'; + * ``` * @param string - 传入带数字的字符串 * @returns 返回纯数字字符串 */ -function getNumber(string: string) { +function getNumber(string: string): string { return string.replace(/[^0-9.]/gi, '') } diff --git a/src/getOsVersion.ts b/src/getOsVersion.ts index 672d9457..38bc5a86 100644 --- a/src/getOsVersion.ts +++ b/src/getOsVersion.ts @@ -3,12 +3,17 @@ import getAppVersion from './getAppVersion' /** * 获取手机系统版本 * + * @example + * ``` + * getAppVersion('iPhone') // '13.2.3' + * getAppVersion('iPhone', true) // 'iPhone/13.2.3' + * ``` * @param osName - 系统类型字符串Android、iPod、iWatch或iPhone * @param withosstr - 是否需要带上名称 * @param userAgent - ua,可不传,默认取navigator.appVersion * @return null/true/false */ -function getOsVersion(osName: string, withosstr: boolean, userAgent: string): string | boolean | null { +function getOsVersion(osName: string, withosstr?: boolean, userAgent?: string): string | boolean | null { userAgent = userAgent || navigator.appVersion var d = ['iPhone', 'iPad', 'iPod', 'iWatch', 'Mac', 'iMac', 'iOS'], name = osName,