Guest User

Untitled

a guest
Nov 15th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. /**
  2. * 检查密码是否符合规则,如果符合规则返回true
  3. * @param {string} pwd
  4. * @return {boolean}
  5. */
  6. function check(pwd) {
  7. return (
  8. notSeries(pwd, "1234567890", 3) &&
  9. notSeries(pwd, "abcdefghijklmnopqrstuvwxyz", 3) &&
  10. notSeries(pwd, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 3) &&
  11. notSeries(pwd, "qwertyuiop", 3) &&
  12. notSeries(pwd, "QWERTYUIOP", 3) &&
  13. notSeries(pwd, "asdfghjkl", 3) &&
  14. notSeries(pwd, "ASDFGHJKL", 3) &&
  15. notSeries(pwd, "zxcvbnm", 3) &&
  16. notSeries(pwd, "ZXCVBNM", 3) &&
  17. notSeries(pwd, "~!@#$%^&*()_+", 3) &&
  18. notRepeat(pwd, 3)
  19. );
  20. }
  21.  
  22. /**
  23. * 获取字符串的字符数组
  24. * @param {string} str
  25. * @return {array}
  26. */
  27. function getChars(str) {
  28. return str.split("").map(c => c.charCodeAt(0));
  29. }
  30.  
  31. /**
  32. * 判断是否包含连续字符串,如果不包含返回true
  33. * @param {string} pwd
  34. * @param {string} str
  35. * @param {number} n
  36. * @return {boolean}
  37. */
  38. function notSeries(pwd, str, n) {
  39. const c1 = getChars(pwd);
  40. const c2 = getChars(str);
  41. for (let i = 0; i < c1.length - n + 1; i++) {
  42. const j = c2.indexOf(c1[i]);
  43. if (j !== -1 && j < str.length - n) {
  44. let ok = true;
  45. for (let k = 1; k < n; k++) {
  46. ok = ok && c1[i + k] === c2[j + k];
  47. }
  48. if (ok) return false;
  49. }
  50. }
  51. return true;
  52. }
  53.  
  54. /**
  55. * 判断是否包含重复的字符串,如果不包含返回true
  56. * @param {string} pwd
  57. * @param {number} n
  58. */
  59. function notRepeat(pwd, n) {
  60. const c = getChars(pwd);
  61. for (let i = 0; i < c.length - n + 1; i++) {
  62. let ok = true;
  63. for (let j = 1; j < n; j++) {
  64. ok = ok && c[i] === c[i + j];
  65. }
  66. if (ok) return false;
  67. }
  68. return true;
  69. }
  70.  
  71. /////////////////////////////////////////////////////////////
  72. // 测试
  73. const assert = require("assert");
  74.  
  75. assert.ok(check("hello, world"));
  76. assert.ok(check("rhu2ihfjsn"));
  77. assert.ok(check("112"));
  78. assert.ok(check("233"));
  79.  
  80. assert.ok(!check("111"));
  81. assert.ok(!check("safqwert"));
  82. assert.ok(!check("qwasd!@#"));
  83.  
  84. console.log('测试通过,666');
Add Comment
Please, Sign In to add comment