now using classes, remove global funcionality to prevent bad habits

This commit is contained in:
Philipp Kunz 2015-11-30 15:04:04 +01:00
parent d2de8231cc
commit f5e48e4fd3
11 changed files with 175 additions and 69 deletions

View File

@ -1,20 +1,60 @@
/// <reference path="index.ts" />
var Environment = (function () {
function Environment(runtimeEnvArg, userAgentArg) {
if (userAgentArg === void 0) { userAgentArg = "undefined"; }
this.runtimeEnv = runtimeEnvArg;
this.userAgent = userAgentArg;
if (runtimeEnvArg == "node") {
this.isBrowser = false;
this.isNode = true;
this.nodeVersion = process.version;
}
else {
this.isBrowser = true;
this.isNode = true;
this.nodeVersion = "undefined";
}
}
;
return Environment;
})();
/// <reference path="typings/tsd.d.ts" />
/// <reference path="classes.ts" />
var beautylog = require("beautylog")("os");
var addToGlobal = function (objectArg, paramName) {
global[paramName] = objectArg;
};
var smartenv = {}; //create smartenv object
smartenv.items = {}; // create the items object to store items to.
smartenv.makeGlobal = function () {
addToGlobal(smartenv, "smartenv"); //add object smartenv as global["smartenv"]
/* ----------------------------------------- *
* ----- Environment ----------------------- *
* ----------------------------------------- */
var environment;
var setEnvironment = function () {
var localRunTimeEnv = "undefined";
var localUserAgent = "undefined";
if (typeof window !== 'undefined') {
localRunTimeEnv = 'browser';
localUserAgent = navigator.userAgent;
}
else if (typeof process !== 'undefined') {
localRunTimeEnv = 'node';
}
environment = new Environment(localRunTimeEnv, localUserAgent);
};
smartenv.info = {};
smartenv.info.node = {};
smartenv.info.node.version = process.version;
smartenv.info.print = function () {
var pck = require("./package.json");
beautylog.log("node version is " + smartenv.info.node.version + " and smartenv version is " + pck.version);
beautylog.log("the smartenv module currently holds the following properties:");
setEnvironment();
smartenv.getEnv = function () {
return environment;
};
/* ----------------------------------------- *
* ----- Info vars ------------------------- *
* ----------------------------------------- */
smartenv.printEnv = function () {
if (environment.isNode) {
var smartenvVersion = require("./package.json").version;
beautylog.log("node version is " + environment.nodeVersion + " and smartenv version is " + smartenvVersion);
}
else {
beautylog.log("browser is " + environment.userAgent);
}
beautylog.log("the smartenv registration store currently holds the following properties:");
console.log(Object.getOwnPropertyNames(smartenv.items).sort());
};
smartenv.register = function (objectArg, paramName) {
@ -28,8 +68,4 @@ smartenv.register = function (objectArg, paramName) {
smartenv.get = function (keyName) {
return smartenv.items[keyName];
};
smartenv.exportEnv = function () {
};
smartenv.importEnv = function () {
};
module.exports = smartenv;

View File

@ -2,14 +2,13 @@
var smartenv = require("./index.js");
var beautylog = require("beautylog")("os");
beautylog.info("Now testing the smartenv module");
smartenv.info.print();
smartenv.printEnv();
beautylog.info("Now testing the smartenv module");
smartenv.register({ key1: "Peter" }, "docit");
smartenv.info.print();
smartenv.printEnv();
beautylog.log(smartenv.get("docit").key1);
beautylog.log(smartenv.get("docit").key1);
smartenv.makeGlobal();
beautylog.log(global.smartenv.get("docit").key1);
var key2 = "hello";
smartenv.get("docit").key2 = key2;
beautylog.log(global.smartenv.get("docit").key2);
beautylog.log(smartenv.get("docit").key2);
beautylog.success("Success!");

21
ts/classes.js Normal file
View File

@ -0,0 +1,21 @@
/// <reference path="index.ts" />
var Environment = (function () {
function Environment(runtimeEnvArg, userAgentArg) {
if (userAgentArg === void 0) { userAgentArg = "undefined"; }
this.runtimeEnv = runtimeEnvArg;
this.userAgent = userAgentArg;
if (runtimeEnvArg == "node") {
this.isBrowser = false;
this.isNode = true;
this.nodeVersion = process.version;
}
else {
this.isBrowser = true;
this.isNode = true;
this.nodeVersion = "undefined";
}
}
;
return Environment;
})();
//# sourceMappingURL=classes.js.map

1
ts/classes.js.map Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"classes.js","sourceRoot":"","sources":["classes.ts"],"names":["Environment","Environment.constructor"],"mappings":"AAAA,iCAAiC;AACjC;IAMIA,qBAAYA,aAAaA,EAACA,YAA0BA;QAA1BC,4BAA0BA,GAA1BA,0BAA0BA;QAChDA,IAAIA,CAACA,UAAUA,GAAGA,aAAaA,CAACA;QAChCA,IAAIA,CAACA,SAASA,GAAGA,YAAYA,CAACA;QAC9BA,EAAEA,CAAAA,CAACA,aAAaA,IAAIA,MAAMA,CAACA,CAAAA,CAACA;YACxBA,IAAIA,CAACA,SAASA,GAAGA,KAAKA,CAACA;YACvBA,IAAIA,CAACA,MAAMA,GAAGA,IAAIA,CAACA;YACnBA,IAAIA,CAACA,WAAWA,GAAGA,OAAOA,CAACA,OAAOA,CAACA;QACvCA,CAACA;QAACA,IAAIA,CAACA,CAACA;YACJA,IAAIA,CAACA,SAASA,GAAGA,IAAIA,CAACA;YACtBA,IAAIA,CAACA,MAAMA,GAAGA,IAAIA,CAACA;YACnBA,IAAIA,CAACA,WAAWA,GAAGA,WAAWA,CAACA;QACnCA,CAACA;IACLA,CAACA;;IACLD,kBAACA;AAADA,CAACA,AAnBD,IAmBC"}

21
ts/classes.ts Normal file
View File

@ -0,0 +1,21 @@
/// <reference path="index.ts" />
class Environment {
public runtimeEnv:string;
public userAgent:string;
public nodeVersion;
public isBrowser:boolean;
public isNode:boolean;
constructor(runtimeEnvArg,userAgentArg = "undefined") {
this.runtimeEnv = runtimeEnvArg;
this.userAgent = userAgentArg;
if(runtimeEnvArg == "node"){
this.isBrowser = false;
this.isNode = true;
this.nodeVersion = process.version;
} else {
this.isBrowser = true;
this.isNode = true;
this.nodeVersion = "undefined";
}
};
}

View File

@ -1,20 +1,40 @@
/// <reference path="typings/tsd.d.ts" />
/// <reference path="classes.ts" />
var beautylog = require("beautylog")("os");
var addToGlobal = function (objectArg, paramName) {
global[paramName] = objectArg;
};
var smartenv = {}; //create smartenv object
smartenv.items = {}; // create the items object to store items to.
smartenv.makeGlobal = function () {
addToGlobal(smartenv, "smartenv"); //add object smartenv as global["smartenv"]
/* ----------------------------------------- *
* ----- Environment ----------------------- *
* ----------------------------------------- */
var environment;
var setEnvironment = function () {
var localRunTimeEnv = "undefined";
var localUserAgent = "undefined";
if (typeof window !== 'undefined') {
localRunTimeEnv = 'browser';
localUserAgent = navigator.userAgent;
}
else if (typeof process !== 'undefined') {
localRunTimeEnv = 'node';
}
environment = new Environment(localRunTimeEnv, localUserAgent);
};
smartenv.info = {};
smartenv.info.node = {};
smartenv.info.node.version = process.version;
smartenv.info.print = function () {
var pck = require("./package.json");
beautylog.log("node version is " + smartenv.info.node.version + " and smartenv version is " + pck.version);
beautylog.log("the smartenv module currently holds the following properties:");
setEnvironment();
smartenv.getEnv = function () {
return environment;
};
/* ----------------------------------------- *
* ----- Info vars ------------------------- *
* ----------------------------------------- */
smartenv.printEnv = function () {
if (environment.isNode) {
var smartenvVersion = require("./package.json").version;
beautylog.log("node version is " + environment.nodeVersion + " and smartenv version is " + smartenvVersion);
}
else {
beautylog.log("browser is " + environment.userAgent);
}
beautylog.log("the smartenv registration store currently holds the following properties:");
console.log(Object.getOwnPropertyNames(smartenv.items).sort());
};
smartenv.register = function (objectArg, paramName) {
@ -28,9 +48,5 @@ smartenv.register = function (objectArg, paramName) {
smartenv.get = function (keyName) {
return smartenv.items[keyName];
};
smartenv.exportEnv = function () {
};
smartenv.importEnv = function () {
};
module.exports = smartenv;
//# sourceMappingURL=index.js.map

View File

@ -1 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,yCAAyC;AACzC,IAAI,SAAS,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,CAAC;AAC3C,IAAI,WAAW,GAAG,UAAS,SAAS,EAAC,SAAS;IAC1C,MAAM,CAAC,SAAS,CAAC,GAAG,SAAS,CAAC;AAClC,CAAC,CAAC;AACF,IAAI,QAAQ,GAAO,EAAE,CAAC,CAAC,wBAAwB;AAC/C,QAAQ,CAAC,KAAK,GAAG,EAAE,CAAC,CAAC,6CAA6C;AAClE,QAAQ,CAAC,UAAU,GAAG;IAClB,WAAW,CAAC,QAAQ,EAAC,UAAU,CAAC,CAAC,CAAA,2CAA2C;AAChF,CAAC,CAAC;AAGF,QAAQ,CAAC,IAAI,GAAG,EAAE,CAAC;AACnB,QAAQ,CAAC,IAAI,CAAC,IAAI,GAAG,EA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Q,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;AACnE,CAAC,CAAC;AAEF,QAAQ,CAAC,QAAQ,GAAG,UAAS,SAAS,EAAC,SAAuB;IAAvB,yBAAuB,GAAvB,uBAAuB;IAC1D,EAAE,CAAC,CAAC,SAAS,IAAI,WAAW,CAAC,CAAA,CAAC;QAC1B,SAAS,CAAC,KAAK,CAAC,wBAAwB,CAAC,CAAC;QAC1C,MAAM,CAAC;IACX,CAAC;IACD,QAAQ,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,SAAS,CAAC;AAC1C,CAAC,CAAC;AACF,QAAQ,CAAC,GAAG,GAAG,UAAS,OAAO;IAC3B,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;AACnC,CAAC,CAAC;AAEF,MAAM,CAAC,OAAO,GAAG,QAAQ,CAAC"}

View File

@ -1,22 +1,44 @@
/// <reference path="typings/tsd.d.ts" />
/// <reference path="classes.ts" />
var beautylog = require("beautylog")("os");
var addToGlobal = function(objectArg,paramName) {
global[paramName] = objectArg;
};
var smartenv:any = {}; //create smartenv object
smartenv.items = {}; // create the items object to store items to.
smartenv.makeGlobal = function() {
addToGlobal(smartenv,"smartenv");//add object smartenv as global["smartenv"]
/* ----------------------------------------- *
* ----- Environment ----------------------- *
* ----------------------------------------- */
var environment:Environment;
var setEnvironment = function() {
var localRunTimeEnv = "undefined";
var localUserAgent = "undefined";
if (typeof window !== 'undefined') {
localRunTimeEnv = 'browser';
localUserAgent = navigator.userAgent;
} else if (typeof process !== 'undefined') {
localRunTimeEnv = 'node';
}
environment = new Environment(localRunTimeEnv,localUserAgent);
};
setEnvironment();
smartenv.getEnv = function() {
return environment;
};
/* ----------------------------------------- *
* ----- Info vars ------------------------- *
* ----------------------------------------- */
smartenv.info = {};
smartenv.info.node = {};
smartenv.info.node.version = process.version;
smartenv.info.print = function() {
var pck = require("./package.json");
beautylog.log("node version is " + smartenv.info.node.version + " and smartenv version is " + pck.version);
beautylog.log("the smartenv module currently holds the following properties:");
smartenv.printEnv = function() {
if (environment.isNode) {
var smartenvVersion = require("./package.json").version;
beautylog.log("node version is " + environment.nodeVersion + " and smartenv version is " + smartenvVersion);
} else {
beautylog.log("browser is " + environment.userAgent)
}
beautylog.log("the smartenv registration store currently holds the following properties:");
console.log(Object.getOwnPropertyNames(smartenv.items).sort());
};
@ -31,12 +53,4 @@ smartenv.get = function(keyName) {
return smartenv.items[keyName];
};
smartenv.exportEnv = function() {
};
smartenv.importEnv = function() {
};
module.exports = smartenv;

View File

@ -2,15 +2,14 @@
var smartenv = require("./index.js");
var beautylog = require("beautylog")("os");
beautylog.info("Now testing the smartenv module");
smartenv.info.print();
smartenv.printEnv();
beautylog.info("Now testing the smartenv module");
smartenv.register({ key1: "Peter" }, "docit");
smartenv.info.print();
smartenv.printEnv();
beautylog.log(smartenv.get("docit").key1);
beautylog.log(smartenv.get("docit").key1);
smartenv.makeGlobal();
beautylog.log(global.smartenv.get("docit").key1);
var key2 = "hello";
smartenv.get("docit").key2 = key2;
beautylog.log(global.smartenv.get("docit").key2);
beautylog.log(smartenv.get("docit").key2);
beautylog.success("Success!");
//# sourceMappingURL=test.js.map

View File

@ -1 +1 @@
{"version":3,"file":"test.js","sourceRoot":"","sources":["test.ts"],"names":[],"mappings":"AAAA,yCAAyC;AACzC,IAAI,QAAQ,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;AACrC,IAAI,SAAS,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,CAAC;AAC3C,SAAS,CAAC,IAAI,CAAC,iCAAiC,CAAC,CAAC;AAClD,QAAQ,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;AACtB,SAAS,CAAC,IAAI,CAAC,iCAAiC,CAAC,CAAC;AAClD,QAAQ,CAAC,QAAQ,CAAC,EAAC,IAAI,EAAC,OAAO,EAAC,EAAC,OAAO,CAAC,CAAC;AAC1C,QAAQ,CAAC,IAAI,CAAC,KAA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O,CAAC,CAAC,IAAI,GAAG,IAAI,CAAC;AAClC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC;AAE1C,SAAS,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC"}

View File

@ -2,16 +2,15 @@
var smartenv = require("./index.js");
var beautylog = require("beautylog")("os");
beautylog.info("Now testing the smartenv module");
smartenv.info.print();
smartenv.printEnv();
beautylog.info("Now testing the smartenv module");
smartenv.register({key1:"Peter"},"docit");
smartenv.info.print();
smartenv.printEnv();
beautylog.log(smartenv.get("docit").key1);
beautylog.log(smartenv.get("docit").key1);
smartenv.makeGlobal();
beautylog.log(global.smartenv.get("docit").key1);
var key2 = "hello";
smartenv.get("docit").key2 = key2;
beautylog.log(global.smartenv.get("docit").key2);
beautylog.log(smartenv.get("docit").key2);
beautylog.success("Success!");