Skip to content

Commit 92cb8ba

Browse files
Onyxzenflexdinesh
authored andcommitted
Added isDeno
1 parent 545bf16 commit 92cb8ba

File tree

9 files changed

+37
-19
lines changed

9 files changed

+37
-19
lines changed

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
test
2-
browser-test
2+
browser-test

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"env": {
55
"mocha": true
66
},
7-
rules: {
7+
"rules": {
88
"comma-dangle": ["error", "only-multiline"]
99
}
1010
}

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ $ npm install --save browser-or-node
1717
ES6 style import
1818

1919
```js
20-
import { isBrowser, isNode, isWebWorker, isJsDom } from "browser-or-node";
20+
import { isBrowser, isNode, isWebWorker, isJsDom, isDeno } from "browser-or-node";
2121

2222
if (isBrowser) {
2323
// do browser only stuff
@@ -34,6 +34,10 @@ if (isWebWorker) {
3434
if (isJsDom) {
3535
// do jsdom only stuff
3636
}
37+
38+
if (isDeno) {
39+
// do deno only stuff
40+
}
3741
```
3842

3943
ES5 style import
@@ -56,8 +60,12 @@ if (jsEnv.isWebWorker) {
5660
if (jsEnv.isJsDom) {
5761
// do jsdom only stuff
5862
}
63+
64+
if (jsEnv.isDeno) {
65+
// do deno only stuff
66+
}
5967
```
6068

6169
## License
6270

63-
MIT © Dineshkumar Pandiyan
71+
MIT © Dineshkumar Pandiyan

browser-test/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ if (window.Worker) {
1515
const myWorker = new Worker("worker.js");
1616

1717
myWorker.onmessage = function(e) {
18-
let res = e.data;
18+
const _res = e.data;
19+
1920
console.log('Web worker reports:');
2021
console.log(' isBrowser: ' + e.data[0]);
2122
console.log(' isWebWorker: ' + e.data[1]);

browser-test/server.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ const app = express();
66
// app.use(express.static(path.join(__dirname, 'public')));
77
// app.use('/static', express.static(path.join(__dirname, 'public')));
88

9-
app.get('/', function(req, res) {
9+
app.get('/', function(_req, res) {
1010
res.sendFile(path.join(__dirname + '/index.html'));
1111
});
1212

13-
app.get('/worker.js', function(req, res) {
13+
app.get('/worker.js', function(_req, res) {
1414
res.sendFile(path.join(__dirname + '/worker.js'));
1515
});
1616

@@ -19,11 +19,11 @@ app.get('/worker.js', function(req, res) {
1919
res.sendFile(path.join(__dirname + '../../src/index.js'));
2020
});
2121
*/
22-
app.get('/index.js', function(req, res) {
22+
app.get('/index.js', function(_req, res) {
2323
res.sendFile(path.join(__dirname + '/index.js'));
2424
});
2525

26-
app.get('/browser-or-node.js', function(req, res) {
26+
app.get('/browser-or-node.js', function(_req, res) {
2727
res.sendFile(path.join(__dirname + '../../src/index.js'));
2828
});
2929

browser-test/worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ const isNode =
1818
process.versions != null &&
1919
process.versions.node != null;
2020

21-
onmessage = function(e) {
21+
onmessage = function(_e) {
2222
postMessage( [isBrowser, isWebWorker, isNode] );
2323
}

src/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ const isNode = typeof process !== 'undefined'
1717
* @see https://github.com/jsdom/jsdom/issues/1537
1818
*/
1919
/* eslint-disable no-undef */
20-
const isJsDom = () => (typeof window !== 'undefined' && window.name === 'nodejs')
20+
const isJsDom = (() => (typeof window !== 'undefined' && window.name === 'nodejs')
2121
|| navigator.userAgent.includes('Node.js')
22-
|| navigator.userAgent.includes('jsdom');
22+
|| navigator.userAgent.includes('jsdom'))();
23+
24+
const isDeno = typeof Deno !== 'undefined' && typeof Deno.core !== 'undefined';
2325

2426
export {
25-
isBrowser, isWebWorker, isNode, isJsDom
26-
};
27+
isBrowser, isWebWorker, isNode, isJsDom, isDeno
28+
};

test/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { assert } from 'chai';
2-
import { isBrowser, isWebBrowser, isNode } from '../src';
2+
import { isBrowser, isWebBrowser, isNode, isDeno, isJsDom } from '../src';
33

4-
console.log(isNode);
4+
console.log('Browser?', isBrowser);
5+
console.log('Node?', isNode);
6+
console.log('Web browser?', isWebBrowser);
7+
console.log('JS Dom?', isJsDom);
8+
console.log('Deno?', isDeno);
59

610
describe('Browser or Node.js', () => {
7-
811
it('should check node env', () => {
912
assert(isNode === true, 'isNode didn\'t work :(');
1013
});
@@ -17,5 +20,4 @@ describe('Browser or Node.js', () => {
1720
it('should check web worker env', () => {
1821
assert(isNode === true, 'isWebWorker didn\'t work :(');
1922
});
20-
21-
});
23+
});

test/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Special check for isDeno
2+
// Run with `deno run test/index.ts`
3+
import { isDeno } from '../src/index.js';
4+
5+
console.log(isDeno);

0 commit comments

Comments
 (0)