Passed
Push — master ( 54ded3...bad67d )
by Evgeny
01:19
created

src/index.js   A

Complexity

Total Complexity 25
Complexity/F 2.27

Size

Lines of Code 110
Function Count 11

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 25
eloc 56
mnd 14
bc 14
fnc 11
dl 0
loc 110
rs 10
bpm 1.2727
cpm 2.2727
noi 0
c 0
b 0
f 0
1
var maps = require('./maps.js');
2
3
var Trans = function() {
4
	var that = this;
5
	
6
	this.maps = maps;
7
	
8
	this.tWord1 = function(word, from_lang, to_lang) {
9
		// console.debug(word, maps.map[from_lang][to_lang]);
10
		
11
		for (var k in maps.map[from_lang][to_lang]) {
12
			// console.debug('k-->', k, word, maps.map[from_lang][to_lang][k]);
13
			if (maps.map[from_lang][to_lang].hasOwnProperty(k)) {
14
				word = word.replace(new RegExp(k, 'g'), maps.map[from_lang][to_lang][k]);
15
			}
16
		}
17
		
18
		return word;
19
	};
20
	
21
	this.tWordN = function(word, from_lang, to_lang, jumpTo) {
22
		if (!word) {
23
			return '';
24
		}
25
		
26
		jumpTo = jumpTo || 0;
27
		var tmp, parts, keys = Object.keys(maps.map[from_lang][to_lang]);
28
		
29
		for (; jumpTo < keys.length; ++jumpTo) {
30
			if (tmp = word.match(new RegExp(keys[jumpTo], 'ig'))) {
31
				try {
32
					tmp[0] = tmp[0].replace(/([\()\[\]])/g, "\\$1");
33
					parts = word.split(new RegExp('(' + tmp[0] + ')')).filter(function(v) {
34
						return v;
35
					});
36
					
37
					// console.debug('in-->', tmp, word, parts);
38
					
39
					for (var i = 0; i < parts.length; ++i) {
40
						if (parts[i] === tmp[0]) {
41
							// console.debug('in-->', keys[jumpTo]);
42
							parts[i] = parts[i].replace(new RegExp(keys[jumpTo], 'ig'), maps.map[from_lang][to_lang][keys[jumpTo]]);
43
						} else {
44
							parts[i] = that.tWordN(parts[i], from_lang, to_lang, jumpTo + 1);
45
						}
46
					}
47
					word = parts.join('');
48
				} catch(e) {
49
					console.error('Failed transliting:', keys[jumpTo], e, jumpTo, tmp, word, from_lang, to_lang);
50
				}
51
				
52
				break;
53
			}
54
		}
55
		
56
		return word;
57
	};
58
	
59
	this.find_bridge = function(str, from_lang, to_lang) {
60
		var found_bridge = false;
61
		
62
		// console.debug(str, from_lang, to_lang, maps.languages);
63
		
64
		for (var i = 0; i < maps.languages.length && found_bridge === false; ++i) {
65
			found_bridge = !!maps.map[maps.languages[i]][to_lang] && maps.languages[i];
66
		}
67
		
68
		if (found_bridge) {
69
			return that.lingualate_helper(that.lingualate_helper(str, from_lang, found_bridge), found_bridge, to_lang);
70
		}
71
		return '';
72
	};
73
	
74
	this.tsplit = function(str) {
75
		return str.split(/[\s\-]/).filter(function(v) { return typeof v !== 'undefined' && v.length; })
76
	};
77
	
78
	this.lingualate_helper = function(str, from_lang, to_lang) {
79
		// return str;
80
		// console.debug('rrr!!!', str, from_lang, to_lang);
81
		if (!str || !maps.map[from_lang]) {
82
			return str || '';
83
		}
84
		
85
		if (!maps.map[from_lang][to_lang]) {
86
			return that.find_bridge(str, from_lang, to_lang);
87
		}
88
		
89
		var words = that.tsplit(str),
90
			func = ['ru', 'uk', 'sr', 'el', 'trans'].indexOf(from_lang) !== -1 ? that.tWord1 : that.tWordN;
91
		
92
		for (var i=0; i < words.length; ++i) {
93
			words[i] = func(words[i], from_lang, to_lang);
94
		}
95
		
96
		return words.join(' ');
97
	};
98
	
99
	this.up = function(str) {
100
		return that.tsplit(str).map(function(v) {
101
			return v.substr(0, 1).toUpperCase() + v.substr(1);
102
		}).join(' ');
103
	};
104
	
105
	this.lingualate = function(str, from_lang, to_lang) {
106
		return that.up(that.lingualate_helper(str.toLowerCase(), from_lang, to_lang));
107
	};
108
};
109
110
module.exports = new Trans();