diff --git a/Day.podspec b/Day.podspec index 768bd15..027f88b 100644 --- a/Day.podspec +++ b/Day.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "Day" - s.version = "1.1" + s.version = "1.1.1" s.summary = "Mini lib to represent a Day" s.description = "This is the mini library to represent a Day value with serialization functionality" s.homepage = "https://github.com/numen31337/Day" diff --git a/Day.xcodeproj/project.pbxproj b/Day.xcodeproj/project.pbxproj index f6c0f85..714ab5e 100644 --- a/Day.xcodeproj/project.pbxproj +++ b/Day.xcodeproj/project.pbxproj @@ -27,6 +27,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + E90301F221B69655002FB724 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = SOURCE_ROOT; }; E9D466311FC5A65600F2BEC3 /* Day.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Day.framework; sourceTree = BUILT_PRODUCTS_DIR; }; E9D466341FC5A65600F2BEC3 /* Day.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Day.h; sourceTree = ""; }; E9D466351FC5A65600F2BEC3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -87,6 +88,7 @@ E9D466511FC5A69900F2BEC3 /* SwiftlyLRU.swift */, E9D466341FC5A65600F2BEC3 /* Day.h */, E9D466351FC5A65600F2BEC3 /* Info.plist */, + E90301F221B69655002FB724 /* README.md */, ); path = Day; sourceTree = ""; diff --git a/Day.xcodeproj/xcshareddata/xcschemes/Day.xcscheme b/Day.xcodeproj/xcshareddata/xcschemes/Day.xcscheme new file mode 100644 index 0000000..ba3f0b5 --- /dev/null +++ b/Day.xcodeproj/xcshareddata/xcschemes/Day.xcscheme @@ -0,0 +1,100 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Day/Day.swift b/Day/Day.swift index 800e560..cc6be99 100644 --- a/Day/Day.swift +++ b/Day/Day.swift @@ -22,11 +22,6 @@ public struct Day { public init(_ day: Day) { rawValue = day.rawValue } - - public init?(_ string: String) { - guard let day = DayString(string)?.day else { return nil } - rawValue = day.rawValue - } } ///MARK: Helpers @@ -67,10 +62,6 @@ extension Day: Comparable { } } -extension Day: CustomStringConvertible { - public var description: String { return DayString(self).stringValue } -} - extension Day: Hashable { public var hashValue: Int { return rawValue.hashValue } } diff --git a/Day/StringSerializableDate.swift b/Day/StringSerializableDate.swift index 5db638a..e9303d5 100644 --- a/Day/StringSerializableDate.swift +++ b/Day/StringSerializableDate.swift @@ -7,7 +7,7 @@ import Foundation -protocol StringSerializableDate: CustomStringConvertible { +protocol StringSerializableDate { var stringValue: String { get } static var formatter: DateFormatter { get } @@ -18,8 +18,6 @@ protocol StringSerializableDate: CustomStringConvertible { } extension StringSerializableDate { - var description: String { return stringValue } - init(_ date: Date) { let parsedString = Self.formatter.string(from: date) assert(Self.isCorrectFormat(parsedString)) @@ -94,3 +92,30 @@ struct DayTimeString: StringSerializableDate { return true } } + +extension Date { + public static func date(fromSerializedDateAndTimeString string: String) -> Date? { + return DayTimeString(string)?.date + } + + public static func date(fromSerializedDay string: String) -> Date? { + return DayString(string)?.day.rawValue + } + + public var serializedDayString: String { return DayString(self).stringValue } + + public var serializedDayAndTimeString: String { return DayTimeString(self).stringValue } +} + +extension Day { + public init?(_ string: String) { + guard let day = DayString(string)?.day else { return nil } + rawValue = day.rawValue + } + + public var serializedDayString: String { return DayString(self).stringValue } +} + +extension Day: CustomStringConvertible { + public var description: String { return serializedDayString } +} diff --git a/DayTests/StringSerializableDate.swift b/DayTests/StringSerializableDate.swift index 6926076..85c719d 100644 --- a/DayTests/StringSerializableDate.swift +++ b/DayTests/StringSerializableDate.swift @@ -7,7 +7,7 @@ // import XCTest -@testable import Day +import Day class DayTests: XCTestCase { let formatter: DateFormatter = { @@ -28,35 +28,50 @@ class DayTests: XCTestCase { func testDayFromString() { let correctResultDate = formatter.date(from: "2018.11.04 00:00") - let dayString = DayString("2018.11.04")!.day.rawValue + let dayString = Day("2018.11.04")!.rawValue + let dateString = Date.date(fromSerializedDay: "2018.11.04") + XCTAssertEqual(correctResultDate, dayString) + XCTAssertEqual(correctResultDate, dateString) } func testDayTimeFromString() { let correctResultDate = formatter.date(from: "2018.11.04 11:10") - let dayString = DayTimeString("2018.11.04.11.10")!.date + let dayString = Date.date(fromSerializedDateAndTimeString: "2018.11.04.11.10") + XCTAssertEqual(correctResultDate, dayString) } - func testStringFromDay() { + func testDayString() { let date = formatter.date(from: "2018.11.04 11:10")! - let string = DayString(date).stringValue - XCTAssertEqual(string, "2018.11.04") + let stringFromDay = Day(date).serializedDayString + let stringFromDate = date.serializedDayString + + XCTAssertEqual(stringFromDay, "2018.11.04") + XCTAssertEqual(stringFromDate, "2018.11.04") } - + func testStringFromDayTime() { let date = formatter.date(from: "2018.11.04 11:10")! - let string = DayTimeString(date).stringValue + let string = date.serializedDayAndTimeString XCTAssertEqual(string, "2018.11.04.11.10") } - - func testDayStringFail() { - let failedValue = DayTimeString("2018+11+04") + + func testDayStringInitFail() { + let failedValue = Day("2018+11+04") XCTAssertNil(failedValue) } + + func testDateStringInitFail() { + let failedValue1 = Date.date(fromSerializedDateAndTimeString: "2018.11.04+11:10") + let failedValue2 = Date.date(fromSerializedDay: "2018+11+04") + + XCTAssertNil(failedValue1) + XCTAssertNil(failedValue2) + } - func testDayTimeStringFail() { - let failedValue = DayTimeString("2018.11.04+11:10") - XCTAssertNil(failedValue) + func testDayDescription() { + XCTAssertEqual("\(String(describing: Day("2018.11.04")!))", "2018.11.04") + XCTAssertEqual(Day("2018.11.04")!.description, "2018.11.04") } } diff --git a/README.md b/README.md index cd6b436..b48dd9f 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ let day = Day(date) //Day value from the date by dropping time components Serialization:
```swift -let serializedString = DayString(day).stringValue //A serialized string value +let serializedString = day.serializedDayString //A serialized string value let day = Day(serializedString) //Day value from the serialized string ```