lenient.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. ['j', 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(value) {
  55. const [y, e, s] = value;
  56. let score = 0;
  57. if (yMatch.has(y)) {
  58. score += yMatch.get(y);
  59. }
  60. if (eMatch.has(e)) {
  61. score += eMatch.get(e);
  62. }
  63. if (sMatch.has(s)) {
  64. score += sMatch.get(s);
  65. }
  66. return score;
  67. }
  68. function getNoMatchScore(value) {
  69. const [n, o] = value;
  70. let score = 0;
  71. if (nMatch.has(n)) {
  72. score += nMatch.get(n);
  73. }
  74. if (oMatch.has(o)) {
  75. score += oMatch.get(o);
  76. }
  77. return score;
  78. }
  79. module.exports = (input, options) => {
  80. if (getYesMatchScore(input) >= YES_MATCH_SCORE_THRESHOLD) {
  81. return true;
  82. }
  83. if (getNoMatchScore(input) >= NO_MATCH_SCORE_THRESHOLD) {
  84. return false;
  85. }
  86. return options.default;
  87. };