Last active
February 20, 2017 15:11
-
-
Save hery/127ff82e0c760333603b01f9d61f28cb to your computer and use it in GitHub Desktop.
Valid Slack Channel Name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def valid_channel_name(name): | |
"""Channel names can only contain lowercase letters, | |
numbers, hyphens, and underscores, and must be 21 characters or less. | |
Some sample conversions from Slack to determine some heuristics: | |
1..................20abc def => 1_20abc-def | |
1..................20abc..def => 1_20abc_def | |
Heuristics: | |
1. Encode input into ASCII | |
2. Replace non-alphanumeric groups of charaters by a single _ | |
3. Replace all spaces by dashes | |
4. Truncate the resulting sring into first 21 characters | |
""" | |
result = unicodedata.normalize('NFD', name).encode('ascii', 'ignore') | |
result = re.sub("[^a-zA-Z\d\s-]+", '_', result) | |
result = result.replace(' ', '-') | |
result = result[:21].lower() | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment