Skip to content
This repository was archived by the owner on Oct 20, 2021. It is now read-only.

Commit 2f09ff3

Browse files
feat(rules): Added Punct rule
1 parent 496d3d0 commit 2f09ff3

File tree

8 files changed

+97
-1
lines changed

8 files changed

+97
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
119119
- [Positive](docs/positive.md)
120120
- [PrimeNumber](docs/prime-number.md)
121121
- [Prnt](docs/prnt.md)
122+
- [Punct](docs/punct.md)
122123
- [RegexInstance](docs/regex-instance.md)
123124
- [RegexType](docs/regex-type.md)
124125
- [Regex](docs/regex.md)

awesome-validator.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/punct.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Punct
2+
3+
Validates if the given input is a punct.
4+
5+
Valid values:
6+
7+
```js
8+
validator.punct().validate('.');
9+
validator.punct().validate(',;:');
10+
validator.punct().validate('-@#$*');
11+
validator.punct().validate('!%^&');
12+
validator.punct().validate('()[]{}');
13+
```
14+
15+
Invalid values:
16+
17+
```js
18+
validator.punct().validate('');
19+
validator.punct().validate(null);
20+
validator.punct().validate(undefined);
21+
validator.punct().validate('16-50');
22+
validator.punct().validate('a');
23+
validator.punct().validate(' ');
24+
validator.punct().validate('Foo');
25+
validator.punct().validate('12.1');
26+
validator.punct().validate('-12');
27+
validator.punct().validate(-12);
28+
validator.punct().validate('( )_{}');
29+
```

src/rules/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export * from './pis';
4040
export * from './positive';
4141
export * from './prime-number';
4242
export * from './prnt';
43+
export * from './punct';
4344
export * from './regex-instance';
4445
export * from './regex-type';
4546
export * from './regex';

src/rules/punct.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { AbstractFilter } from './abstract-filter';
2+
import { Regex } from './regex';
3+
4+
export class Punct extends AbstractFilter {
5+
6+
/**
7+
* Validate Clean.
8+
*/
9+
public validateClean(input: string): boolean {
10+
return new Regex(/^[\u0021-\u002F\u003A-\u0040\u005B-\u0060\u007B-\u007E]+$/g).validate(input);
11+
}
12+
}

src/validator.ts

+7
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,13 @@ export class Validator extends rules.AllOf {
299299
return this.addRule(new rules.Prnt());
300300
}
301301

302+
/**
303+
* Punct.
304+
*/
305+
public punct(): this {
306+
return this.addRule(new rules.Punct());
307+
}
308+
302309
/**
303310
* Regex Instance.
304311
*/

test/rules/punct.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { assert } from 'chai';
2+
3+
import { AbstractRule } from '../../src/rules/abstract-rule';
4+
import { Punct } from '../../src/rules/punct';
5+
6+
describe('Punct', () => {
7+
8+
let punct: Punct;
9+
10+
beforeEach(() => {
11+
punct = new Punct();
12+
});
13+
14+
it('is rule', () => {
15+
assert.instanceOf(punct, AbstractRule);
16+
});
17+
18+
it('values is valid with additional characters', () => {
19+
assert.isTrue(new Punct('abc123 ').validate('!@#$%^&*(){} abc 123'));
20+
assert.isTrue(new Punct('abc123 \t\n').validate('[]?+=/\\-_|\',<>. \t \n abc 123'));
21+
});
22+
23+
it('values is valid', () => {
24+
assert.isTrue(punct.validate('.'));
25+
assert.isTrue(punct.validate(',;:'));
26+
assert.isTrue(punct.validate('-@#$*'));
27+
assert.isTrue(punct.validate('!%^&'));
28+
assert.isTrue(punct.validate('()[]{}'));
29+
});
30+
31+
it('values is not valid', () => {
32+
assert.isFalse(punct.validate(''));
33+
assert.isFalse(punct.validate(null));
34+
assert.isFalse(punct.validate(undefined));
35+
assert.isFalse(punct.validate('16-50'));
36+
assert.isFalse(punct.validate('a'));
37+
assert.isFalse(punct.validate(' '));
38+
assert.isFalse(punct.validate('Foo'));
39+
assert.isFalse(punct.validate('12.1'));
40+
assert.isFalse(punct.validate('-12'));
41+
assert.isFalse(punct.validate(-12));
42+
assert.isFalse(punct.validate('( )_{}'));
43+
});
44+
45+
});

test/validator.ts

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ describe('Validator', () => {
5656
assert.instanceOf(validator.positive(), Validator);
5757
assert.instanceOf(validator.primeNumber(), Validator);
5858
assert.instanceOf(validator.prnt(), Validator);
59+
assert.instanceOf(validator.punct(), Validator);
5960
assert.instanceOf(validator.regexInstance(), Validator);
6061
assert.instanceOf(validator.regexType(), Validator);
6162
assert.instanceOf(validator.regex(/foo/), Validator);

0 commit comments

Comments
 (0)