1
- import { Component , Input , OnChanges , OnInit , SimpleChanges } from '@angular/core' ;
1
+ import { Component , EventEmitter , Input , OnChanges , OnInit , Output , SimpleChanges } from '@angular/core' ;
2
2
3
3
export enum Colors {
4
4
primary = 'primary' ,
@@ -19,6 +19,9 @@ export class PasswordStrengthComponent implements OnInit, OnChanges {
19
19
@Input ( )
20
20
externalError : boolean ;
21
21
22
+ @Output ( )
23
+ onStrengthChanged : EventEmitter < number > = new EventEmitter < number > ( ) ;
24
+
22
25
containAtLeastEightChars : boolean ;
23
26
containAtLeastOneLowerCaseLetter : boolean ;
24
27
containAtLeastOneUpperCaseLetter : boolean ;
@@ -36,7 +39,6 @@ export class PasswordStrengthComponent implements OnInit, OnChanges {
36
39
}
37
40
38
41
ngOnChanges ( changes : SimpleChanges ) : void {
39
- console . log ( 'on change: ' , changes ) ;
40
42
if ( changes . externalError && changes . externalError . firstChange ) {
41
43
this . _color = Colors . primary ;
42
44
return ;
@@ -46,10 +48,9 @@ export class PasswordStrengthComponent implements OnInit, OnChanges {
46
48
return ;
47
49
}
48
50
this . password && this . password . length > 0 ?
49
- this . calculatePasswordStrength ( ) : this . _strength = 0 ;
51
+ this . calculatePasswordStrength ( ) : this . reset ( ) ;
50
52
}
51
53
52
-
53
54
get strength ( ) : number {
54
55
return this . _strength ? this . _strength : 0 ;
55
56
}
@@ -66,41 +67,51 @@ export class PasswordStrengthComponent implements OnInit, OnChanges {
66
67
}
67
68
68
69
private _containAtLeastEightChars ( ) : boolean {
69
- return this . password . length >= 8 ;
70
+ this . containAtLeastEightChars = this . password . length >= 8 ;
71
+ return this . containAtLeastEightChars ;
70
72
}
71
73
72
74
private _containAtLeastOneLowerCaseLetter ( ) : boolean {
73
- return RegExp ( / ^ (? = .* ?[ a - z ] ) / ) . test ( this . password ) ;
75
+ this . containAtLeastOneLowerCaseLetter = RegExp ( / ^ (? = .* ?[ a - z ] ) / ) . test ( this . password ) ;
76
+ return this . containAtLeastOneLowerCaseLetter ;
74
77
}
75
78
76
79
private _containAtLeastOneUpperCaseLetter ( ) : boolean {
77
- return RegExp ( / ^ (? = .* ?[ A - Z ] ) / ) . test ( this . password ) ;
80
+ this . containAtLeastOneUpperCaseLetter = RegExp ( / ^ (? = .* ?[ A - Z ] ) / ) . test ( this . password ) ;
81
+ return this . containAtLeastOneUpperCaseLetter ;
78
82
}
79
83
80
84
private _containAtLeastOneDigit ( ) : boolean {
81
- return RegExp ( / ^ (? = .* ?[ 0 - 9 ] ) / ) . test ( this . password ) ;
85
+ this . containAtLeastOneDigit = RegExp ( / ^ (? = .* ?[ 0 - 9 ] ) / ) . test ( this . password ) ;
86
+ return this . containAtLeastOneDigit ;
82
87
}
83
88
84
89
private _containAtLeastOneSpecialChar ( ) : boolean {
85
- return RegExp ( / ^ (? = .* ?[ " ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \] ^ _ ` { | } ~ " ] ) / ) . test ( this . password ) ;
90
+ this . containAtLeastOneSpecialChar = RegExp ( / ^ (? = .* ?[ " ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \] ^ _ ` { | } ~ " ] ) / ) . test ( this . password ) ;
91
+ return this . containAtLeastOneSpecialChar ;
86
92
}
87
93
88
94
calculatePasswordStrength ( ) {
89
95
const requirements : Array < boolean > = [ ] ;
90
96
const unit = 100 / 5 ;
91
97
92
-
93
98
requirements . push (
94
99
this . _containAtLeastEightChars ( ) ,
95
100
this . _containAtLeastOneLowerCaseLetter ( ) ,
96
101
this . _containAtLeastOneUpperCaseLetter ( ) ,
97
102
this . _containAtLeastOneDigit ( ) ,
98
103
this . _containAtLeastOneSpecialChar ( ) ) ;
99
104
100
- console . log ( 'requirements = ' , requirements ) ;
101
-
102
105
this . _strength = requirements . filter ( v => v ) . length * unit ;
106
+ this . onStrengthChanged . emit ( this . strength ) ;
107
+ }
103
108
104
- console . log ( 'strength = ' , this . _strength ) ;
109
+ reset ( ) {
110
+ this . _strength = 0 ;
111
+ this . containAtLeastEightChars =
112
+ this . containAtLeastOneLowerCaseLetter =
113
+ this . containAtLeastOneUpperCaseLetter =
114
+ this . containAtLeastOneDigit =
115
+ this . containAtLeastOneSpecialChar = false ;
105
116
}
106
117
}
0 commit comments