1- import { Component , Input , OnChanges , OnInit , SimpleChanges } from '@angular/core' ;
1+ import { Component , EventEmitter , Input , OnChanges , OnInit , Output , SimpleChanges } from '@angular/core' ;
22
33export enum Colors {
44 primary = 'primary' ,
@@ -19,6 +19,9 @@ export class PasswordStrengthComponent implements OnInit, OnChanges {
1919 @Input ( )
2020 externalError : boolean ;
2121
22+ @Output ( )
23+ onStrengthChanged : EventEmitter < number > = new EventEmitter < number > ( ) ;
24+
2225 containAtLeastEightChars : boolean ;
2326 containAtLeastOneLowerCaseLetter : boolean ;
2427 containAtLeastOneUpperCaseLetter : boolean ;
@@ -36,7 +39,6 @@ export class PasswordStrengthComponent implements OnInit, OnChanges {
3639 }
3740
3841 ngOnChanges ( changes : SimpleChanges ) : void {
39- console . log ( 'on change: ' , changes ) ;
4042 if ( changes . externalError && changes . externalError . firstChange ) {
4143 this . _color = Colors . primary ;
4244 return ;
@@ -46,10 +48,9 @@ export class PasswordStrengthComponent implements OnInit, OnChanges {
4648 return ;
4749 }
4850 this . password && this . password . length > 0 ?
49- this . calculatePasswordStrength ( ) : this . _strength = 0 ;
51+ this . calculatePasswordStrength ( ) : this . reset ( ) ;
5052 }
5153
52-
5354 get strength ( ) : number {
5455 return this . _strength ? this . _strength : 0 ;
5556 }
@@ -66,41 +67,51 @@ export class PasswordStrengthComponent implements OnInit, OnChanges {
6667 }
6768
6869 private _containAtLeastEightChars ( ) : boolean {
69- return this . password . length >= 8 ;
70+ this . containAtLeastEightChars = this . password . length >= 8 ;
71+ return this . containAtLeastEightChars ;
7072 }
7173
7274 private _containAtLeastOneLowerCaseLetter ( ) : boolean {
73- return RegExp ( / ^ (? = .* ?[ a - z ] ) / ) . test ( this . password ) ;
75+ this . containAtLeastOneLowerCaseLetter = RegExp ( / ^ (? = .* ?[ a - z ] ) / ) . test ( this . password ) ;
76+ return this . containAtLeastOneLowerCaseLetter ;
7477 }
7578
7679 private _containAtLeastOneUpperCaseLetter ( ) : boolean {
77- return RegExp ( / ^ (? = .* ?[ A - Z ] ) / ) . test ( this . password ) ;
80+ this . containAtLeastOneUpperCaseLetter = RegExp ( / ^ (? = .* ?[ A - Z ] ) / ) . test ( this . password ) ;
81+ return this . containAtLeastOneUpperCaseLetter ;
7882 }
7983
8084 private _containAtLeastOneDigit ( ) : boolean {
81- return RegExp ( / ^ (? = .* ?[ 0 - 9 ] ) / ) . test ( this . password ) ;
85+ this . containAtLeastOneDigit = RegExp ( / ^ (? = .* ?[ 0 - 9 ] ) / ) . test ( this . password ) ;
86+ return this . containAtLeastOneDigit ;
8287 }
8388
8489 private _containAtLeastOneSpecialChar ( ) : boolean {
85- return RegExp ( / ^ (? = .* ?[ " ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \] ^ _ ` { | } ~ " ] ) / ) . test ( this . password ) ;
90+ this . containAtLeastOneSpecialChar = RegExp ( / ^ (? = .* ?[ " ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \] ^ _ ` { | } ~ " ] ) / ) . test ( this . password ) ;
91+ return this . containAtLeastOneSpecialChar ;
8692 }
8793
8894 calculatePasswordStrength ( ) {
8995 const requirements : Array < boolean > = [ ] ;
9096 const unit = 100 / 5 ;
9197
92-
9398 requirements . push (
9499 this . _containAtLeastEightChars ( ) ,
95100 this . _containAtLeastOneLowerCaseLetter ( ) ,
96101 this . _containAtLeastOneUpperCaseLetter ( ) ,
97102 this . _containAtLeastOneDigit ( ) ,
98103 this . _containAtLeastOneSpecialChar ( ) ) ;
99104
100- console . log ( 'requirements = ' , requirements ) ;
101-
102105 this . _strength = requirements . filter ( v => v ) . length * unit ;
106+ this . onStrengthChanged . emit ( this . strength ) ;
107+ }
103108
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 ;
105116 }
106117}
0 commit comments