Skip to content

Commit 465c244

Browse files
committed
refactor(component): enum Colors contains string and improved special chars check
1 parent 6496491 commit 465c244

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/module/component/password-strength/password-strength.component.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {Component, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
22

33
export enum Colors {
4-
primary,
5-
accent,
6-
warn
4+
primary = 'primary',
5+
accent = 'accent',
6+
warn = 'warn'
77
}
88

99
@Component({
@@ -38,31 +38,30 @@ export class PasswordStrengthComponent implements OnInit, OnChanges {
3838
ngOnChanges(changes: SimpleChanges): void {
3939
console.log('on change: ', changes);
4040
if (changes.externalError && changes.externalError.firstChange) {
41-
this._color = Colors[Colors.primary];
41+
this._color = Colors.primary;
4242
return;
4343
}
4444
if (changes.externalError && changes.externalError.currentValue) {
45-
this._color = Colors[Colors.warn];
45+
this._color = Colors.warn;
4646
return;
4747
}
4848
this.password && this.password.length > 0 ?
49-
this._calculatePasswordStrength() : this._strength = 0;
49+
this.calculatePasswordStrength() : this._strength = 0;
5050
}
5151

5252

5353
get strength(): number {
54-
// console.log('strength = ', this._strength);
55-
return this._strength;
54+
return this._strength ? this._strength : 0;
5655
}
5756

5857
get color(): string {
5958

6059
if (this._strength <= 20) {
61-
return Colors[Colors.warn];
60+
return Colors.warn;
6261
} else if (this._strength <= 80) {
63-
return Colors[Colors.accent];
62+
return Colors.accent;
6463
} else {
65-
return Colors[Colors.primary];
64+
return Colors.primary;
6665
}
6766
}
6867

@@ -83,11 +82,10 @@ export class PasswordStrengthComponent implements OnInit, OnChanges {
8382
}
8483

8584
private _containAtLeastOneSpecialChar(): boolean {
86-
return RegExp(/^(?=.*?[#?!@$%^&*-])/).test(this.password);
85+
return RegExp(/^(?=.*?[" !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"])/).test(this.password);
8786
}
8887

89-
private _calculatePasswordStrength() {
90-
console.log('on _calculatePasswordStrength()');
88+
calculatePasswordStrength() {
9189
const requirements: Array<boolean> = [];
9290
const unit = 100 / 5;
9391

0 commit comments

Comments
 (0)