Skip to content

Commit

Permalink
Merge pull request #8 from sblondon/master
Browse files Browse the repository at this point in the history
Factorize conversion to slug
  • Loading branch information
djmitche committed Sep 19, 2018
2 parents 53ab244 + 7d68676 commit 066f7a8
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions slugid/slugid.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ def encode(uuid_):
Returns the given uuid.UUID object as a 22 character slug. This can be a
regular v4 slug or a "nice" slug.
"""
return base64.urlsafe_b64encode(uuid_.bytes)[:-2] # Drop '==' padding
return _convert_bytes_to_slug(uuid_.bytes)


def _convert_bytes_to_slug(bytes_):
return base64.urlsafe_b64encode(bytes_)[:-2] # Drop '==' padding


def decode(slug):
Expand All @@ -28,7 +32,7 @@ def v4():
"""
Returns a randomly generated uuid v4 compliant slug
"""
return base64.urlsafe_b64encode(uuid.uuid4().bytes)[:-2] # Drop '==' padding
return _convert_bytes_to_slug(uuid.uuid4().bytes)


def nice():
Expand All @@ -45,4 +49,4 @@ def nice():
"""
rawBytes = bytearray(uuid.uuid4().bytes)
rawBytes[0] = rawBytes[0] & 0x7f # Ensure slug starts with [A-Za-f]
return base64.urlsafe_b64encode(rawBytes)[:-2] # Drop '==' padding
return _convert_bytes_to_slug(rawBytes)

0 comments on commit 066f7a8

Please sign in to comment.