Skip to content

Commit f84e47e

Browse files
committed
feat: added is_half_width validator
1 parent 15dcb04 commit f84e47e

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Validator | Description
7272
**is_fqdn(str, options)** | check if the string is a fully qualified domain name (e.g. domain.com).<br/><br/>`options` is an dictionary which defaults to `{ "require_tld": True, "allow_underscores": False, "allow_trailing_dot": False, "allow_numeric_tld": False, "allow_wildcard": False }`. If `allow_wildcard` is set to *True*, the validator will allow domain starting with `*.` (e.g. `*.example.com` or `*.shop.example.com`).
7373
**is_freight_container_id(str)** | alias for `isISO6346`, check if the string is a valid [ISO 6346](https://en.wikipedia.org/wiki/ISO_6346) shipping container identification.
7474
**is_full_width(str)** | check if the string contains any full-width chars.
75+
**is_half_width(str)** | check if the string contains any half-width chars.
7576
**is_hash(str, algorithm)** | check if the string is a hash of type algorithm.<br/><br/>Algorithm is one of `['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']`
7677
**is_hexadecimal(str)** | check if the string is a hexadecimal number.
7778
**is_hsl(str)** | check if the string is an HSL (hue, saturation, lightness, optional alpha) color based on [CSS Colors Level 4 specification](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value).<br/><br/>Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`).

pyvalidator/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from pyvalidator.is_float import is_float
2525
from pyvalidator.is_fqdn import is_fqdn
2626
from pyvalidator.is_full_width import is_full_width
27+
from pyvalidator.is_half_width import is_half_width
2728
from pyvalidator.is_hash import is_hash
2829
from pyvalidator.is_hexadecimal import is_hexadecimal
2930
from pyvalidator.is_hsl import is_hsl

pyvalidator/is_half_width.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from .utils.assert_string import assert_string
2+
from .utils.Classes.RegEx import RegEx
3+
4+
5+
def is_half_width(input: str) -> bool:
6+
input = assert_string(input)
7+
8+
half_width_pattern = RegEx(r'[\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]')
9+
10+
return half_width_pattern.match(input)

test/test_is_half_width.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import unittest
2+
from pyvalidator.is_half_width import is_half_width
3+
from . import print_test_ok
4+
5+
class TestIsHalfWidth(unittest.TestCase):
6+
7+
def test_valid_half_width(self):
8+
for i in [
9+
'!"#$%&()<>/+=-_? ~^|.,@`{}[]',
10+
'l-btn_02--active',
11+
'abc123い',
12+
'カタカナ゙ᆲ←',
13+
'!"#$%&()<>/+=-_? ~^|.,@`{}[]'
14+
]:
15+
self.assertTrue(is_half_width(i))
16+
print_test_ok()
17+
18+
def test_invalid_half_width(self):
19+
for i in [
20+
'3ー0 a@com'
21+
'あいうえお',
22+
'0011',
23+
'Ğood=Parts'
24+
]:
25+
self.assertFalse(is_half_width(i))
26+
print_test_ok()
27+
28+
if __name__ == '__main__':
29+
unittest.main()

0 commit comments

Comments
 (0)