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

Commit 70cd580

Browse files
feat(rules): Added HalfWidth rule
1 parent 04ad484 commit 70cd580

5 files changed

Lines changed: 62 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
139139
- [FunctionName](docs/function-name.md)
140140
- [FunctionType](docs/function-type.md)
141141
- [Graph](docs/graph.md)
142+
- [HalfWidth](docs/half-width.md)
142143
- [HexRgbColor](docs/hex-rgb-color.md)
143144
- [Identical](docs/identical.md)
144145
- [Image](docs/image.md)

docs/half-width.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# HalfWidth
2+
3+
Validates if the given input is half width.
4+
5+
Valid values:
6+
7+
```js
8+
validator.halfWidth().validate('!"#$%&()<>/+=-_? ~^|.,@`{}[]');
9+
validator.halfWidth().validate('l-btn_02--active');
10+
```
11+
12+
Invalid values:
13+
14+
```js
15+
validator.halfWidth().validate('あいうえお');
16+
validator.halfWidth().validate('0011');
17+
```

src/rules/half-width.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { AbstractRegex } from './abstract-regex';
2+
3+
export class HalfWidth extends AbstractRegex {
4+
5+
/**
6+
* Get pattern.
7+
*/
8+
protected getPattern(): string | RegExp {
9+
return /[\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]/;
10+
}
11+
}
12+
13+
export default HalfWidth;

src/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export * from './function-instance';
5757
export * from './function-name';
5858
export * from './function-type';
5959
export * from './graph';
60+
export * from './half-width';
6061
export * from './hex-rgb-color';
6162
export * from './identical';
6263
export * from './image';

test/rules/half-width.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { assert } from 'chai';
2+
3+
import { AbstractRule } from '../../src/rules/abstract-rule';
4+
import { HalfWidth } from '../../src/rules/half-width';
5+
6+
describe('HalfWidth', () => {
7+
8+
let halfWidth: HalfWidth;
9+
10+
beforeEach(() => {
11+
halfWidth = new HalfWidth();
12+
});
13+
14+
it('is rule', () => {
15+
assert.instanceOf(halfWidth, AbstractRule);
16+
});
17+
18+
it('values is valid', () => {
19+
assert.isTrue(halfWidth.validate('!"#$%&()<>/+=-_? ~^|.,@`{}[]'));
20+
assert.isTrue(halfWidth.validate('l-btn_02--active'));
21+
assert.isTrue(halfWidth.validate('abc123い'));
22+
assert.isTrue(halfWidth.validate('カタカナ゙ᆲ←'));
23+
});
24+
25+
it('values is not valid', () => {
26+
assert.isFalse(halfWidth.validate('あいうえお'));
27+
assert.isFalse(halfWidth.validate('0011'));
28+
});
29+
30+
});

0 commit comments

Comments
 (0)