Created
November 14, 2017 02:35
-
-
Save leizongmin/49fb0fcb38f2b9d4d5a992d64b597ae4 to your computer and use it in GitHub Desktop.
检查密码规则
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 检查密码是否符合规则,如果符合规则返回true | |
* @param {string} pwd | |
* @return {boolean} | |
*/ | |
function check(pwd) { | |
return ( | |
notSeries(pwd, "1234567890", 3) && | |
notSeries(pwd, "abcdefghijklmnopqrstuvwxyz", 3) && | |
notSeries(pwd, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 3) && | |
notSeries(pwd, "qwertyuiop", 3) && | |
notSeries(pwd, "QWERTYUIOP", 3) && | |
notSeries(pwd, "asdfghjkl", 3) && | |
notSeries(pwd, "ASDFGHJKL", 3) && | |
notSeries(pwd, "zxcvbnm", 3) && | |
notSeries(pwd, "ZXCVBNM", 3) && | |
notSeries(pwd, "~!@#$%^&*()_+", 3) && | |
notRepeat(pwd, 3) | |
); | |
} | |
/** | |
* 获取字符串的字符数组 | |
* @param {string} str | |
* @return {array} | |
*/ | |
function getChars(str) { | |
return str.split("").map(c => c.charCodeAt(0)); | |
} | |
/** | |
* 判断是否包含连续字符串,如果不包含返回true | |
* @param {string} pwd | |
* @param {string} str | |
* @param {number} n | |
* @return {boolean} | |
*/ | |
function notSeries(pwd, str, n) { | |
const c1 = getChars(pwd); | |
const c2 = getChars(str); | |
for (let i = 0; i < c1.length - n + 1; i++) { | |
const j = c2.indexOf(c1[i]); | |
if (j !== -1 && j < str.length - n) { | |
let ok = true; | |
for (let k = 1; k < n; k++) { | |
ok = ok && c1[i + k] === c2[j + k]; | |
} | |
if (ok) return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* 判断是否包含重复的字符串,如果不包含返回true | |
* @param {string} pwd | |
* @param {number} n | |
*/ | |
function notRepeat(pwd, n) { | |
const c = getChars(pwd); | |
for (let i = 0; i < c.length - n + 1; i++) { | |
let ok = true; | |
for (let j = 1; j < n; j++) { | |
ok = ok && c[i] === c[i + j]; | |
} | |
if (ok) return false; | |
} | |
return true; | |
} | |
///////////////////////////////////////////////////////////// | |
// 测试 | |
const assert = require("assert"); | |
assert.ok(check("hello, world")); | |
assert.ok(check("rhu2ihfjsn")); | |
assert.ok(check("112")); | |
assert.ok(check("233")); | |
assert.ok(!check("111")); | |
assert.ok(!check("safqwert")); | |
assert.ok(!check("qwasd!@#")); | |
console.log('测试通过,666'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment