Skip to content

Commit

Permalink
feat: new function - isWeixin
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatAuk committed Mar 6, 2023
1 parent 5c81139 commit 2e38e97
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pnpm add @utopia-utils/dom

* waitForSelector: 等待指定的选择器匹配的元素出现在页面中,如果调用此方法时已经有匹配的元素,那么此方法立即返回。 如果指定的选择器在超时时间后扔不出现,返回 `null`[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/waitForSelector.ts)
* panzoom: 为指定的元素添加拖拽缩放功能。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/panzoom/core.ts)
* isWeixin: 判断是否是微信浏览器。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/isWeixin.ts)
### 杂项
* [defineDictionary](#defineDictionary): 定义业务字典。 **typesafe** [source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/defineDictionary.ts)
* ~~[createEnumFromOptions](#createEnumFromOptions): 通过 `options` 自动生成对应的 `enum`, 后期只需要维护 `options`**typesafe**[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/createEnumFromOptions.ts)~~
Expand Down
3 changes: 2 additions & 1 deletion packages/dom/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './waitForSelector'
export * from './panzoom'
export * from './isWeixin'
export * from './waitForSelector'
17 changes: 17 additions & 0 deletions packages/dom/src/isWeixin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @vitest-environment happy-dom
*/
import { isWeixin } from './isWeixin'

describe('isWeixin', () => {
afterEach(() => {
vi.resetAllMocks()
})
it('should return false if ua does not contain MicroMessenger', () => {
expect(isWeixin()).toBe(false)
})
it('should return true if ua contains MicroMessenger', () => {
vi.spyOn(navigator, 'userAgent', 'get').mockReturnValueOnce('MicroMessenger')
expect(isWeixin()).toBe(true)
})
})
8 changes: 8 additions & 0 deletions packages/dom/src/isWeixin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Check if the current environment is Weixin. (微信)
* @returns A boolean value.
*/
export function isWeixin() {
const ua = navigator.userAgent.toLowerCase()
return /MicroMessenger/i.test(ua)
}

0 comments on commit 2e38e97

Please sign in to comment.