Skip to content

Commit

Permalink
perf: 修改平台验证函数使用方法(breaking)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ares-Chang committed Sep 20, 2024
1 parent b3395db commit 9564ecd
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 134 deletions.
17 changes: 9 additions & 8 deletions src/platform/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
*
* @returns 是否为浏览器环境
*/
export function isBrowser(): boolean {
return typeof window !== 'undefined' && typeof window.document !== 'undefined'
}
export const isBrowser: boolean = typeof window !== 'undefined' && typeof window.document !== 'undefined'

/**
* 判断是否为 Node 环境
* @returns 是否为 Node 环境
*/
export function isNode(): boolean {
// eslint-disable-next-line node/prefer-global/process
return typeof process !== 'undefined' && process.versions !== undefined && process.versions.node !== undefined
}
// eslint-disable-next-line node/prefer-global/process
export const isNode: boolean = typeof process !== 'undefined' && process.versions !== undefined && process.versions.node !== undefined

/**
* 浏览器的 UA
*/
export const ua: string = isBrowser ? window.navigator?.userAgent : ''

/**
* 获取浏览器的 UA
* @returns 浏览器的 UA
*/
export function getUA(): string {
return isBrowser() ? window.navigator?.userAgent : ''
return ua
}
66 changes: 17 additions & 49 deletions src/platform/brower.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,58 @@
import { getUA, isBrowser } from './base'
import { isBrowser, ua } from './base'

/**
* 判断是否为 Chrome 浏览器
* @returns 是否为 Chrome
*/
export function isChrome(): boolean {
return isBrowser() && /Chrome/i.test(getUA())
}
export const isChrome: boolean = isBrowser && /Chrome/i.test(ua)

/**
* 判断是否为 Firefox 浏览器
* @returns 是否为 Firefox 浏览器
*/
export function isFirefox(): boolean {
return isBrowser() && /Firefox/i.test(getUA())
}
export const isFirefox: boolean = isBrowser && /Firefox/i.test(ua)

/**
* 判断是否为 Safari 浏览器
* @returns 是否为 Safari 浏览器
*/
export function isSafari(): boolean {
return isBrowser() && /Safari/i.test(getUA()) && !isChrome()
}
export const isSafari: boolean = isBrowser && /Safari/i.test(ua) && !isChrome

/**
* 判断是否为 Edge 浏览器
* @returns 是否为 Edge 浏览器
*/
export function isEdge(): boolean {
return isBrowser() && /Edge/i.test(getUA())
}
export const isEdge: boolean = isBrowser && /Edge/i.test(ua)

/**
* 判断是否为 IE 浏览器
* @returns 是否为 IE 浏览器
*/
export function isIE(): boolean {
return isBrowser() && /Trident/i.test(getUA())
}
export const isIE: boolean = isBrowser && /Trident/i.test(ua)

/**
* 判断是否为 Opera 浏览器
* @returns 是否为 Opera 浏览器
*/
export function isOpera(): boolean {
return isBrowser() && /Opera/i.test(getUA())
}
export const isOpera: boolean = isBrowser && /Opera/i.test(ua)

/**
* 判断是否为 QQ 浏览器
* @returns 是否为 QQ 浏览器
*/
export function isQQ(): boolean {
return isBrowser() && /QQ/i.test(getUA())
}
export const isQQ: boolean = isBrowser && /QQ/i.test(ua)

/**
* 判断是否为微信浏览器
* @returns 是否为微信浏览器
*/
export function isWeChat(): boolean {
return isBrowser() && /MicroMessenger/i.test(getUA())
}
export const isWeChat: boolean = isBrowser && /MicroMessenger/i.test(ua)

/**
* 判断是否为企业微信浏览器
* @returns 是否为企业微信浏览器
*/
export function isWeWork(): boolean {
return isBrowser() && /wxwork/i.test(getUA())
}
export const isWeWork: boolean = isBrowser && /wxwork/i.test(ua)

/**
* 判断是否为微信小程序 webview 环境内
Expand All @@ -79,54 +61,40 @@ export function isWeWork(): boolean {
* @see {@link https://developers.weixin.qq.com/miniprogram/dev/component/web-view.html}
* @returns 是否为微信小程序 webview 环境内
*/
export function isWeApplet(): boolean {
return isBrowser() && (window as any)?.__wxjs_environment === 'miniprogram'
}
export const isWeApplet: boolean = isBrowser && (window as any)?.__wxjs_environment === 'miniprogram'

/**
* 判断是否为百度浏览器
* @returns 是否为百度浏览器
*/
export function isBaidu(): boolean {
return isBrowser() && /Baidu/i.test(getUA())
}
export const isBaidu: boolean = isBrowser && /Baidu/i.test(ua)

