Files
saladeaula.digital/layercake/layercake/strutils.py
2025-05-21 15:48:59 -03:00

19 lines
467 B
Python

import hashlib
def first_word(s: str) -> str:
"""Returns the first word from a string."""
return s.split(' ', 1)[0]
def truncate_str(s: str, maxlen: int = 30) -> str:
"""Truncates a string to fit within a maximum length, adding '...' if truncated."""
if len(s) <= maxlen:
return s
return s[: maxlen - 3] + '...'
def md5_hash(s: str) -> str:
"""Computes the MD5 hash of a string."""
return hashlib.md5(s.encode()).hexdigest()