Skip to content

Commit

Permalink
feat: add isMac
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed May 31, 2024
1 parent a82c0d8 commit 647999f
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 52 deletions.
15 changes: 15 additions & 0 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -7856,6 +7856,21 @@ isLeapYear(2000); // -> true
isLeapYear(2002); // -> false
```

## isMac

Check if platform is mac.

<details>
<summary>Type Definition</summary>
<pre>
<code class="language-typescript">const isMac: boolean;</code>
</pre>
</details>

```javascript
console.log(isMac); // -> true if running on mac
```

## isMap

Check if value is a Map object.
Expand Down
15 changes: 15 additions & 0 deletions DOC_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -7849,6 +7849,21 @@ isLeapYear(2000); // -> true
isLeapYear(2002); // -> false
```

## isMac

检测是否运行在 mac 操作系统上。

<details>
<summary>类型定义</summary>
<pre>
<code class="language-typescript">const isMac: boolean;</code>
</pre>
</details>

```javascript
console.log(isMac); // -> true if running on mac
```

## isMap

检查值是否是 Map 对象。
Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@
"isIp",
"isJson",
"isLeapYear",
"isMac",
"isMap",
"isMatch",
"isMiniProgram",
Expand Down
4 changes: 4 additions & 0 deletions i18n/isMac.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## CN

检测是否运行在 mac 操作系统上。

29 changes: 25 additions & 4 deletions index.json
Original file line number Diff line number Diff line change
Expand Up @@ -2147,7 +2147,8 @@
},
"detectOs": {
"dependencies": [
"isBrowser"
"isBrowser",
"isNode"
],
"description": "Detect operating system using ua.",
"env": [
Expand Down Expand Up @@ -3674,6 +3675,21 @@
"browser"
]
},
"isMac": {
"dependencies": [
"detectOs"
],
"description": "Check if platform is mac.",
"env": [
"node",
"browser",
"miniprogram"
],
"test": [
"node",
"browser"
]
},
"isMap": {
"dependencies": [
"objToStr"
Expand Down Expand Up @@ -4150,13 +4166,18 @@
]
},
"isWindows": {
"dependencies": [],
"dependencies": [
"detectOs"
],
"description": "Check if platform is windows.",
"env": [
"node"
"node",
"browser",
"miniprogram"
],
"test": [
"node"
"node",
"browser"
]
},
"jsonClone": {
Expand Down
44 changes: 33 additions & 11 deletions src/detectOs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,45 @@
* export declare function detectOs(ua?: string): string;
*/

_('isBrowser');
_('isBrowser isNode');

exports = function(ua) {
ua = ua || (isBrowser ? navigator.userAgent : '');

ua = ua.toLowerCase();

if (detect('windows phone')) return 'windows phone';
if (detect('win')) return 'windows';
if (detect('android')) return 'android';
if (detect('ipad') || detect('iphone') || detect('ipod')) return 'ios';
if (detect('mac')) return 'os x';
if (detect('linux')) return 'linux';
if (!ua && isBrowser) {
ua = navigator.userAgent;
}

function detect(keyword) {
return ua.indexOf(keyword) > -1;
}

if (ua) {
ua = ua.toLowerCase();

if (detect('windows phone')) return 'windows phone';
if (detect('win')) return 'windows';
if (detect('android')) return 'android';
if (detect('ipad') || detect('iphone') || detect('ipod')) return 'ios';
if (detect('mac')) return 'os x';
if (detect('linux')) return 'linux';
} else if (isNode) {
const { platform, env } = process;

if (
platform === 'win32' ||
env.OSTYPE === 'cygwin' ||
env.OSTYPE === 'msys'
) {
return 'windows';
}

if (platform === 'darwin') {
return 'os x';
}

if (platform === 'linux') {
return 'linux';
}
}

return 'unknown';
};
18 changes: 18 additions & 0 deletions src/isMac.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Check if platform is mac.
*/

/* example
* console.log(isMac); // -> true if running on mac
*/

/* module
* env: all
*/

/* typescript
* export declare const isMac: boolean;
*/

_('detectOs');

exports = detectOs() === 'os x';
11 changes: 3 additions & 8 deletions src/isWindows.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@
*/

/* module
* env: node
* env: all
*/

/* typescript
* export declare const isWindows: boolean;
*/

exports = false;
_('detectOs');

if (typeof process !== 'undefined') {
exports =
process.platform === 'win32' ||
process.env.OSTYPE === 'cygwin' ||
process.env.OSTYPE === 'msys';
}
exports = detectOs() === 'windows';
71 changes: 44 additions & 27 deletions test/detectOs.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
tests([
[
'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',
'ios'
],
[
'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',
'android'
],
[
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'windows'
],
[
'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0',
'linux'
],
[
'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)',
'windows phone'
],
[
'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',
'os x'
],
['Nonsense', 'unknown']
]);
it('basic', () => {
tests([
[
'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',
'ios'
],
[
'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',
'android'
],
[
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'windows'
],
[
'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0',
'linux'
],
[
'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)',
'windows phone'
],
[
'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',
'os x'
],
['Nonsense', 'unknown']
]);
});

if (util.isNode) {
const { platform } = process;
it('node', () => {
if (platform === 'darwin') {
expect(detectOs()).to.equal('os x');
} else if (platform === 'linux') {
expect(detectOs()).to.equal('linux');
} else if (platform === 'win32') {
expect(detectOs()).to.equal('windows');
} else {
expect(detectOs()).to.equal('unknown');
}
});
}
2 changes: 1 addition & 1 deletion test/isDarkMode.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
expect(isDarkMode()).to.be.a.boolean;
expect(isDarkMode()).to.be.a('boolean');
5 changes: 5 additions & 0 deletions test/isMac.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if (typeof process !== 'undefined' && process.platform === 'darwin') {
expect(isMac).to.be.true;
} else {
expect(isMac).to.be.a('boolean');
}
2 changes: 1 addition & 1 deletion test/isWindows.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
if (typeof process !== 'undefined' && process.platform === 'win32') {
expect(isWindows).to.be.true;
} else {
expect(isWindows).to.be.false;
expect(isWindows).to.be.a('boolean');
}

0 comments on commit 647999f

Please sign in to comment.