-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathemojisplit.js
91 lines (83 loc) · 2.6 KB
/
emojisplit.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var EmojiSpliter = function(){
var json = '';
var predic = new Array();
var stack = '';
this.loadData = function(){
json = (function () {
json = null;
$.ajax({
'async': false,
'global': false,
'url': 'emoji.json',
'dataType': "json",
'success': function (data) {
json = data;
}
});
return json;
})();
}
this.create = function(){
this.loadData();
for(var i=0; i<5; i++) predic[i] = new Array();
return this;
}
function decodeEntities(s){
var str, temp = document.createElement('p');
temp.innerHTML= s;
str= temp.textContent || temp.innerText;
temp=null;
return str;
}
function generateUID() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
function getBackFromUID(eleID, st){
return st[eleID];
}
this.splitBySymbol = function(emojistr, symbol){
if(symbol==undefined)
symbol = '/////';
$.each(json, function(k, v){
if(v['code_decimal'].split('‍').length===4 || v['code_decimal'].split('‍').length===4)
predic[4].push([decodeEntities(v['code_decimal']), v['unicode']]);
else if(v['code_decimal'].split('‍').length===3 || v['code_decimal'].split('‍').length===3)
predic[3].push([decodeEntities(v['code_decimal']), v['unicode']]);
else if(v['code_decimal'].split('‍').length===2 || v['code_decimal'].split('‍').length===2)
predic[2].push([decodeEntities(v['code_decimal']), v['unicode']]);
else if(v['code_decimal'].indexOf('🏻')>0 || v['code_decimal'].indexOf('🏼')>0 || v['code_decimal'].indexOf('🏽')>0 || v['code_decimal'].indexOf('🏾')>0 || v['code_decimal'].indexOf('🏿')>0)
predic[1].push([decodeEntities(v['code_decimal']), v['unicode']]);
else
predic[0].push([decodeEntities(v['code_decimal']), v['unicode']]);
});
stack = "{";
for(var i=4; i>=0; i--)
$.each(predic[i], function(it, val){
if(emojistr.indexOf(val[0])>=0){
var guid = generateUID();
stack += '"'+guid+'":' + '"'+val[0]+'",';
emojistr = emojistr.replace(RegExp(val[0], 'g'), symbol+guid+symbol);
}
});
if(stack.length>2)
stack = stack.substring(0, stack.length-1)+'}';
else
stack = stack+'}';
var stackUID = JSON.parse(stack);
var splitUID = emojistr.split(RegExp(symbol, 'g'));
var result = new Array();
for(var i=0; i<splitUID.length; i++){
if(stackUID[splitUID[i]]!=null)
result.push(stackUID[splitUID[i]]);
else
result.push(splitUID[i]);
}
return result.filter(Boolean);
}
}