Skip to content

Commit ed008a7

Browse files
committed
feat(ngx-material-password-strength): added the main component as progress bar
1 parent b3ef465 commit ed008a7

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<mat-progress-bar mode="determinate"
2+
[color]="color"
3+
[value]="strength">
4+
</mat-progress-bar>

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

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
//
3+
// import { PasswordStrengthComponent } from './password-strength.component';
4+
//
5+
// describe('PasswordStrengthComponent', () => {
6+
// let component: PasswordStrengthComponent;
7+
// let fixture: ComponentFixture<PasswordStrengthComponent>;
8+
//
9+
// beforeEach(async(() => {
10+
// TestBed.configureTestingModule({
11+
// declarations: [ PasswordStrengthComponent ]
12+
// })
13+
// .compileComponents();
14+
// }));
15+
//
16+
// beforeEach(() => {
17+
// fixture = TestBed.createComponent(PasswordStrengthComponent);
18+
// component = fixture.componentInstance;
19+
// fixture.detectChanges();
20+
// });
21+
//
22+
// it('should create', () => {
23+
// expect(component).toBeTruthy();
24+
// });
25+
// });
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import {Component, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
2+
3+
export enum Colors {
4+
primary,
5+
accent,
6+
warn
7+
}
8+
9+
@Component({
10+
selector: 'ngx-material-password-strength',
11+
templateUrl: './password-strength.component.html',
12+
styleUrls: ['./password-strength.component.scss']
13+
})
14+
export class PasswordStrengthComponent implements OnInit, OnChanges {
15+
16+
@Input()
17+
password: string;
18+
19+
@Input()
20+
externalError: boolean;
21+
22+
private _strength: number;
23+
24+
private _color: string;
25+
26+
constructor() {
27+
}
28+
29+
ngOnInit(): void {
30+
}
31+
32+
ngOnChanges(changes: SimpleChanges): void {
33+
console.log('on change: ', changes);
34+
if (changes.externalError && changes.externalError.firstChange) {
35+
this._color = Colors[Colors.primary];
36+
return;
37+
}
38+
if (changes.externalError && changes.externalError.currentValue) {
39+
this._color = Colors[Colors.warn];
40+
return;
41+
}
42+
this.password && this.password.length > 0 ?
43+
this._calculatePasswordStrength() : this._strength = 0;
44+
}
45+
46+
47+
get strength(): number {
48+
// console.log('strength = ', this._strength);
49+
return this._strength;
50+
}
51+
52+
get color(): string {
53+
54+
if (this._strength <= 20) {
55+
return Colors[Colors.warn];
56+
} else if (this._strength <= 80) {
57+
return Colors[Colors.accent];
58+
} else {
59+
return Colors[Colors.primary];
60+
}
61+
}
62+
63+
private _containAtLeastEightChars(): boolean {
64+
return this.password.length >= 8;
65+
}
66+
67+
private _containAtLeastOneLowerCaseLetter(): boolean {
68+
return RegExp(/^(?=.*?[a-z])/).test(this.password);
69+
}
70+
71+
private _containAtLeastOneUpperCaseLetter(): boolean {
72+
return RegExp(/^(?=.*?[A-Z])/).test(this.password);
73+
}
74+
75+
private _containAtLeastOneDigit(): boolean {
76+
return RegExp(/^(?=.*?[0-9])/).test(this.password);
77+
}
78+
79+
private _containAtLeastOneSpecialChar(): boolean {
80+
return RegExp(/^(?=.*?[#?!@$%^&*-])/).test(this.password);
81+
}
82+
83+
private _calculatePasswordStrength() {
84+
console.log('on _calculatePasswordStrength()');
85+
const requirements: Array<boolean> = [];
86+
const unit = 100 / 5;
87+
88+
89+
requirements.push(
90+
this._containAtLeastEightChars(),
91+
this._containAtLeastOneLowerCaseLetter(),
92+
this._containAtLeastOneUpperCaseLetter(),
93+
this._containAtLeastOneDigit(),
94+
this._containAtLeastOneSpecialChar());
95+
96+
console.log('requirements = ', requirements);
97+
98+
this._strength = requirements.filter(v => v).length * unit;
99+
100+
console.log('strength = ', this._strength);
101+
}
102+
}

0 commit comments

Comments
 (0)