Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
acc0e5221b | |||
a6839420f1 | |||
2df7cf1467 | |||
bae4305dec | |||
1737075753 | |||
1a74ee8bb6 | |||
28538001e9 | |||
9ec0b49716 | |||
cab52e1b02 | |||
d601a0eeb0 | |||
491f22ffca | |||
1fec5323d9 | |||
582afc0361 | |||
c054ecb0c7 | |||
249b626bb5 | |||
08d743477f |
9
.gitignore
vendored
9
.gitignore
vendored
@ -1,3 +1,10 @@
|
||||
node_modules/
|
||||
.settings/
|
||||
.idea/
|
||||
.idea/
|
||||
|
||||
|
||||
#npm devug
|
||||
npm-debug.log
|
||||
|
||||
ts/*.js
|
||||
ts/*.js.map
|
||||
|
156
index.js
156
index.js
@ -18,54 +18,114 @@ var Environment = (function () {
|
||||
;
|
||||
return Environment;
|
||||
})();
|
||||
/// <reference path="index.ts" />
|
||||
/**
|
||||
* Deals with the environment the current JS script is running in.
|
||||
*/
|
||||
var SmartenvEnvironment;
|
||||
(function (SmartenvEnvironment) {
|
||||
var environment;
|
||||
var envDetermined = false;
|
||||
/**
|
||||
* returns the environment
|
||||
* @returns {Environment}
|
||||
*/
|
||||
var getEnv = function () {
|
||||
if (!envDetermined) {
|
||||
(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);
|
||||
})();
|
||||
}
|
||||
;
|
||||
return environment;
|
||||
};
|
||||
/**
|
||||
* prints the environment to console
|
||||
*/
|
||||
var printEnv = function () {
|
||||
if (this.getEnv().isNode) {
|
||||
var smartenvVersion = require("./package.json").version;
|
||||
plugins.beautylog.log("node version is " + this.getEnv().nodeVersion + " and smartenv version is " + smartenvVersion);
|
||||
}
|
||||
else {
|
||||
plugins.beautylog.log("browser is " + this.getEnv().userAgent);
|
||||
}
|
||||
plugins.beautylog.log("the smartenv registration store currently holds the following properties:");
|
||||
console.log(Object.getOwnPropertyNames(smartenv.obs.getAll()));
|
||||
};
|
||||
SmartenvEnvironment.init = function (objectArg) {
|
||||
objectArg.getEnv = getEnv;
|
||||
objectArg.printEnv = printEnv;
|
||||
};
|
||||
})(SmartenvEnvironment || (SmartenvEnvironment = {}));
|
||||
/// <reference path="index.ts" />
|
||||
var SmartenvObjectStorage;
|
||||
(function (SmartenvObjectStorage) {
|
||||
function init() {
|
||||
var obs = {
|
||||
add: function (paramNameArg, objectArg) {
|
||||
if (paramNameArg === void 0) { paramNameArg = "undefined"; }
|
||||
if (objectArg === void 0) { objectArg = "undefined"; }
|
||||
if (paramNameArg == "undefined") {
|
||||
plugins.beautylog.error("paramName is undefined");
|
||||
return;
|
||||
}
|
||||
if (objectArg == "undefined") {
|
||||
plugins.beautylog.error("objectArg is undefined");
|
||||
}
|
||||
if (typeof obsItems[paramNameArg] === "undefined") {
|
||||
obsItems[paramNameArg] = objectArg;
|
||||
}
|
||||
else {
|
||||
plugins.beautylog.error("object is already present, so add operation has failed.");
|
||||
}
|
||||
return obsItems[paramNameArg];
|
||||
},
|
||||
replace: function (paramNameArg, objectArg) {
|
||||
obsItems[paramNameArg] = objectArg;
|
||||
},
|
||||
merge: function (paramNameArg, objectArg) {
|
||||
if (!(typeof obsItems[paramNameArg] === "undefined")) {
|
||||
obsItems[paramNameArg] = plugins._.assign(obsItems[paramNameArg], objectArg);
|
||||
}
|
||||
else {
|
||||
plugins.beautylog.error("object is not present, so there is nothing to merge");
|
||||
}
|
||||
},
|
||||
get: function (keyName) {
|
||||
return obsItems[keyName];
|
||||
},
|
||||
getAll: function () {
|
||||
return obsItems;
|
||||
},
|
||||
addComplete: function (itemsArg) {
|
||||
obsItems = plugins._.assign(obsItems, itemsArg);
|
||||
return obsItems;
|
||||
}
|
||||
};
|
||||
var obsItems = {};
|
||||
return obs;
|
||||
}
|
||||
SmartenvObjectStorage.init = init;
|
||||
})(SmartenvObjectStorage || (SmartenvObjectStorage = {}));
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
/// <reference path="classes.ts" />
|
||||
var beautylog = require("beautylog")("os");
|
||||
/// <reference path="smartenv.classes.ts" />
|
||||
/// <reference path="smartenv.environment.ts" />
|
||||
/// <reference path="smartenv.objectstorage.ts" />
|
||||
var plugins = {
|
||||
beautylog: require("beautylog")("os"),
|
||||
_: require("lodash")
|
||||
};
|
||||
var smartenv = {}; //create smartenv object
|
||||
smartenv.items = {}; // create the items object to store items to.
|
||||
/* ----------------------------------------- *
|
||||
* ----- 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);
|
||||
};
|
||||
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) {
|
||||
if (paramName === void 0) { paramName = "undefined"; }
|
||||
if (paramName == "undefined") {
|
||||
beautylog.error("paramName is undefined");
|
||||
return;
|
||||
}
|
||||
smartenv.items[paramName] = objectArg;
|
||||
};
|
||||
smartenv.get = function (keyName) {
|
||||
return smartenv.items[keyName];
|
||||
};
|
||||
SmartenvEnvironment.init(smartenv);
|
||||
smartenv.obs = SmartenvObjectStorage.init();
|
||||
module.exports = smartenv;
|
||||
|
10
package.json
10
package.json
@ -1,13 +1,14 @@
|
||||
{
|
||||
"name": "smartenv",
|
||||
"version": "0.0.6",
|
||||
"version": "0.0.13",
|
||||
"description": "store things about your environment and let them travel across modules",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "(cd ts/compile && node compile.js) && (node test.js)",
|
||||
"reinstall": "(rm -r node_modules && npm install)",
|
||||
"release": "(git pull origin master && npm version patch && git push origin master && git checkout release && git merge master && git push origin release && git checkout master)",
|
||||
"startdev": "(git checkout master && git pull origin master && npm install)"
|
||||
"update": "(git checkout master && git pull origin master && npm install)",
|
||||
"upgrade": "(npm run update) && (ncu upgradeAll && npm install)"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -23,10 +24,11 @@
|
||||
},
|
||||
"homepage": "https://github.com/pushrocks/smartenv",
|
||||
"dependencies": {
|
||||
"beautylog": "^1.0.4"
|
||||
"beautylog": "^1.0.6",
|
||||
"lodash": "^3.10.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"gulp": "^3.9.0",
|
||||
"gulp-typescript": "^2.9.2"
|
||||
"gulp-typescript": "^2.10.0"
|
||||
}
|
||||
}
|
||||
|
17
test.js
17
test.js
@ -4,11 +4,18 @@ var beautylog = require("beautylog")("os");
|
||||
beautylog.info("Now testing the smartenv module");
|
||||
smartenv.printEnv();
|
||||
beautylog.info("Now testing the smartenv module");
|
||||
smartenv.register({ key1: "Peter" }, "docit");
|
||||
//test smartenv.obs.add
|
||||
smartenv.obs.add("myTestObject", { key1: "Peter" });
|
||||
smartenv.obs.add("myTestObject", { key1: "Klaus" }); //now trying to add a second
|
||||
smartenv.printEnv();
|
||||
beautylog.log(smartenv.get("docit").key1);
|
||||
beautylog.log(smartenv.get("docit").key1);
|
||||
beautylog.log(smartenv.obs.get("myTestObject").key1); // this should be Peter
|
||||
//test smartenv.obs.replace
|
||||
smartenv.obs.replace("myTestObject", { key1: "Klaus" });
|
||||
beautylog.log(smartenv.obs.get("myTestObject").key1); // this should be Klaus
|
||||
//test smartenv.obs.merge
|
||||
smartenv.obs.merge("myTestObject", { key2: "Peter" });
|
||||
beautylog.log(smartenv.obs.get("myTestObject").key1 + smartenv.obs.get("myTestObject").key2); // this should be KlausPeter
|
||||
var key2 = "hello";
|
||||
smartenv.get("docit").key2 = key2;
|
||||
beautylog.log(smartenv.get("docit").key2);
|
||||
smartenv.obs.get("myTestObject").key2 = key2;
|
||||
beautylog.log(smartenv.obs.get("myTestObject").key2);
|
||||
beautylog.success("Success!");
|
||||
|
@ -1,21 +0,0 @@
|
||||
/// <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 +0,0 @@
|
||||
{"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"}
|
57
ts/index.js
57
ts/index.js
@ -1,52 +1,13 @@
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
/// <reference path="classes.ts" />
|
||||
var beautylog = require("beautylog")("os");
|
||||
/// <reference path="smartenv.classes.ts" />
|
||||
/// <reference path="smartenv.environment.ts" />
|
||||
/// <reference path="smartenv.objectstorage.ts" />
|
||||
var plugins = {
|
||||
beautylog: require("beautylog")("os"),
|
||||
_: require("lodash")
|
||||
};
|
||||
var smartenv = {}; //create smartenv object
|
||||
smartenv.items = {}; // create the items object to store items to.
|
||||
/* ----------------------------------------- *
|
||||
* ----- 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);
|
||||
};
|
||||
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) {
|
||||
if (paramName === void 0) { paramName = "undefined"; }
|
||||
if (paramName == "undefined") {
|
||||
beautylog.error("paramName is undefined");
|
||||
return;
|
||||
}
|
||||
smartenv.items[paramName] = objectArg;
|
||||
};
|
||||
smartenv.get = function (keyName) {
|
||||
return smartenv.items[keyName];
|
||||
};
|
||||
SmartenvEnvironment.init(smartenv);
|
||||
smartenv.obs = SmartenvObjectStorage.init();
|
||||
module.exports = smartenv;
|
||||
//# sourceMappingURL=index.js.map
|
@ -1 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,yCAAyC;AACzC,mCAAmC;AACnC,IAAI,SAAS,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,CAAC;AAC3C,IAAI,QAAQ,GAAO,EAAE,CAAC,CAAC,wBAAwB;AAC/C,QAAQ,CAAC,KAAK,GAAG,EAAE,CAAC,CAAC,6CAA6C;AAElE;;+CAE+C;AAC/C,IAAI,WAAuB,CAAC;AAC5B,IAAI,cAAc,GAAG;IACjB,IAAI,eAAe,GAAG,WAAW,CAAC;IAClC,IAAI,cAAc,GAAG,WAAW,CAAC;IACjC,EAAE,CAAC,CAAC,OAAO,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC;QAChC,eAAe,GAAG,SAAS,CAAC;QAC5B,cAAc,GAAG,SAAS,CAAC,SAAS,CAAC;IACzC,CAAC;IA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OAAO;IAC3B,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;AACnC,CAAC,CAAC;AAEF,MAAM,CAAC,OAAO,GAAG,QAAQ,CAAC"}
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,yCAAyC;AACzC,4CAA4C;AAC5C,gDAAgD;AAChD,kDAAkD;AAClD,IAAI,OAAO,GAAG;IACV,SAAS,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC;IACrC,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC;CACvB,CAAA;AACD,IAAI,QAAQ,GAAO,EAAE,CAAC,CAAC,wBAAwB;AAE/C,mBAAmB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AACnC,QAAQ,CAAC,GAAG,GAAG,qBAAqB,CAAC,IAAI,EAAE,CAAC;AAI5C,MAAM,CAAC,OAAO,GAAG,QAAQ,CAAC"}
|
58
ts/index.ts
58
ts/index.ts
@ -1,56 +1,16 @@
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
/// <reference path="classes.ts" />
|
||||
var beautylog = require("beautylog")("os");
|
||||
/// <reference path="smartenv.classes.ts" />
|
||||
/// <reference path="smartenv.environment.ts" />
|
||||
/// <reference path="smartenv.objectstorage.ts" />
|
||||
var plugins = {
|
||||
beautylog: require("beautylog")("os"),
|
||||
_: require("lodash")
|
||||
}
|
||||
var smartenv:any = {}; //create smartenv object
|
||||
smartenv.items = {}; // create the items object to store items to.
|
||||
|
||||
/* ----------------------------------------- *
|
||||
* ----- 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();
|
||||
SmartenvEnvironment.init(smartenv);
|
||||
smartenv.obs = SmartenvObjectStorage.init();
|
||||
|
||||
|
||||
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 = "undefined") {
|
||||
if (paramName == "undefined"){
|
||||
beautylog.error("paramName is undefined");
|
||||
return;
|
||||
}
|
||||
smartenv.items[paramName] = objectArg;
|
||||
};
|
||||
smartenv.get = function(keyName) {
|
||||
return smartenv.items[keyName];
|
||||
};
|
||||
|
||||
module.exports = smartenv;
|
||||
|
@ -2,7 +2,7 @@
|
||||
class Environment {
|
||||
public runtimeEnv:string;
|
||||
public userAgent:string;
|
||||
public nodeVersion;
|
||||
public nodeVersion:string;
|
||||
public isBrowser:boolean;
|
||||
public isNode:boolean;
|
||||
constructor(runtimeEnvArg,userAgentArg = "undefined") {
|
||||
@ -18,4 +18,4 @@ class Environment {
|
||||
this.nodeVersion = "undefined";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
49
ts/smartenv.environment.ts
Normal file
49
ts/smartenv.environment.ts
Normal file
@ -0,0 +1,49 @@
|
||||
/// <reference path="index.ts" />
|
||||
/**
|
||||
* Deals with the environment the current JS script is running in.
|
||||
*/
|
||||
module SmartenvEnvironment {
|
||||
|
||||
var environment:Environment;
|
||||
var envDetermined:boolean = false;
|
||||
|
||||
/**
|
||||
* returns the environment
|
||||
* @returns {Environment}
|
||||
*/
|
||||
var getEnv = function(){
|
||||
if (!envDetermined) {
|
||||
(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);
|
||||
})();
|
||||
};
|
||||
return environment;
|
||||
};
|
||||
|
||||
/**
|
||||
* prints the environment to console
|
||||
*/
|
||||
var printEnv = function() {
|
||||
if (this.getEnv().isNode) {
|
||||
var smartenvVersion = require("./package.json").version;
|
||||
plugins.beautylog.log("node version is " + this.getEnv().nodeVersion + " and smartenv version is " + smartenvVersion);
|
||||
} else {
|
||||
plugins.beautylog.log("browser is " + this.getEnv().userAgent)
|
||||
}
|
||||
plugins.beautylog.log("the smartenv registration store currently holds the following properties:");
|
||||
console.log(Object.getOwnPropertyNames(smartenv.obs.getAll()));
|
||||
}
|
||||
|
||||
export var init = function(objectArg) {
|
||||
objectArg.getEnv = getEnv;
|
||||
objectArg.printEnv = printEnv;
|
||||
}
|
||||
}
|
44
ts/smartenv.objectstorage.ts
Normal file
44
ts/smartenv.objectstorage.ts
Normal file
@ -0,0 +1,44 @@
|
||||
/// <reference path="index.ts" />
|
||||
module SmartenvObjectStorage {
|
||||
export function init() {
|
||||
var obs:any = {
|
||||
add: function(paramNameArg = "undefined",objectArg = "undefined") {
|
||||
if (paramNameArg == "undefined"){
|
||||
plugins.beautylog.error("paramName is undefined");
|
||||
return;
|
||||
}
|
||||
if (objectArg == "undefined"){
|
||||
plugins.beautylog.error("objectArg is undefined");
|
||||
}
|
||||
if (typeof obsItems[paramNameArg] === "undefined"){
|
||||
obsItems[paramNameArg] = objectArg;
|
||||
} else {
|
||||
plugins.beautylog.error("object is already present, so add operation has failed.");
|
||||
}
|
||||
return obsItems[paramNameArg];
|
||||
},
|
||||
replace: function(paramNameArg,objectArg){
|
||||
obsItems[paramNameArg] = objectArg;
|
||||
},
|
||||
merge: function(paramNameArg,objectArg){
|
||||
if(!(typeof obsItems[paramNameArg] === "undefined")){
|
||||
obsItems[paramNameArg] = plugins._.assign(obsItems[paramNameArg],objectArg);
|
||||
} else {
|
||||
plugins.beautylog.error("object is not present, so there is nothing to merge");
|
||||
}
|
||||
},
|
||||
get: function(keyName) {
|
||||
return obsItems[keyName];
|
||||
},
|
||||
getAll: function () {
|
||||
return obsItems;
|
||||
},
|
||||
addComplete: function(itemsArg) {
|
||||
obsItems = plugins._.assign(obsItems,itemsArg);
|
||||
return obsItems;
|
||||
}
|
||||
};
|
||||
var obsItems:any = {};
|
||||
return obs;
|
||||
}
|
||||
}
|
17
ts/test.js
17
ts/test.js
@ -4,12 +4,19 @@ var beautylog = require("beautylog")("os");
|
||||
beautylog.info("Now testing the smartenv module");
|
||||
smartenv.printEnv();
|
||||
beautylog.info("Now testing the smartenv module");
|
||||
smartenv.register({ key1: "Peter" }, "docit");
|
||||
//test smartenv.obs.add
|
||||
smartenv.obs.add("myTestObject", { key1: "Peter" });
|
||||
smartenv.obs.add("myTestObject", { key1: "Klaus" }); //now trying to add a second
|
||||
smartenv.printEnv();
|
||||
beautylog.log(smartenv.get("docit").key1);
|
||||
beautylog.log(smartenv.get("docit").key1);
|
||||
beautylog.log(smartenv.obs.get("myTestObject").key1); // this should be Peter
|
||||
//test smartenv.obs.replace
|
||||
smartenv.obs.replace("myTestObject", { key1: "Klaus" });
|
||||
beautylog.log(smartenv.obs.get("myTestObject").key1); // this should be Klaus
|
||||
//test smartenv.obs.merge
|
||||
smartenv.obs.merge("myTestObject", { key2: "Peter" });
|
||||
beautylog.log(smartenv.obs.get("myTestObject").key1 + smartenv.obs.get("myTestObject").key2); // this should be KlausPeter
|
||||
var key2 = "hello";
|
||||
smartenv.get("docit").key2 = key2;
|
||||
beautylog.log(smartenv.get("docit").key2);
|
||||
smartenv.obs.get("myTestObject").key2 = key2;
|
||||
beautylog.log(smartenv.obs.get("myTestObject").key2);
|
||||
beautylog.success("Success!");
|
||||
//# sourceMappingURL=test.js.map
|
@ -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,QAAQ,EAAE,CAAC;AACpB,SAAS,CAAC,IAAI,CAAC,iCAAiC,CAAC,CAAC;AAClD,QAAQ,CAAC,QAAQ,CAAC,EAAC,IAAI,EAAC,OAAO,EAAC,EAAC,OAAO,CAAC,CAAC;AAC1C,QAAQ,CAAC,QAAQ,EAAE,CAAC;AACpB,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC;AAC1C,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC;AAE1C,IAAI,IAAI,GAAG,OAAO,CAAC;AACnB,QAAQ,CAAC,GAAG,CAAC,OAAO,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"}
|
||||
{"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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AC7C,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,CAAC;AAErD,SAAS,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC"}
|
20
ts/test.ts
20
ts/test.ts
@ -4,13 +4,23 @@ var beautylog = require("beautylog")("os");
|
||||
beautylog.info("Now testing the smartenv module");
|
||||
smartenv.printEnv();
|
||||
beautylog.info("Now testing the smartenv module");
|
||||
smartenv.register({key1:"Peter"},"docit");
|
||||
|
||||
//test smartenv.obs.add
|
||||
smartenv.obs.add("myTestObject",{key1:"Peter"});
|
||||
smartenv.obs.add("myTestObject",{key1:"Klaus"}); //now trying to add a second
|
||||
smartenv.printEnv();
|
||||
beautylog.log(smartenv.get("docit").key1);
|
||||
beautylog.log(smartenv.get("docit").key1);
|
||||
beautylog.log(smartenv.obs.get("myTestObject").key1); // this should be Peter
|
||||
|
||||
//test smartenv.obs.replace
|
||||
smartenv.obs.replace("myTestObject",{key1:"Klaus"});
|
||||
beautylog.log(smartenv.obs.get("myTestObject").key1); // this should be Klaus
|
||||
|
||||
//test smartenv.obs.merge
|
||||
smartenv.obs.merge("myTestObject",{key2:"Peter"});
|
||||
beautylog.log(smartenv.obs.get("myTestObject").key1 + smartenv.obs.get("myTestObject").key2); // this should be KlausPeter
|
||||
|
||||
var key2 = "hello";
|
||||
smartenv.get("docit").key2 = key2;
|
||||
beautylog.log(smartenv.get("docit").key2);
|
||||
smartenv.obs.get("myTestObject").key2 = key2;
|
||||
beautylog.log(smartenv.obs.get("myTestObject").key2);
|
||||
|
||||
beautylog.success("Success!");
|
Reference in New Issue
Block a user