Permalink
Browse files

initial checkin

  • Loading branch information...
0 parents commit fd9ba6715128e7de7e214b3b9ec7840f8dc77c80 @azer committed May 29, 2011
Showing with 217 additions and 0 deletions.
  1. +53 −0 README.markdown
  2. +61 −0 lib/environ.js
  3. +49 −0 test/browser.html
  4. +12 −0 test/node.js
  5. +42 −0 test/tests.js
@@ -0,0 +1,53 @@
+Cross platform environment detection library for JavaScript. Tested platforms; Node, Firefox 4, Chrome 12
+
+Install
+=======
+npm install environ
+
+Usage
+=====
+On Browsers:
+
+ <script src="environ.js"></script>
+
+ > environ.node()
+ false
+ > environ.dom()
+ true
+ > environ.v8()
+ true
+ > environ.webkit()
+ true
+
+On a CommonJS compliant platform:
+
+ > var environ = require('environ');
+ > environ.node()
+ true
+ > environ.dom()
+ false
+ > environ.v8()
+ true
+ > environ.webkit()
+ false
+
+Available Methods
+=================
+* dom
+* firefox
+* gecko
+* jsc
+* ie
+* linux
+* navigator
+* node
+* mobile
+* opera
+* safari
+* webkit
+* v8
+
+Testing
+=======
+node test/node.js
+w3 test/browser.html
@@ -0,0 +1,61 @@
+/*
+ * environ.js
+ * wtf public license
+ */
+var environ = (function(exports, undefined){
+
+ function dom(){
+ return typeof document != 'undefined' && document.documentElement != undefined;
+ }
+
+ function jsc(){
+ try {
+ throw new Error;
+ } catch(exc){
+ return typeof exc.sourceId == 'number';
+ }
+ }
+
+ function linux(){
+ return /linux/i.test(node() && process.platform || navigator.userAgent);
+ }
+
+ function navigator(){
+ return typeof window != 'undefined' && window.navigator != undefined;
+ }
+
+ function newRegexTest(pattern,str){
+ return function(){
+ return pattern.test(str || ( navigator() && window.navigator.userAgent ));
+ }
+ }
+
+ function node(){
+ return !navigator() && typeof process == 'object' && process.EventEmitter != undefined;
+ }
+
+ function v8(){
+ return node() || ( exports.webkit() && !jsc() );
+ }
+
+ return (exports = {
+ 'dom':dom,
+ 'firefox':newRegexTest(/firefox/i),
+ 'gecko':newRegexTest(/gecko/i),
+ 'jsc':jsc,
+ 'ie':newRegexTest(/msie/i),
+ 'linux':linux,
+ 'navigator':navigator,
+ 'node':node,
+ 'mobile':newRegexTest(/mobile/i),
+ 'opera':newRegexTest(/opera/i),
+ 'safari':newRegexTest(/safari/i),
+ 'webkit':newRegexTest(/webkit/i),
+ 'v8':v8
+ });
+
+})();
+
+if(typeof module != 'undefined' && module.exports){
+  module.exports = environ;
+}
@@ -0,0 +1,49 @@
+<html>
+ <head>
+ <meta http-equiv="content-type" content="text/html; charset=utf-8">
+ <style type="text/css" media="screen">
+ html, body { padding:0; margin:100px 0 0 0; background:#eee; }
+ h1, h2 { display:block; text-align:center; font:bold 72px sans-serif; }
+ h1 { color:#0a0; }
+ h2 { color:#f00; }
+ textarea { display:block; width:1000px; height:250px; margin:0 auto; }
+ </style>
+ <script src="../lib/environ.js"></script>
+ <script src="tests.js"></script>
+ <script>
+ var assert = {
+ 'equal':function(){
+ if(arguments[0]!=arguments[1]){
+ throw new Error('Assertion Error: '+arguments[0]+'=='+arguments[1]);
+ }
+ }
+ }
+
+ var output = '';
+
+ try {
+
+ for(var name in tests){
+ if(name.substring(0,5)=='test_'){
+ tests[name]();
+ window.console && console.log('Ran "'+name+'"');
+ }
+ }
+
+ output = '<h1>OK</h1>';
+ } catch(exc){
+ output = '<h2>FAIL</h2>'
+ + '<textarea>';
+ for(var key in exc){
+ output += key + '\n====\n' + exc[key] + '\n\n';
+ }
+ output += '</textarea>';
+ }
+
+ window.onload = function(){
+ document.body.innerHTML = output;
+ }
+
+ </script>
+ </head>
+</html>
@@ -0,0 +1,12 @@
+var tests = require('./tests'),
+ puts = require('sys').puts,
+ startTS = (new Date).getTime();
+
+for(var name in tests){
+ if(name.substring(0,5)=='test_'){
+ tests[name]();
+ console.log('Ran "'+name+'"');
+ }
+}
+
+puts('OK - '+( (new Date).getTime() - startTS )/1000+'s');
@@ -0,0 +1,42 @@
+if(typeof require!='undefined'){
+ environ = require('../lib/environ'),
+ assert = require('assert');
+}
+
+var node = typeof process != 'undefined' && process.EventEmitter != undefined,
+ nav = typeof window != 'undefined' && window.navigator != undefined;
+
+function test_browsers(){
+ assert.equal(environ.firefox(), nav && /firefox/i.test(navigator.userAgent));
+ assert.equal(environ.safari(), nav && /safari/i.test(navigator.userAgent));
+ assert.equal(environ.opera(), nav && /opera/i.test(navigator.userAgent));
+ assert.equal(environ.mobile(), nav && /mobile/i.test(navigator.userAgent));
+ assert.equal(environ.ie(), nav && /msie/i.test(navigator.userAgent));
+}
+
+function test_dom(){
+ assert.equal(environ.dom(), nav && window.document && window.document.documentElement != undefined);
+ assert.equal(environ.navigator(), nav);
+}
+
+function test_engines(){
+ var jsc = false;
+ try { err++ } catch(exc){ jsc = exc.sourceId != undefined }
+ assert.equal(environ.jsc(), jsc);
+ assert.equal(environ.v8(), node || ( environ.webkit() && !jsc ) );
+ assert.equal(environ.gecko(), nav && /gecko/i.test(navigator.userAgent));
+ assert.equal(environ.webkit(), nav && /webkit/i.test(navigator.userAgent));
+}
+
+function test_platform(){
+ assert.equal(environ.node(), node);
+}
+
+var tests = {
+ 'test_browsers':test_browsers,
+ 'test_dom':test_dom,
+ 'test_engines':test_engines,
+ 'test_platform':test_platform
+}
+
+typeof module != 'undefined' && ( module.exports = tests );

0 comments on commit fd9ba67

Please sign in to comment.