Last active
October 22, 2024 23:28
-
-
Save rhysburnie/498bfd98f24b7daf5fd5930c7f3c1b7b to your computer and use it in GitHub Desktop.
Detect node or browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// determine if in-browser or using node.js | |
// thruthy | |
var _nodejs = ( | |
typeof process !== 'undefined' && process.versions && process.versions.node); | |
if (_nodejs) { | |
_nodejs = { | |
version: process.versions.node | |
}; | |
} | |
// truthy | |
var _browser = !_nodejs && | |
(typeof window !== 'undefined' || typeof self !== 'undefined'); | |
if(_browser) { | |
_browser = { | |
window: false, | |
self: false, | |
$: false | |
}; | |
if(typeof global === 'undefined') { | |
if(typeof window !== 'undefined') { | |
global = window; | |
_browser.window = true; | |
} else if(typeof self !== 'undefined') { | |
global = self; | |
_browser.self = true; | |
} else if(typeof $ !== 'undefined') { | |
global = $; | |
_browser.$ = true; | |
} | |
} | |
} | |
if (_nodejs) { | |
module.export = { | |
nodejs: _nodejs, | |
browser: _browser | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
modified to code to return thruthy false or object with detail - untested