Skip to content

Commit 5f43477

Browse files
authored
Merge pull request #230 from SirZach/feature/warn-accent-threshold
feat(package): allow for custom warn and accent thresholds
2 parents b944b9b + 8957197 commit 5f43477

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ export class OtherModule {
187187
| enableSpecialCharRule | `Input() ` | `boolean` | true | whether a special char is optional
188188
| min | `Input() ` | `number` | 8 | the minimum length of the password
189189
| max | `Input() ` | `number` | 30 | the maximum length of the password
190+
| warnThreshold | `Input() ` | `number` | 21 | password strength less than this number shows the warn color
191+
| accentThreshold | `Input() ` | `number` | 81 | password strength less than this number shows the accent color
190192
| onStrengthChanged | Output() | `number` | - | emits the strength of the provided password in % e.g: 20%, 40%, 60%, 80% or 100%
191193

192194
### `<mat-password-strength-info>` used to display more information about the strength of a provided password - [see the demo examples](https://angular-material-extensions.github.io/password-strength/examples/mat-password-strength-info)

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,24 @@ describe('PasswordStrengthComponent', () => {
180180
});
181181
});
182182

183+
it('should strength at least 80 and color = accent when the password fulfills 4 criteria and accentThreshold set to 100',
184+
() => {
185+
const charsList = ['a', 'A', '9', '!', 'bcdef'];
186+
const combinations = generator.loadCombinationList(charsList, 4, 4, true);
187+
const accentThreshold = 100;
188+
189+
combinations.forEach(combination => {
190+
const isCharDuplicate = new RegExp(/^.*(.).*\1.*$/);
191+
if (!isCharDuplicate.test(combination)) {
192+
component.password = combination;
193+
component.accentThreshold = accentThreshold;
194+
component.calculatePasswordStrength();
195+
expect(component.strength).toBeGreaterThanOrEqual(80);
196+
component.strength < accentThreshold ? expect(component.color).toBe(Colors.accent) : expect(component.color).toBe(Colors.primary);
197+
}
198+
});
199+
});
200+
183201
it('should strength equal 100 and color = primary when the password fulfills all 5 criteria ',
184202
() => {
185203
const charsList = ['a', 'A', '9', '!', 'bcdef'];

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export class MatPasswordStrengthComponent implements OnInit, OnChanges {
2929
@Input() max = 30;
3030
@Input() customValidator: RegExp;
3131

32+
@Input() warnThreshold = 21;
33+
@Input() accentThreshold = 81;
34+
3235
@Output()
3336
onStrengthChanged: EventEmitter<number> = new EventEmitter();
3437

@@ -81,9 +84,9 @@ export class MatPasswordStrengthComponent implements OnInit, OnChanges {
8184

8285
get color(): ThemePalette {
8386

84-
if (this._strength <= 20) {
87+
if (this._strength < this.warnThreshold) {
8588
return Colors.warn;
86-
} else if (this._strength <= 80) {
89+
} else if (this._strength < this.accentThreshold) {
8790
return Colors.accent;
8891
} else {
8992
return Colors.primary;

0 commit comments

Comments
 (0)