Simplefied small parts of creating the random part

This commit is contained in:
mufumade 2021-05-13 18:00:43 +02:00
parent 16b31986ca
commit b0b7d99443
1 changed files with 4 additions and 4 deletions

View File

@ -42,8 +42,8 @@ public struct ULID: Hashable, Equatable, Comparable, CustomStringConvertible {
/// - uuid: Data representation of the random part of the ULID
/// - Returns: **NIL** if the ULID is less than 80 bits or 10 bytes in size
public init?(timestamp: Date = Date(), uuid: Data){
let randomDataInBits = 80
guard uuid.count >= randomDataInBits / 8 else { return nil }
let randomDataInBytes = 10
guard uuid.count >= randomDataInBytes else { return nil }
withUnsafeMutableBytes(of: &ulid) { (buffer) in
var i = 0
@ -55,8 +55,8 @@ public struct ULID: Hashable, Equatable, Comparable, CustomStringConvertible {
}
}
var randomPart:Data = Data()
if uuid.count > randomDataInBits / 8{
randomPart = uuid.prefix(randomDataInBits)
if uuid.count > randomDataInBytes{
randomPart = uuid.prefix(randomDataInBytes)
}else{
randomPart = uuid
}