Refactoring
This commit is contained in:
parent
484454ead0
commit
d701eefc5b
|
@ -17,21 +17,6 @@ public struct ULID: Hashable, Equatable, CustomStringConvertible {
|
|||
self.ulid = ulid
|
||||
}
|
||||
|
||||
public init(timestamp: Date = Date()) {
|
||||
withUnsafeMutableBytes(of: &ulid) { (buffer) in
|
||||
var millisec = UInt64(timestamp.timeIntervalSince1970 * 1000.0).bigEndian
|
||||
withUnsafeBytes(of: &millisec) {
|
||||
for i in 0 ..< 6 {
|
||||
buffer[i] = $0[2 + i]
|
||||
}
|
||||
}
|
||||
let range = UInt8.min ... .max
|
||||
for i in 6 ..< 16 {
|
||||
buffer[i] = UInt8.random(in: range)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public init?(ulidData data: Data) {
|
||||
guard data.count == 16 else {
|
||||
return nil
|
||||
|
@ -48,15 +33,18 @@ public struct ULID: Hashable, Equatable, CustomStringConvertible {
|
|||
self.init(ulidData: data.dropFirst(4))
|
||||
}
|
||||
|
||||
public var timestamp: Date {
|
||||
return withUnsafeBytes(of: ulid) { (buffer) in
|
||||
var millisec: UInt64 = 0
|
||||
withUnsafeMutableBytes(of: &millisec) {
|
||||
public init(timestamp: Date = Date()) {
|
||||
withUnsafeMutableBytes(of: &ulid) { (buffer) in
|
||||
var millisec = UInt64(timestamp.timeIntervalSince1970 * 1000.0).bigEndian
|
||||
withUnsafeBytes(of: &millisec) {
|
||||
for i in 0 ..< 6 {
|
||||
$0[2 + i] = buffer[i]
|
||||
buffer[i] = $0[2 + i]
|
||||
}
|
||||
}
|
||||
return Date(timeIntervalSince1970: TimeInterval(millisec.bigEndian) / 1000.0)
|
||||
let range = UInt8.min ... .max
|
||||
for i in 6 ..< 16 {
|
||||
buffer[i] = UInt8.random(in: range)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,6 +58,18 @@ public struct ULID: Hashable, Equatable, CustomStringConvertible {
|
|||
return String((Data(count: 4) + ulidData).base32EncodedString().dropFirst(6))
|
||||
}
|
||||
|
||||
public var timestamp: Date {
|
||||
return withUnsafeBytes(of: ulid) { (buffer) in
|
||||
var millisec: UInt64 = 0
|
||||
withUnsafeMutableBytes(of: &millisec) {
|
||||
for i in 0 ..< 6 {
|
||||
$0[2 + i] = buffer[i]
|
||||
}
|
||||
}
|
||||
return Date(timeIntervalSince1970: TimeInterval(millisec.bigEndian) / 1000.0)
|
||||
}
|
||||
}
|
||||
|
||||
public func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(ulid.0)
|
||||
hasher.combine(ulid.1)
|
||||
|
|
|
@ -37,6 +37,8 @@ final class ULIDTests: XCTestCase {
|
|||
let ulid1 = ULID(timestamp: timestamp)
|
||||
let ulid2 = ULID(timestamp: timestamp)
|
||||
|
||||
XCTAssertEqual(ulid1.timestamp, ulid2.timestamp)
|
||||
|
||||
XCTAssertFalse(
|
||||
ulid1.ulid.6 == ulid2.ulid.6 &&
|
||||
ulid1.ulid.7 == ulid2.ulid.7 &&
|
||||
|
@ -89,7 +91,7 @@ final class ULIDTests: XCTestCase {
|
|||
XCTAssertEqual(ulid1.hashValue, ulid2.hashValue)
|
||||
}
|
||||
|
||||
func testEquatable() {
|
||||
func testEquatable1() {
|
||||
let timestamp = Date(timeIntervalSince1970: 1547213173.513)
|
||||
|
||||
let ulid1 = ULID(timestamp: timestamp)
|
||||
|
@ -98,6 +100,15 @@ final class ULIDTests: XCTestCase {
|
|||
XCTAssertEqual(ulid1, ulid2)
|
||||
}
|
||||
|
||||
func testEquatable2() {
|
||||
let timestamp = Date(timeIntervalSince1970: 1547213173.513)
|
||||
|
||||
let ulid1 = ULID(timestamp: timestamp)
|
||||
let ulid2 = ULID(timestamp: timestamp)
|
||||
|
||||
XCTAssertNotEqual(ulid1, ulid2)
|
||||
}
|
||||
|
||||
func testCustomStringConvertible() {
|
||||
let ulid = ULID()
|
||||
XCTAssertEqual(ulid.ulidString, ulid.description)
|
||||
|
|
Loading…
Reference in New Issue