diff --git a/lib/map.js b/lib/map.js index 7f6d188..6913e9c 100644 --- a/lib/map.js +++ b/lib/map.js @@ -1,5 +1,19 @@ 'use strict'; -module.exports = function(options) { - +module.exports = function(locale, options) { + var regexs = {}; + for (var findString in options.dictionary) { + //don't replace characters inside html tags (between `<` and `>`) + regexs[options.dictionary[findString]] = new RegExp('(?![^<]*>)' + escapeRegExp(findString), 'g'); + } + for (var hash in locale) { + for (var replacement in regexs) { + locale[hash] = locale[hash].replace(regexs[replacement], replacement); + } + } + return locale; }; + +function escapeRegExp(string) { + return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1'); +} diff --git a/test/spec.map.js b/test/spec.map.js index 6e37e61..bc196fc 100644 --- a/test/spec.map.js +++ b/test/spec.map.js @@ -10,8 +10,21 @@ describe('s18n.map()', function() { assert.equal(typeof s18n.map, 'function'); }); - it('should be called', function() { - s18n.map(); + it('should replace substrings using a provided dictionary', function() { + var locale = { + '3c82f755': 'This is a test.' + }; + var mappedLocale = s18n.map(locale, { + dictionary: { + 'Th': 'Z', + 'a': 'á', + 'e': 'é', + 'i': 'í' + } + }); + assert.deepEqual(mappedLocale, { + '3c82f755': 'Zís ís á tést.' + }); }); });