Skip to content

Commit

Permalink
feat: added is_half_width validator
Browse files Browse the repository at this point in the history
  • Loading branch information
theteladras committed Aug 25, 2024
1 parent 15dcb04 commit f84e47e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Validator | Description
**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`).
**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.
**is_full_width(str)** | check if the string contains any full-width chars.
**is_half_width(str)** | check if the string contains any half-width chars.
**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']`
**is_hexadecimal(str)** | check if the string is a hexadecimal number.
**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)`).
Expand Down
1 change: 1 addition & 0 deletions pyvalidator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from pyvalidator.is_float import is_float
from pyvalidator.is_fqdn import is_fqdn
from pyvalidator.is_full_width import is_full_width
from pyvalidator.is_half_width import is_half_width
from pyvalidator.is_hash import is_hash
from pyvalidator.is_hexadecimal import is_hexadecimal
from pyvalidator.is_hsl import is_hsl
Expand Down
10 changes: 10 additions & 0 deletions pyvalidator/is_half_width.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .utils.assert_string import assert_string
from .utils.Classes.RegEx import RegEx


def is_half_width(input: str) -> bool:
input = assert_string(input)

half_width_pattern = RegEx(r'[\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]')

return half_width_pattern.match(input)
29 changes: 29 additions & 0 deletions test/test_is_half_width.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import unittest
from pyvalidator.is_half_width import is_half_width
from . import print_test_ok

class TestIsHalfWidth(unittest.TestCase):

def test_valid_half_width(self):
for i in [
'!"#$%&()<>/+=-_? ~^|.,@`{}[]',
'l-btn_02--active',
'abc123い',
'カタカナ゙ᆲ←',
'!"#$%&()<>/+=-_? ~^|.,@`{}[]'
]:
self.assertTrue(is_half_width(i))
print_test_ok()

def test_invalid_half_width(self):
for i in [
'3ー0 a@com'
'あいうえお',
'0011',
'Ğood=Parts'
]:
self.assertFalse(is_half_width(i))
print_test_ok()

if __name__ == '__main__':
unittest.main()

0 comments on commit f84e47e

Please sign in to comment.