lenient.js 1.4 KB

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