Skip to content

Commit

Permalink
map method
Browse files Browse the repository at this point in the history
  • Loading branch information
bitjson committed Apr 16, 2015
1 parent 63206e6 commit ac25f39
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
18 changes: 16 additions & 2 deletions lib/map.js
Original file line number Diff line number Diff line change
@@ -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');
}
17 changes: 15 additions & 2 deletions test/spec.map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
});
});

});

0 comments on commit ac25f39

Please sign in to comment.