Skip to content

Commit 647999f

Browse files
committed
feat: add isMac
1 parent a82c0d8 commit 647999f

File tree

12 files changed

+165
-52
lines changed

12 files changed

+165
-52
lines changed

DOC.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7856,6 +7856,21 @@ isLeapYear(2000); // -> true
78567856
isLeapYear(2002); // -> false
78577857
```
78587858

7859+
## isMac
7860+
7861+
Check if platform is mac.
7862+
7863+
<details>
7864+
<summary>Type Definition</summary>
7865+
<pre>
7866+
<code class="language-typescript">const isMac: boolean;</code>
7867+
</pre>
7868+
</details>
7869+
7870+
```javascript
7871+
console.log(isMac); // -> true if running on mac
7872+
```
7873+
78597874
## isMap
78607875

78617876
Check if value is a Map object.

DOC_CN.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7849,6 +7849,21 @@ isLeapYear(2000); // -> true
78497849
isLeapYear(2002); // -> false
78507850
```
78517851

7852+
## isMac
7853+
7854+
检测是否运行在 mac 操作系统上。
7855+
7856+
<details>
7857+
<summary>类型定义</summary>
7858+
<pre>
7859+
<code class="language-typescript">const isMac: boolean;</code>
7860+
</pre>
7861+
</details>
7862+
7863+
```javascript
7864+
console.log(isMac); // -> true if running on mac
7865+
```
7866+
78527867
## isMap
78537868

78547869
检查值是否是 Map 对象。

cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@
213213
"isIp",
214214
"isJson",
215215
"isLeapYear",
216+
"isMac",
216217
"isMap",
217218
"isMatch",
218219
"isMiniProgram",

i18n/isMac.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## CN
2+
3+
检测是否运行在 mac 操作系统上。
4+

index.json

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,7 +2147,8 @@
21472147
},
21482148
"detectOs": {
21492149
"dependencies": [
2150-
"isBrowser"
2150+
"isBrowser",
2151+
"isNode"
21512152
],
21522153
"description": "Detect operating system using ua.",
21532154
"env": [
@@ -3674,6 +3675,21 @@
36743675
"browser"
36753676
]
36763677
},
3678+
"isMac": {
3679+
"dependencies": [
3680+
"detectOs"
3681+
],
3682+
"description": "Check if platform is mac.",
3683+
"env": [
3684+
"node",
3685+
"browser",
3686+
"miniprogram"
3687+
],
3688+
"test": [
3689+
"node",
3690+
"browser"
3691+
]
3692+
},
36773693
"isMap": {
36783694
"dependencies": [
36793695
"objToStr"
@@ -4150,13 +4166,18 @@
41504166
]
41514167
},
41524168
"isWindows": {
4153-
"dependencies": [],
4169+
"dependencies": [
4170+
"detectOs"
4171+
],
41544172
"description": "Check if platform is windows.",
41554173
"env": [
4156-
"node"
4174+
"node",
4175+
"browser",
4176+
"miniprogram"
41574177
],
41584178
"test": [
4159-
"node"
4179+
"node",
4180+
"browser"
41604181
]
41614182
},
41624183
"jsonClone": {

src/detectOs.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,45 @@
2222
* export declare function detectOs(ua?: string): string;
2323
*/
2424

25-
_('isBrowser');
25+
_('isBrowser isNode');
2626

2727
exports = function(ua) {
28-
ua = ua || (isBrowser ? navigator.userAgent : '');
29-
30-
ua = ua.toLowerCase();
31-
32-
if (detect('windows phone')) return 'windows phone';
33-
if (detect('win')) return 'windows';
34-
if (detect('android')) return 'android';
35-
if (detect('ipad') || detect('iphone') || detect('ipod')) return 'ios';
36-
if (detect('mac')) return 'os x';
37-
if (detect('linux')) return 'linux';
28+
if (!ua && isBrowser) {
29+
ua = navigator.userAgent;
30+
}
3831

3932
function detect(keyword) {
4033
return ua.indexOf(keyword) > -1;
4134
}
4235

36+
if (ua) {
37+
ua = ua.toLowerCase();
38+
39+
if (detect('windows phone')) return 'windows phone';
40+
if (detect('win')) return 'windows';
41+
if (detect('android')) return 'android';
42+
if (detect('ipad') || detect('iphone') || detect('ipod')) return 'ios';
43+
if (detect('mac')) return 'os x';
44+
if (detect('linux')) return 'linux';
45+
} else if (isNode) {
46+
const { platform, env } = process;
47+
48+
if (
49+
platform === 'win32' ||
50+
env.OSTYPE === 'cygwin' ||
51+
env.OSTYPE === 'msys'
52+
) {
53+
return 'windows';
54+
}
55+
56+
if (platform === 'darwin') {
57+
return 'os x';
58+
}
59+
60+
if (platform === 'linux') {
61+
return 'linux';
62+
}
63+
}
64+
4365
return 'unknown';
4466
};

src/isMac.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Check if platform is mac.
2+
*/
3+
4+
/* example
5+
* console.log(isMac); // -> true if running on mac
6+
*/
7+
8+
/* module
9+
* env: all
10+
*/
11+
12+
/* typescript
13+
* export declare const isMac: boolean;
14+
*/
15+
16+
_('detectOs');
17+
18+
exports = detectOs() === 'os x';

src/isWindows.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@
66
*/
77

88
/* module
9-
* env: node
9+
* env: all
1010
*/
1111

1212
/* typescript
1313
* export declare const isWindows: boolean;
1414
*/
1515

16-
exports = false;
16+
_('detectOs');
1717

18-
if (typeof process !== 'undefined') {
19-
exports =
20-
process.platform === 'win32' ||
21-
process.env.OSTYPE === 'cygwin' ||
22-
process.env.OSTYPE === 'msys';
23-
}
18+
exports = detectOs() === 'windows';

test/detectOs.js

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,44 @@
1-
tests([
2-
[
3-
'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
4-
'ios'
5-
],
6-
[
7-
'Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36',
8-
'android'
9-
],
10-
[
11-
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
12-
'windows'
13-
],
14-
[
15-
'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0',
16-
'linux'
17-
],
18-
[
19-
'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)',
20-
'windows phone'
21-
],
22-
[
23-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2',
24-
'os x'
25-
],
26-
['Nonsense', 'unknown']
27-
]);
1+
it('basic', () => {
2+
tests([
3+
[
4+
'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
5+
'ios'
6+
],
7+
[
8+
'Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36',
9+
'android'
10+
],
11+
[
12+
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
13+
'windows'
14+
],
15+
[
16+
'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0',
17+
'linux'
18+
],
19+
[
20+
'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)',
21+
'windows phone'
22+
],
23+
[
24+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2',
25+
'os x'
26+
],
27+
['Nonsense', 'unknown']
28+
]);
29+
});
30+
31+
if (util.isNode) {
32+
const { platform } = process;
33+
it('node', () => {
34+
if (platform === 'darwin') {
35+
expect(detectOs()).to.equal('os x');
36+
} else if (platform === 'linux') {
37+
expect(detectOs()).to.equal('linux');
38+
} else if (platform === 'win32') {
39+
expect(detectOs()).to.equal('windows');
40+
} else {
41+
expect(detectOs()).to.equal('unknown');
42+
}
43+
});
44+
}

test/isDarkMode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
expect(isDarkMode()).to.be.a.boolean;
1+
expect(isDarkMode()).to.be.a('boolean');

0 commit comments

Comments
 (0)