UNPKG

1.43 kBJavaScriptView Raw
1'use strict';
2
3const YES_MATCH_SCORE_THRESHOLD = 2;
4const NO_MATCH_SCORE_THRESHOLD = 1.25;
5
6const yMatch = new Map([
7 [5, 0.25],
8 [6, 0.25],
9 [7, 0.25],
10 ['t', 0.75],
11 ['y', 1],
12 ['u', 0.75],
13 ['g', 0.25],
14 ['h', 0.25],
15 ['j', 0.25]
16]);
17
18const eMatch = new Map([
19 [2, 0.25],
20 [3, 0.25],
21 [4, 0.25],
22 ['w', 0.75],
23 ['e', 1],
24 ['r', 0.75],
25 ['s', 0.25],
26 ['d', 0.25],
27 ['f', 0.25]
28]);
29
30const sMatch = new Map([
31 ['q', 0.25],
32 ['w', 0.25],
33 ['e', 0.25],
34 ['a', 0.75],
35 ['s', 1],
36 ['d', 0.75],
37 ['z', 0.25],
38 ['x', 0.25],
39 ['c', 0.25]
40]);
41
42const nMatch = new Map([
43 ['h', 0.25],
44 ['j', 0.25],
45 ['k', 0.25],
46 ['b', 0.75],
47 ['n', 1],
48 ['m', 0.75]
49]);
50
51const oMatch = new Map([
52 [9, 0.25],
53 [0, 0.25],
54 ['i', 0.75],
55 ['o', 1],
56 ['p', 0.75],
57 ['k', 0.25],
58 ['l', 0.25]
59]);
60
61function getYesMatchScore(value) {
62 const [y, e, s] = value;
63 let score = 0;
64
65 if (yMatch.has(y)) {
66 score += yMatch.get(y);
67 }
68
69 if (eMatch.has(e)) {
70 score += eMatch.get(e);
71 }
72
73 if (sMatch.has(s)) {
74 score += sMatch.get(s);
75 }
76
77 return score;
78}
79
80function getNoMatchScore(value) {
81 const [n, o] = value;
82 let score = 0;
83
84 if (nMatch.has(n)) {
85 score += nMatch.get(n);
86 }
87
88 if (oMatch.has(o)) {
89 score += oMatch.get(o);
90 }
91
92 return score;
93}
94
95module.exports = (input, default_) => {
96 if (getYesMatchScore(input) >= YES_MATCH_SCORE_THRESHOLD) {
97 return true;
98 }
99
100 if (getNoMatchScore(input) >= NO_MATCH_SCORE_THRESHOLD) {
101 return false;
102 }
103
104 return default_;
105};