/**
* 判断是否为钉钉浏览器
* @returns 是否为钉钉浏览器
*/
export function isDingTalk(): boolean {
return isBrowser() && /DingTalk/i.test(getUA())
}
export const isDingTalk: boolean = isBrowser && /DingTalk/i.test(ua)

/**
* 判断是否为支付宝浏览器
* @returns 是否为支付宝浏览器
*/
export function isAliPay(): boolean {
return isBrowser() && /AliPay/i.test(getUA())
}
export const isAliPay: boolean = isBrowser && /AliPay/i.test(ua)

/**
* 判断是否为淘宝浏览器
* @returns 是否为淘宝浏览器
*/
export function isTaobao(): boolean {
return isBrowser() && /Taobao/i.test(getUA())
}
export const isTaobao: boolean = isBrowser && /Taobao/i.test(ua)

/**
* 判断是否为 360 浏览器
* @returns 是否为 360 浏览器
*/
export function is360(): boolean {
return isBrowser() && /360/.test(getUA())
}
export const is360: boolean = isBrowser && /360/.test(ua)

/**
* 判断是否为搜狗浏览器
* @returns 是否为搜狗浏览器
*/
export function isSogou(): boolean {
return isBrowser() && /Sogou/i.test(getUA())
}
export const isSogou: boolean = isBrowser && /Sogou/i.test(ua)
44 changes: 14 additions & 30 deletions src/platform/system.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,49 @@
import { getUA, isBrowser } from './base'

/**
* 判断是否为 PC
* @returns 是否为 PC
*/
export function isPC(): boolean {
return !isMobile() && !isPad()
}
import { isBrowser, ua } from './base'

/**
* 判断是否为移动端
* @returns 是否为移动端
*/
export function isMobile(): boolean {
return isBrowser() && /Android|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(getUA())
}
export const isMobile: boolean = isBrowser && /Android|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(ua)

/**
* 判断是否为平板
* @returns 是否为平板
*/
export function isPad(): boolean {
return isBrowser() && /iPad/i.test(getUA())
}
export const isPad: boolean = isBrowser && /iPad/i.test(ua)

/**
* 判断是否为 PC
* @returns 是否为 PC
*/
export const isPC: boolean = !isMobile && !isPad

/**
* 判断是否为 iOS
* @returns 是否为 iOS
*/
export function isIOS(): boolean {
return isBrowser() && /iPhone|iPad|iPod|iOS/i.test(getUA())
}
export const isIOS: boolean = isBrowser && /iPhone|iPad|iPod|iOS/i.test(ua)

/**
* 判断是否为 Android
* @returns 是否为 Android
*/
export function isAndroid(): boolean {
return isBrowser() && /Android/i.test(getUA())
}
export const isAndroid: boolean = isBrowser && /Android/i.test(ua)

/**
* 判断是否为 Windows 系统
* @returns 是否为 Windows 系统
*/
export function isWindows(): boolean {
return isBrowser() && /Windows/i.test(getUA())
}
export const isWindows: boolean = isBrowser && /Windows/i.test(ua)

/**
* 判断是否为 Mac 系统
* @returns 是否为 Mac 系统
*/
export function isMac(): boolean {
return isBrowser() && /Mac/i.test(getUA())
}
export const isMac: boolean = isBrowser && /Mac/i.test(ua)

/**
* 判断是否为 Linux 系统
* @returns 是否为 Linux 系统
*/
export function isLinux(): boolean {
return isBrowser() && /Linux/i.test(getUA())
}
export const isLinux: boolean = isBrowser && /Linux/i.test(ua)
22 changes: 14 additions & 8 deletions test/platform/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@ import { afterEach, describe, expect, it, vi } from 'vitest'

import { getUA, isBrowser, isNode } from '../../src/platform/base'

describe('环境判断和获取 UA 的函数测试', () => {
// @TODO: 不会测了,都改为了属性导出强依赖初次加载时的值,不会 mock 了。
describe.todo('环境判断和获取 UA 的函数测试', () => {
afterEach(() => {
vi.unstubAllGlobals()
})

it('isBrowser Function', () => {
expect(isBrowser()).toBe(false)
it('isBrowser Function', async () => {
expect(isBrowser).toBe(false)

vi.stubGlobal('window', {
document: {},
// vi.stubGlobal('window', {
// document: {},
// })
vi.doMock('../../src/platform/base', async () => {
const arg = await vi.importActual('../../src/platform/base')

return { ...arg, isBrowser: true }
})
expect(isBrowser()).toBe(true)
expect(isBrowser).toBe(true)
})

it('isNode Function', () => {
expect(isNode()).toBe(true)
expect(isNode).toBe(true)

vi.stubGlobal('process', undefined)
expect(isNode()).toBe(false)
expect(isNode).toBe(false)
})

it('getUA Function', () => {
Expand Down
Loading

0 comments on commit 9564ecd

Please sign in to comment.