-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsleeboard.js
131 lines (119 loc) · 3.14 KB
/
sleeboard.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict';
const am = require('./am.js');
var sleeboard = function () {
if (!(this instanceof sleeboard)) {
return new sleeboard()
}
getInputJson();
var lang,
scope,
maxInserted = 0,
ignoreKeyCodes = [8, 10, 18, 35, 36, 37, 38, 39, 40, 127];
var finalText = '';
/**
* Write the Symbol to the right position on input
* FIXME: caret gets hidden when overflow
* @param vm
* @param entry
*/
function writeOnField(entry, finalText) {
if (entry) {
maxInserted = scope['_scope'] ? 0 : maxInserted;
var sub = finalText.substring(0, finalText.length - (maxInserted));
if (scope['_scope']) {
maxInserted = 1;
} else {
maxInserted = maxInserted < entry.length ? entry.length : maxInserted;
}
return sub + entry;
}
return finalText;
}
/**
* Shrink Scope for Performance
* @param char
*/
function shrinkScope(char) {
if (scope[char]) {
scope = scope[char]['next'] ? scope[char]['next'] : lang;
} else {
scope = lang;
}
}
/**
* Get Symbol from Character
* @param char
* @returns {*}
*/
function getSymbolFromScope(char) {
scope = scope ? scope : lang;
if (scope[char]) {
return scope[char]['value'];
} else if (lang[char]) {
scope = lang;
return scope[char]['value'];
} else {
scope = lang;
return char;
}
}
/**
* handler
* @param text
*/
function handler(text) {
var splitString = text.split('');
var asterisk = 2;
splitString.forEach(char => {
asterisk = char == '*' ? asterisk + 1 : asterisk;
if (char && asterisk % 2 == 0 && char != '*') {
char = char.toLowerCase();
var symbol = getSymbolFromScope(char);
finalText = writeOnField(symbol, finalText);
shrinkScope(char);
} else if(char && asterisk % 2 != 0 && char != '*'){
finalText = finalText+char
scope = lang
}
})
var keyvalue = [];
if(!scope['_scope']){
var example = finalText.slice(0, -1);
keyvalue = Object.keys(scope).map((key)=>{
var chars = key;
var scopeCopy = scope;
var value = scopeCopy[key].value
while(!scopeCopy[key].value){
scopeCopy = scopeCopy[key].next
key = Object.keys(scopeCopy)[0]
chars = chars + key
value = scopeCopy[key].value
}
var result = example+value
return {chars, result}
})
keyvalue.unshift({chars: '', result: finalText})
}else {
keyvalue.unshift({chars: '', result: finalText})
}
return keyvalue;
}
/**
* Request server for json
*/
function getInputJson() {
lang = am
lang['_scope'] = 'all';
}
/**
* Change Input Type
* @param fileName
*/
this.getAmharic = function(text){
finalText = ""
scope = lang
maxInserted = 0
return handler(text)
}
};
module.exports = sleeboard()