Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: flexdinesh/browser-or-node
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: dd3970eabf095f22d710de485961e21e2c1c92fa
Choose a base ref
...
head repository: flexdinesh/browser-or-node
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 545bf1649148fff3006127e0c8fea3d0bf488f1a
Choose a head ref
  • 5 commits
  • 4 files changed
  • 2 contributors

Commits on Jun 25, 2020

  1. Copy the full SHA
    666ba71 View commit details
  2. Fix lint errors

    flexdinesh committed Jun 25, 2020
    Copy the full SHA
    b39d40a View commit details
  3. Copy the full SHA
    5ae3776 View commit details
  4. 1.3.0

    flexdinesh committed Jun 25, 2020
    Copy the full SHA
    fe87a05 View commit details
  5. Upgrade deps

    flexdinesh committed Jun 25, 2020
    Copy the full SHA
    545bf16 View commit details
Showing with 1,351 additions and 700 deletions.
  1. +19 −3 README.md
  2. +13 −13 package.json
  3. +16 −11 src/index.js
  4. +1,303 −673 yarn.lock
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@
[![npm version](https://badge.fury.io/js/browser-or-node.svg)](https://www.npmjs.com/package/browser-or-node)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)


Check whether the code is running in the browser or node.js runtime.

## Install
@@ -16,8 +15,9 @@ $ npm install --save browser-or-node
## Usage

ES6 style import

```js
import { isBrowser, isNode } from 'browser-or-node';
import { isBrowser, isNode, isWebWorker, isJsDom } from "browser-or-node";

if (isBrowser) {
// do browser only stuff
@@ -27,10 +27,19 @@ if (isNode) {
// do node.js only stuff
}

if (isWebWorker) {
// do web worker only stuff
}

if (isJsDom) {
// do jsdom only stuff
}
```

ES5 style import

```js
var jsEnv = require('browser-or-node');
var jsEnv = require("browser-or-node");

if (jsEnv.isBrowser) {
// do browser only stuff
@@ -40,6 +49,13 @@ if (jsEnv.isNode) {
// do node.js only stuff
}

if (jsEnv.isWebWorker) {
// do web worker only stuff
}

if (jsEnv.isJsDom) {
// do jsdom only stuff
}
```

## License
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "browser-or-node",
"version": "1.2.1",
"version": "1.3.0",
"description": "Check where the code is running in the browser or node.js",
"main": "./lib/index.js",
"scripts": {
@@ -10,7 +10,7 @@
"test:watch": "npm test -- --watch",
"test:examples": "node examples/",
"cover": "cross-env BABEL_ENV=commonjs istanbul cover node_modules/mocha/bin/_mocha -- --require babel-core/register --recursive",
"lint": "eslint src test",
"lint": "eslint src",
"build": "cross-env BABEL_ENV=commonjs babel src --out-dir lib",
"prepublish": "npm run clean && npm run lint && npm run test && npm run build"
},
@@ -42,19 +42,19 @@
"devDependencies": {
"babel": "^6.23.0",
"babel-cli": "^6.26.0",
"babel-eslint": "^8.2.1",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-eslint": "^10.1.0",
"babel-plugin-add-module-exports": "^1.0.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"chai": "^4.1.2",
"cross-env": "^5.1.3",
"eslint": "^4.16.0",
"eslint-config-airbnb": "^16.1.0",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-jsx-a11y": "^6.0.2",
"eslint-plugin-react": "^7.4.0",
"chai": "^4.2.0",
"cross-env": "^7.0.2",
"eslint": "^7.3.1",
"eslint-config-airbnb": "^18.2.0",
"eslint-plugin-import": "^2.21.2",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-react": "^7.20.0",
"istanbul": "^1.0.0-alpha",
"mocha": "^5.0.0",
"rimraf": "^2.6.2"
"mocha": "^8.0.1",
"rimraf": "^3.0.2"
}
}
27 changes: 16 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -3,19 +3,24 @@
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';

/* eslint-disable no-restricted-globals */
const isWebWorker =
typeof self === 'object' &&
self.constructor &&
self.constructor.name === 'DedicatedWorkerGlobalScope';
const isWebWorker = typeof self === 'object'
&& self.constructor
&& self.constructor.name === 'DedicatedWorkerGlobalScope';
/* eslint-enable no-restricted-globals */

const isNode =
typeof process !== 'undefined' &&
process.versions != null &&
process.versions.node != null;
const isNode = typeof process !== 'undefined'
&& process.versions != null
&& process.versions.node != null;

/**
* @see https://github.com/jsdom/jsdom/releases/tag/12.0.0
* @see https://github.com/jsdom/jsdom/issues/1537
*/
/* eslint-disable no-undef */
const isJsDom = () => (typeof window !== 'undefined' && window.name === 'nodejs')
|| navigator.userAgent.includes('Node.js')
|| navigator.userAgent.includes('jsdom');

export {
isBrowser,
isWebWorker,
isNode
isBrowser, isWebWorker, isNode, isJsDom
};
1,976 changes: 1,303 additions & 673 deletions yarn.lock

Large diffs are not rendered by default.