-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
106 lines (92 loc) · 1.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* Password Strength
*
* Check if a password is trong enough.
*
* Copyright (c) 2014 by Hsiaoming Yang.
*/
/**
* If the password is alphabet step by step.
*/
function byStep(raw) {
// e.g. 123456, abcde
var delta = raw.charCodeAt(1) - raw.charCodeAt(0);
for (var i = 0; i < raw.length-1; i++) {
if (raw.charCodeAt(i+1) - raw.charCodeAt(i) !== delta) {
return false;
}
}
return true;
}
var ASDF = ['qwertyuiop', 'asdfghjkl', 'zxcvbnm'];
/**
* If the password is in the order on keyboard.
*/
function isAsdf(raw) {
var reverse = raw.split('').reverse().join('');
var asdf = ASDF.join('');
if (~asdf.indexOf(raw) || ~asdf.indexOf(reverse)) {
return true;
}
asdf = ASDF.reverse().join('');
if (~asdf.indexOf(raw) || ~asdf.indexOf(reverse)) {
return true;
}
return false;
}
function strength(raw) {
if (raw.length < 6) {
return 'simple';
}
var types = 0;
// lower case
if (/[a-z]/.test(raw)) types += 1;
// upper case
if (/[A-Z]/.test(raw)) types += 1;
// number
if (/[0-9]/.test(raw)) types += 1;
// marks
if (/[^0-9a-zA-Z]/.test(raw)) types += 1;
if (raw.length < 8 && types === 1) {
return 'simple';
}
return types > 2 ? 'strong': 'medium';
}
/**
* Export interface.
*/
function valid(raw, cb) {
var ret;
if (raw.length < valid.min) {
ret = {
valid: false,
strength: 'simple',
hint: 'too short'
};
} else if (~valid.words.indexOf(raw)) {
ret = {
valid: false,
strength: 'simple',
hint: 'simple word'
};
} else if (byStep(raw) || isAsdf(raw)) {
ret = {
valid: false,
strength: 'simple',
hint: 'too simple'
};
} else {
ret = {
valid: true,
strength: strength(raw),
hint: null
};
}
cb && cb(ret);
return ret;
}
// min length of the password
valid.min = 4;
// password can't be these words
valid.words = [];
module.exports = valid;