From 2e38e97c772ffd54733d09e0b20e403a8d23ec2d Mon Sep 17 00:00:00 2001 From: Utopia Date: Mon, 6 Mar 2023 23:00:10 +0800 Subject: [PATCH] feat: new function - isWeixin --- README.md | 1 + packages/dom/src/index.ts | 3 ++- packages/dom/src/isWeixin.test.ts | 17 +++++++++++++++++ packages/dom/src/isWeixin.ts | 8 ++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 packages/dom/src/isWeixin.test.ts create mode 100644 packages/dom/src/isWeixin.ts diff --git a/README.md b/README.md index f9b8b8a..4066b65 100644 --- a/README.md +++ b/README.md @@ -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)~~ diff --git a/packages/dom/src/index.ts b/packages/dom/src/index.ts index 2f63faa..70164e6 100644 --- a/packages/dom/src/index.ts +++ b/packages/dom/src/index.ts @@ -1,2 +1,3 @@ -export * from './waitForSelector' export * from './panzoom' +export * from './isWeixin' +export * from './waitForSelector' diff --git a/packages/dom/src/isWeixin.test.ts b/packages/dom/src/isWeixin.test.ts new file mode 100644 index 0000000..19ab82e --- /dev/null +++ b/packages/dom/src/isWeixin.test.ts @@ -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) + }) +}) diff --git a/packages/dom/src/isWeixin.ts b/packages/dom/src/isWeixin.ts new file mode 100644 index 0000000..835eb57 --- /dev/null +++ b/packages/dom/src/isWeixin.ts @@ -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) +}