-
Notifications
You must be signed in to change notification settings - Fork 2
/
dominant_writing.js
49 lines (43 loc) · 1.24 KB
/
dominant_writing.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
'use strict'
require('./scripts.js');
function characterScript(code) {
for (let script of SCRIPTS) {
if (script.ranges.some(([from, to]) => {
return code >= from && code < to;
})) {
return script;
}
}
return null;
}
function countBy(items, groupName) {
let counts = [];
for (let item of items) {
let name = groupName(item);
let known = counts.findIndex(c => c.name == name);
if (known == -1) {
counts.push({name, count: 1});
} else {
counts[known].count++;
}
}
return counts;
}
function dominantDirection (text) {
let counted = countBy(text,char => {
let script = characterScript(char.codePointAt(0));
return script ? script.name : "none";
}).filter(s => s.name !== "none");
if (counted.length === 0) { return "ltr"}
let majorScript = counted.reduce( (a,b) => a.count < b.count ? b : a);
for (let script of SCRIPTS) {
if (script.name === majorScript.name) {
return `Dominant Language: ${script.name} and its direction: ${script.direction}`;
}
}
}
console.log(dominantDirection('英国的狗说"woof", 俄罗斯的狗说"тяв"'))
console.log(dominantDirection("Hello!"));
// → ltr
console.log(dominantDirection("Hey, مساء الخير"));
// → rtl