https://project.mdnd-it.cc/work_packages/94
This commit is contained in:
2025-08-23 04:25:28 +02:00
parent 725516ad6c
commit 19cfa031d0
25823 changed files with 1095587 additions and 2801760 deletions
+21
View File
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2018 Dineshkumar Pandiyan <flexdinesh@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+71
View File
@@ -0,0 +1,71 @@
# Browser or Node.js
[![Build Status](https://travis-ci.org/flexdinesh/browser-or-node.svg?branch=master)](https://travis-ci.org/flexdinesh/browser-or-node)
[![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
```
$ npm install --save browser-or-node
```
## Usage
ES6 style import
```js
import { isBrowser, isNode, isWebWorker, isJsDom, isDeno } from "browser-or-node";
if (isBrowser) {
// do browser only stuff
}
if (isNode) {
// do node.js only stuff
}
if (isWebWorker) {
// do web worker only stuff
}
if (isJsDom) {
// do jsdom only stuff
}
if (isDeno) {
// do deno only stuff
}
```
ES5 style import
```js
var jsEnv = require("browser-or-node");
if (jsEnv.isBrowser) {
// do browser only stuff
}
if (jsEnv.isNode) {
// do node.js only stuff
}
if (jsEnv.isWebWorker) {
// do web worker only stuff
}
if (jsEnv.isJsDom) {
// do jsdom only stuff
}
if (jsEnv.isDeno) {
// do deno only stuff
}
```
## License
MIT © Dineshkumar Pandiyan
+5
View File
@@ -0,0 +1,5 @@
export declare const isBrowser: boolean;
export declare const isWebWorker: boolean;
export declare const isNode: boolean;
export declare const isJsDom: boolean;
export declare const isDeno: boolean;
+27
View File
@@ -0,0 +1,27 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
var isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
var isWebWorker = (typeof self === "undefined" ? "undefined" : _typeof(self)) === "object" && self.constructor && self.constructor.name === "DedicatedWorkerGlobalScope";
/**
* @see https://github.com/jsdom/jsdom/releases/tag/12.0.0
* @see https://github.com/jsdom/jsdom/issues/1537
*/
var isJsDom = typeof window !== "undefined" && window.name === "nodejs" || typeof navigator !== "undefined" && (navigator.userAgent.includes("Node.js") || navigator.userAgent.includes("jsdom"));
var isDeno = typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined";
exports.isBrowser = isBrowser;
exports.isWebWorker = isWebWorker;
exports.isNode = isNode;
exports.isJsDom = isJsDom;
exports.isDeno = isDeno;
+61
View File
@@ -0,0 +1,61 @@
{
"name": "browser-or-node",
"version": "2.1.1",
"description": "Check where the code is running in the browser or node.js",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"scripts": {
"clean": "rimraf lib",
"test": "npm run cover",
"test:only": "cross-env BABEL_ENV=commonjs mocha --require babel-core/register --recursive",
"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",
"build": "cross-env BABEL_ENV=commonjs babel src --out-dir lib --copy-files",
"prepublish": "npm run clean && npm run lint && npm run test && npm run build"
},
"files": [
"lib",
"src"
],
"repository": {
"type": "git",
"url": "git+https://github.com/flexdinesh/browser-or-node.git"
},
"keywords": [
"npm",
"browser",
"node",
"is browser",
"is node",
"is browser node"
],
"author": "Dineshkumar Pandiyan <flexdinesh@gmail.com>",
"contributors": [
"Daniel Wang <daniel.liberated@gmail.com> (https://github.com/dan1wang/)"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/flexdinesh/browser-or-node/issues"
},
"homepage": "https://github.com/flexdinesh/browser-or-node#readme",
"devDependencies": {
"babel": "^6.23.0",
"babel-cli": "^6.26.0",
"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.2.0",
"cross-env": "^7.0.2",
"eslint": "^7.3.1",
"eslint-config-prettier": "^8.3.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": "^8.0.1",
"rimraf": "^3.0.2"
}
}
+5
View File
@@ -0,0 +1,5 @@
export declare const isBrowser: boolean;
export declare const isWebWorker: boolean;
export declare const isNode: boolean;
export declare const isJsDom: boolean;
export declare const isDeno: boolean;
+29
View File
@@ -0,0 +1,29 @@
const isBrowser =
typeof window !== "undefined" && typeof window.document !== "undefined";
const isNode =
typeof process !== "undefined" &&
process.versions != null &&
process.versions.node != null;
const isWebWorker =
typeof self === "object" &&
self.constructor &&
self.constructor.name === "DedicatedWorkerGlobalScope";
/**
* @see https://github.com/jsdom/jsdom/releases/tag/12.0.0
* @see https://github.com/jsdom/jsdom/issues/1537
*/
const isJsDom =
(typeof window !== "undefined" && window.name === "nodejs") ||
(typeof navigator !== "undefined" &&
(navigator.userAgent.includes("Node.js") ||
navigator.userAgent.includes("jsdom")));
const isDeno =
typeof Deno !== "undefined" &&
typeof Deno.version !== "undefined" &&
typeof Deno.version.deno !== "undefined";
export { isBrowser, isWebWorker, isNode, isJsDom, isDeno };