Skip to content

Commit b832d37

Browse files
committed
fix: properly count and display missed placeholders
1 parent 1090ceb commit b832d37

File tree

4 files changed

+90
-6
lines changed

4 files changed

+90
-6
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { extractPlaceholders } from './extract-placeholders';
2+
3+
describe('extractPlaceholders', () => {
4+
it('should extract one placeholder', () => {
5+
const res = extractPlaceholders('{$PLACEHOLDER}');
6+
expect(res).toEqual(['{$PLACEHOLDER}']);
7+
});
8+
9+
it('should extract one placeholder surrounded with text', () => {
10+
const res = extractPlaceholders('Prev text {$PLACEHOLDER}after text');
11+
expect(res).toEqual(['{$PLACEHOLDER}']);
12+
});
13+
14+
it('should extract multiple placeholders', () => {
15+
const res = extractPlaceholders('{$PLACEHOLDER1}{$PLACEHOLDER2}');
16+
expect(res).toEqual(['{$PLACEHOLDER1}', '{$PLACEHOLDER2}']);
17+
});
18+
19+
it('should extract multiple placeholders surrounded with text', () => {
20+
const res = extractPlaceholders('Prev text {$PLACEHOLDER1} middle text{$PLACEHOLDER2}after text');
21+
expect(res).toEqual(['{$PLACEHOLDER1}', '{$PLACEHOLDER2}']);
22+
});
23+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { PlaceholdersValidatorDirective } from './placeholders-validator.directive';
2+
3+
describe('PlaceholdersValidatorDirective', () => {
4+
let directive: PlaceholdersValidatorDirective;
5+
6+
beforeEach(() => {
7+
directive = new PlaceholdersValidatorDirective();
8+
});
9+
10+
it('should create', () => {
11+
expect(directive).toBeTruthy();
12+
});
13+
14+
it('should be valid without original and value placeholders', () => {
15+
directive.originalPlaceholders = [];
16+
const err = directive.validate({value: 'No placeholder text'} as any);
17+
expect(err).toEqual({placeholders: null});
18+
});
19+
20+
it('should be valid without original placeholder', () => {
21+
directive.originalPlaceholders = [];
22+
const err = directive.validate({value: '{$PLACEHOLDER}'} as any);
23+
expect(err).toEqual({placeholders: null});
24+
});
25+
26+
it('should be valid when placeholders are equal', () => {
27+
directive.originalPlaceholders = ['{$PLACEHOLDER}'];
28+
const err = directive.validate({value: '{$PLACEHOLDER}'} as any);
29+
expect(err).toEqual({placeholders: null});
30+
});
31+
32+
it('should be invalid when placeholder are different', () => {
33+
directive.originalPlaceholders = ['{$PLACEHOLDER_ORIG}'];
34+
const err = directive.validate({value: '{$PLACEHOLDER_VAL}'} as any);
35+
expect(err).toEqual({placeholders: ['{$PLACEHOLDER_ORIG}']});
36+
});
37+
38+
it('should be invalid when multiple placeholder are different', () => {
39+
directive.originalPlaceholders = ['{$PLACEHOLDER_1}', '{$PLACEHOLDER_2}', '{$PLACEHOLDER_3}', '{$PLACEHOLDER_4}'];
40+
const err = directive.validate({value: '{$PLACEHOLDER_1}, {$PLACEHOLDER_3}'} as any);
41+
expect(err).toEqual({placeholders: ['{$PLACEHOLDER_2}', '{$PLACEHOLDER_4}']});
42+
});
43+
44+
it('should be invalid when similar placeholder in original have another count in content', () => {
45+
directive.originalPlaceholders = ['{$PLACEHOLDER_ORIG}', '{$PLACEHOLDER_ORIG}', '{$PLACEHOLDER_ORIG}'];
46+
const err = directive.validate({value: '{$PLACEHOLDER_ORIG}{$PLACEHOLDER_ORIG}'} as any);
47+
expect(err).toEqual({placeholders: ['{$PLACEHOLDER_ORIG}']});
48+
});
49+
50+
it('should be invalid when multiple similar placeholder in original have another count in content', () => {
51+
directive.originalPlaceholders = ['{$PLACEHOLDER_ORIG}', '{$PLACEHOLDER_ORIG_2}', '{$PLACEHOLDER_ORIG}', '{$PLACEHOLDER_ORIG_2}', '{$PLACEHOLDER_ORIG_3}', '{$PLACEHOLDER_ORIG}'];
52+
const err = directive.validate({value: '{$PLACEHOLDER_ORIG}{$PLACEHOLDER_ORIG}{$PLACEHOLDER_ORIG_2}{$PLACEHOLDER_ORIG_3}'} as any);
53+
expect(err).toEqual({placeholders: ['{$PLACEHOLDER_ORIG_2}', '{$PLACEHOLDER_ORIG}']});
54+
});
55+
});

projects/ngxe/src/app/placeholders/placeholders-validator.directive.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ export class PlaceholdersValidatorDirective implements Validator {
1414

1515
validate(control: AbstractControl): ValidationErrors | null {
1616
const placeholders = extractPlaceholders(control.value);
17-
const changes = this
18-
.originalPlaceholders
19-
.filter(op => placeholders.indexOf(op) === -1);
17+
const misses: string[] = [];
18+
for (const original of this.originalPlaceholders) {
19+
const ind = placeholders.indexOf(original);
20+
if (ind !== -1) {
21+
placeholders.splice(ind, 1);
22+
} else {
23+
misses.push(original);
24+
}
25+
}
2026
return {
21-
placeholders: changes.length > 0 ? changes : null,
27+
placeholders: misses.length > 0 ? misses : null,
2228
};
2329
}
2430
}

projects/ngxe/src/app/table/table.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ <h5 class="row-title">Suggestions</h5>
8383
💡 {{ suggestion }}
8484
</button>
8585
</div>
86-
<div *ngIf="model.hasError('placeholders')" class="validation">
86+
<div *ngIf="model.getError('placeholders'); let error" class="validation">
8787
<h5 class="row-title">Validation</h5>
88-
🚧 Placeholders not found: {{ row.placeholders.join(', ') }}.
88+
🚧 Placeholders not found: {{ error.join(', ') }}.
8989
</div>
9090
</ng-container>
9191
<ng-container *ngIf="row.type === 'deleted'">

0 commit comments

Comments
 (0)