package/.babelrc000644 0000000330 3560116604 010647 0ustar00000000 000000 { "env": { "cjs": { "presets": [["@babel/preset-typescript"]], "plugins": ["@babel/plugin-transform-modules-commonjs"] }, "es": { "presets": [["@babel/preset-typescript"]] } } } package/.editorconfig000644 0000000617 3560116604 011741 0ustar00000000 000000 # top-most EditorConfig file root = true # Unix-style newlines with a newline ending every file [*] end_of_line = lf insert_final_newline = true # Matches multiple files with brace expansion notation # Set default charset [*.{js,ts,mjs,cjs}] charset = utf-8 indent_style = space indent_size = 2 # Matches the exact files either package.json [{package.json}] indent_style = space indent_size = 2 package/.prettierrc000644 0000000055 3560116604 011444 0ustar00000000 000000 { "semi": true, "trailingComma": "all" } package/LICENSE000644 0000002064 3560116604 010267 0ustar00000000 000000 MIT License Copyright (c) 2016-2019 Jared Anderson 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. package/.eslintrc.js000644 0000000451 3560116604 011517 0ustar00000000 000000 module.exports = { root: true, ignorePatterns: ["dist/**/*", ".eslintrc.js"], parser: "@typescript-eslint/parser", plugins: ["@typescript-eslint", "prettier"], extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"], rules: { "prettier/prettier": "error", }, }; package/package.json000644 0000003571 3560116604 011554 0ustar00000000 000000 { "name": "is-in-browser", "version": "2.0.0", "description": "Simple check to see if current app is running in browser", "main": "dist/index.cjs", "module": "dist/index.mjs", "types": "dist/index.d.ts", "exports": { ".": { "require": "./dist/index.cjs", "import": "./dist/index.mjs" }, "./package.json": "./package.json" }, "scripts": { "dist.cjs": "BABEL_ENV=cjs babel -x \".ts\" src/index.ts -o dist/index.cjs", "dist.es": "BABEL_ENV=es babel -x \".ts\" src/index.ts -o dist/index.mjs", "dist.declarations": "tsc --emitDeclarationOnly src/index.ts --declaration --outDir dist", "dist": "concurrently \"npm:dist.*\"", "test.jest": "BABEL_ENV=cjs jest", "test.node": "BABEL_ENV=cjs babel-node -x \".ts\" src/node.check.ts", "test": "concurrently \"npm:test.*\"", "lint": "eslint src --ext .ts", "format": "prettier -w src", "prepublishOnly": "concurrently npm:test npm:dist" }, "keywords": [], "browserslist": [ "defaults" ], "license": "MIT", "devDependencies": { "@babel/cli": "^7.12.13", "@babel/core": "^7.12.13", "@babel/node": "^7.12.13", "@babel/plugin-transform-modules-commonjs": "^7.12.13", "@babel/preset-typescript": "^7.12.13", "@types/jest": "^26.0.20", "@typescript-eslint/eslint-plugin": "^4.14.2", "@typescript-eslint/parser": "^4.14.2", "babel-eslint": "^4.1.6", "concurrently": "^5.3.0", "eslint": "^7.19.0", "eslint-plugin-prettier": "^3.3.1", "jest": "^26.6.3", "prettier": "^2.2.1", "typescript": "^4.1.3" }, "directories": { "test": "test" }, "repository": { "type": "git", "url": "git+https://github.com/tuxsudo/is-in-browser.git" }, "author": "Jared Anderson", "bugs": { "url": "https://github.com/tuxsudo/is-in-browser/issues" }, "homepage": "https://github.com/tuxsudo/is-in-browser#readme" } package/readme.md000644 0000000750 3560116604 011041 0ustar00000000 000000 # Is In Browser? ```js import isBrowser from "is-in-browser"; if (isBrowser) { //... } ``` More thoroughly: ```js import { isJsDom, isNode, isBrowser } from "is-in-browser"; if (isBrowser) { // you're in the browser // jsdom considered in browser } if (isJsDom) { // you're in the JSDom } if (isNode) { // you're in the Node } ``` ## CommonJS For those not using Babel / ES6 Modules ```js var isBrowser = require('is-in-browser').default; if(isBrowser) { //... } ``` package/src/index.ts000644 0000000565 3560116604 011534 0ustar00000000 000000 export const isJsDom = typeof navigator === "object" && navigator.userAgent && navigator.userAgent.includes("jsdom"); export const isNode = typeof process === "object" && !!process.versions && !!process.versions.node; export const isBrowser = typeof window === "object" && typeof document === "object" && document.nodeType === 9; export default isBrowser; package/src/node.check.ts000644 0000000445 3560116604 012423 0ustar00000000 000000 import { isBrowser, isNode, isJsDom } from "./index"; console.log(isBrowser ? "❌" : "✅", "browser:", isBrowser); console.log(isNode ? "✅" : "❌", "node:", isNode); console.log(isBrowser ? "❌" : "✅", "js-dom:", isJsDom); if (isBrowser || !isNode || isJsDom) { process.exit(1); } package/src/test.ts000644 0000000575 3560116604 011405 0ustar00000000 000000 import isInBrowser, { isBrowser, isJsDom, isNode } from "."; test("isInBrowser", () => { // jest runs jsdom, so true expect(isBrowser).toBe(true); expect(isInBrowser).toBe(true); // jest runs jsdom expect(isJsDom).toBe(true); expect(isBrowser && !isJsDom).toBe(false); // jest runs on Node, so true expect(isNode).toBe(true); // jest runs JSDOm, so true });