1
1
import { Component , Input , OnChanges , OnInit , SimpleChanges } from '@angular/core' ;
2
2
3
3
export enum Colors {
4
- primary ,
5
- accent ,
6
- warn
4
+ primary = 'primary' ,
5
+ accent = 'accent' ,
6
+ warn = 'warn'
7
7
}
8
8
9
9
@Component ( {
@@ -38,31 +38,30 @@ export class PasswordStrengthComponent implements OnInit, OnChanges {
38
38
ngOnChanges ( changes : SimpleChanges ) : void {
39
39
console . log ( 'on change: ' , changes ) ;
40
40
if ( changes . externalError && changes . externalError . firstChange ) {
41
- this . _color = Colors [ Colors . primary ] ;
41
+ this . _color = Colors . primary ;
42
42
return ;
43
43
}
44
44
if ( changes . externalError && changes . externalError . currentValue ) {
45
- this . _color = Colors [ Colors . warn ] ;
45
+ this . _color = Colors . warn ;
46
46
return ;
47
47
}
48
48
this . password && this . password . length > 0 ?
49
- this . _calculatePasswordStrength ( ) : this . _strength = 0 ;
49
+ this . calculatePasswordStrength ( ) : this . _strength = 0 ;
50
50
}
51
51
52
52
53
53
get strength ( ) : number {
54
- // console.log('strength = ', this._strength);
55
- return this . _strength ;
54
+ return this . _strength ? this . _strength : 0 ;
56
55
}
57
56
58
57
get color ( ) : string {
59
58
60
59
if ( this . _strength <= 20 ) {
61
- return Colors [ Colors . warn ] ;
60
+ return Colors . warn ;
62
61
} else if ( this . _strength <= 80 ) {
63
- return Colors [ Colors . accent ] ;
62
+ return Colors . accent ;
64
63
} else {
65
- return Colors [ Colors . primary ] ;
64
+ return Colors . primary ;
66
65
}
67
66
}
68
67
@@ -83,11 +82,10 @@ export class PasswordStrengthComponent implements OnInit, OnChanges {
83
82
}
84
83
85
84
private _containAtLeastOneSpecialChar ( ) : boolean {
86
- return RegExp ( / ^ (? = .* ?[ # ? ! @ $ % ^ & * - ] ) / ) . test ( this . password ) ;
85
+ return RegExp ( / ^ (? = .* ?[ " ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \] ^ _ ` { | } ~ " ] ) / ) . test ( this . password ) ;
87
86
}
88
87
89
- private _calculatePasswordStrength ( ) {
90
- console . log ( 'on _calculatePasswordStrength()' ) ;
88
+ calculatePasswordStrength ( ) {
91
89
const requirements : Array < boolean > = [ ] ;
92
90
const unit = 100 / 5 ;
93
91
0 commit comments