Skip to content

Commit

Permalink
完善typescript类型
Browse files Browse the repository at this point in the history
  • Loading branch information
saqqdy committed Sep 3, 2021
1 parent af088cf commit 2d3a4cd
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 30 deletions.
15 changes: 10 additions & 5 deletions es/getAppVersion.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -39,6 +44,6 @@ function getAppVersion(appName, withappstr, userAgent) {
return null;
}
}
}

module.exports = getAppVersion;
}

module.exports = getAppVersion;
6 changes: 1 addition & 5 deletions src/getAppVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/getCHSLength.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @param str - 字符串
* @returns 返回长度
*/
function getCHSLength(str: string) {
function getCHSLength(str: string): number {
return str.replace(/[^\x00-\xff]/g, '**').length
}

Expand Down
2 changes: 1 addition & 1 deletion src/getCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/getCookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions src/getDirParam.ts
Original file line number Diff line number Diff line change
@@ -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]?:\/\/[^\/]+/)
Expand Down
16 changes: 12 additions & 4 deletions src/getFileType.ts
Original file line number Diff line number Diff line change
@@ -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
19 changes: 10 additions & 9 deletions src/getIsAppVersionLastest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion src/getNumber.ts
Original file line number Diff line number Diff line change
@@ -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, '')
}

Expand Down
7 changes: 6 additions & 1 deletion src/getOsVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2d3a4cd

Please sign in to comment.