Skip to content

Commit

Permalink
Merge pull request IBM#2 from NyWn/master
Browse files Browse the repository at this point in the history
Adding context object option to catalogue manager
  • Loading branch information
Grahahax committed Mar 9, 2017
2 parents 7538864 + a29c4d2 commit 719cf65
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
42 changes: 32 additions & 10 deletions lib/message-catalog-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

var fileUrlRe = /^file:\/\//gi;
var remoteUrlRe = /^http:\/\//gi;
var _ = require('underscore');

//Message-Catalog-Manager class
var MessageCatalogManager = function (indexFilePath) {
Expand Down Expand Up @@ -66,27 +67,27 @@ MessageCatalogManager.prototype.checkCatalog = function (catalogName) {
}
};

var applyInserts = function (msg, context, inserts) {
var applyInserts = function (msg, namedInserts, posInserts) {
var i;
var re;
//Replace positional inserts
for (i = 0; i < inserts.length; i++) {
for (i = 0; i < posInserts.length; i++) {
re = new RegExp("\\{" + i + "\\}", "gi");
msg = msg.replace(re, inserts[i]);
msg = msg.replace(re, posInserts[i]);
}

//Replace key inserts
for (var key in context) {
if (context.hasOwnProperty(key)) {
for (var key in namedInserts) {
if (namedInserts.hasOwnProperty(key)) {
re = new RegExp("\\{" + key + "\\}", "gi");
msg = msg.replace(re, context[key]);
msg = msg.replace(re, namedInserts[key]);
}
}

return msg;
};

MessageCatalogManager.prototype.getMessage = function (catalogName, code, context, inserts, language) {
MessageCatalogManager.prototype.getMessage = function (catalogName, code, namedInserts, posInserts, language, verbose) {

this.checkCatalog(catalogName);
var msg;
Expand Down Expand Up @@ -116,20 +117,41 @@ MessageCatalogManager.prototype.getMessage = function (catalogName, code, contex
//clone the message
var message = JSON.parse(JSON.stringify(msg));

for (var key in namedInserts){
/* istanbul ignore else */
if (namedInserts.hasOwnProperty(key)) {
if (typeof namedInserts[key] !== "string") {
throw new Error("namedInserts value: '" + key + "' must be of type string");
}
}
}

if (message.namedInserts) {
message.namedInserts = _.extend(namedInserts, message.namedInserts);
}
else {
//if no catalog does not contain field, then create from incoming namedInserts object
message.namedInserts = namedInserts;
}

if (message.message) {
//apply Inserts to message
message.message = applyInserts(message.message, context, inserts);
message.message = applyInserts(message.message, namedInserts, posInserts);
}
else {
throw new Error("Message " + code + " found in catalog " + catalogName + " but without message property");
}
if (message.action) {
//apply Inserts to action
message.action = applyInserts(message.action, context, inserts);
message.action = applyInserts(message.action, namedInserts, posInserts);
}
if (message.detail) {
//apply Inserts to detail
message.detail = applyInserts(message.detail, context, inserts);
message.detail = applyInserts(message.detail, namedInserts, posInserts);
}

if(!verbose) {
delete message.namedInserts;
}

return message;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"coveralls": "npm run cover -- --report lcovonly && cat ./coverage/lcov.info | coveralls"
},
"dependencies": {
"fs": "0.0.2"
"fs": "0.0.2",
"underscore": "^1.8.3"
},
"license": "MIT",
"devDependencies": {
Expand Down
8 changes: 8 additions & 0 deletions test/catalogs/example/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,13 @@
},
"0006": {
"message": "This is an example message with no action"
},
"0007": {
"message": "This is a message only in the default catalog",
"action": "Write a real message",
"url": "https://github.com/IBM/message-catalog-manager/README.md",
"namedInserts": {
"key": "value"
}
}
}
24 changes: 22 additions & 2 deletions test/message-catalog-manager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ describe('MessageCatalogManager', function () {
expect(message.message).to.equal("This is an example message with positional inserts 1 2 three");
});
it('returns a resolved message with [positional inserts in a different locale', function () {
var message = MC.getMessage("exampleLocal", "0003", {}, ["ein", "2", "drei"], "de");
var message = MC.getMessage("exampleLocal", "0003", {}, ["eins", "2", "drei"], "de");
console.log(JSON.stringify(message));
expect(message.message).to.equal("This is an example message with positional inserts ein 2 drei, in german locale");
expect(message.message).to.equal("This is an example message with positional inserts eins 2 drei, in german locale");
});
it('returns a resolved message with [positional inserts in the default locale if the given one does not exist', function () {
var message = MC.getMessage("exampleLocal", "0003", {}, ["one", "2", "three"], "ro");
Expand All @@ -106,6 +106,26 @@ describe('MessageCatalogManager', function () {
var test = function() { MC.getMessage("wibble", "invalid_message"); };
expect(test).to.throw(Error, /Catalog wibble not found/);
});
it('returns additional information (namedInserts) if verbose option is set', function () {
var message = MC.getMessage("exampleLocal", "0007", {foo:"bar"},{},"en", 1);
console.log(JSON.stringify(message));
expect(message.namedInserts).to.deep.equal({key:"value",foo:"bar"});
});
it('throws an error if the value of namedInserts is not a string', function() {
var test = function () {MC.getMessage("exampleLocal", "0007", {foo:23},{},"en", 1);};
expect(test).to.throw(Error, /namedInserts value: 'foo' must be of type string/);
});
it('returns a combined notification context object if message.namedInserts exists', function() {
var message = MC.getMessage("exampleLocal", "0007", {foo:"bar"}, {}, "en", 1);
console.log(JSON.stringify(message));
expect(message.namedInserts).to.deep.equal({foo:"bar",key:"value"});
});
it('returns notification context object if message.namedInserts does not exists', function() {
var message = MC.getMessage("exampleLocal", "0001", {foo:"bar"}, {}, "en", 1);
console.log(JSON.stringify(message));
expect(message.namedInserts).to.deep.equal({foo:"bar"});
});


});

Expand Down

0 comments on commit 719cf65

Please sign in to comment.