diff --git a/index.js b/index.js index 18b00b0..88b2141 100644 --- a/index.js +++ b/index.js @@ -71,30 +71,48 @@ var SmartenvEnvironment; var SmartenvObjectStorage; (function (SmartenvObjectStorage) { function init() { - var obs = {}; + 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 = {}; - 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"); - } - obsItems[paramNameArg] = objectArg; - }; - obs.get = function (keyName) { - return obsItems[keyName]; - }; - obs.getAll = function () { - return obsItems; - }; - obs.addComplete = function (itemsArg) { - obsItems = plugins._.assign(obsItems, itemsArg); - return obsItems; - }; return obs; } SmartenvObjectStorage.init = init; diff --git a/test.js b/test.js index 81fb9ff..e195820 100644 --- a/test.js +++ b/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.obs.add("docit", { key1: "Peter" }); +//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.obs.get("docit").key1); -beautylog.log(smartenv.obs.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.obs.get("docit").key2 = key2; -beautylog.log(smartenv.obs.get("docit").key2); +smartenv.obs.get("myTestObject").key2 = key2; +beautylog.log(smartenv.obs.get("myTestObject").key2); beautylog.success("Success!"); diff --git a/ts/smartenv.objectstorage.ts b/ts/smartenv.objectstorage.ts index 74e375a..dfa341e 100644 --- a/ts/smartenv.objectstorage.ts +++ b/ts/smartenv.objectstorage.ts @@ -1,29 +1,44 @@ /// module SmartenvObjectStorage { export function init() { - var obs:any = {}; + 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 = {}; - obs.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"); - } - obsItems[paramNameArg] = objectArg; - }; - obs.get = function(keyName) { - return obsItems[keyName]; - }; - obs.getAll = function () { - return obsItems; - } - - obs.addComplete = function(itemsArg) { - obsItems = plugins._.assign(obsItems,itemsArg); - return obsItems; - }; return obs; } } \ No newline at end of file diff --git a/ts/test.js b/ts/test.js index 307c3d5..7832d19 100644 --- a/ts/test.js +++ b/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.obs.add("docit", { key1: "Peter" }); +//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.obs.get("docit").key1); -beautylog.log(smartenv.obs.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.obs.get("docit").key2 = key2; -beautylog.log(smartenv.obs.get("docit").key2); +smartenv.obs.get("myTestObject").key2 = key2; +beautylog.log(smartenv.obs.get("myTestObject").key2); beautylog.success("Success!"); //# sourceMappingURL=test.js.map \ No newline at end of file diff --git a/ts/test.js.map b/ts/test.js.map index a7f85cf..e9cb713 100644 --- a/ts/test.js.map +++ b/ts/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,GAAG,CAAC,GAAG,CAAC,OAAO,EAAC,EAAC,IAAI,EAAC,OAAO,EAAC,CAAC,CAAC;AACzC,QAAQ,CAAC,QAAQ,EAAE,CAAC;AACpB,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC;AAC9C,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC;AAE9C,IAAI,IAAI,GAAG,OAAO,CAAC;AACnB,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,GAAG,IAAI,CAAC;AACtC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC;AAE9C,SAAS,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC"} \ No newline at end of file +{"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,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AAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,CAAC;AAErD,SAAS,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC"} \ No newline at end of file diff --git a/ts/test.ts b/ts/test.ts index 8604e96..bd03fdf 100644 --- a/ts/test.ts +++ b/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.obs.add("docit",{key1:"Peter"}); + +//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.obs.get("docit").key1); -beautylog.log(smartenv.obs.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.obs.get("docit").key2 = key2; -beautylog.log(smartenv.obs.get("docit").key2); +smartenv.obs.get("myTestObject").key2 = key2; +beautylog.log(smartenv.obs.get("myTestObject").key2); beautylog.success("Success!"); \ No newline at end of file