-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
35 lines (30 loc) · 870 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var wemoji = require('wemoji');
// Regex for replacing
var pattern = new RegExp(Object.keys( wemoji.unicode ).join('|'), 'g');
var defaults = {
before: '[',
after: ']',
field: 'name'
};
module.exports.convert = function (str, options) {
if (typeof str !== 'string') {
return str;
}
if (options == null) options = {};
var before, after
if ( 'delimiter' in options ) {
before = options.delimiter
after = options.delimiter
} else {
before = 'before' in options ? options.before : defaults.before
after = 'after' in options ? options.after : defaults.after
}
var field = options.field || defaults.field;
return str.replace(pattern, function(val) {
if (options.callback) {
return options.callback(val, wemoji.unicode[ val ]);
} else {
return before + wemoji.unicode[ val ][ field ] + after;
}
});
}