diff --git a/Sources/TypedDate/Date+scope.swift b/Sources/TypedDate/Date+scope.swift index e6e6ad5..01e9dde 100644 --- a/Sources/TypedDate/Date+scope.swift +++ b/Sources/TypedDate/Date+scope.swift @@ -1,18 +1,21 @@ import Foundation extension Date { - @_disfavoredOverload // To test the one with a return value of TypedDate? + /// Returns a `TypedDate` representing the specified date components. + /// + /// - Parameters: + /// - keyPath: A key path to the specific date component. + /// - calendar: The calendar used for date calculations. Defaults to the current calendar. + /// - Returns: A `TypedDate` representing the specified date components. + @_disfavoredOverload public func scope( to keyPath: KeyPath<_TypedDateContext, TypedDate?>, calendar: Calendar = .current ) -> TypedDate { - _TypedDateContext(date: self, calendar: calendar)[keyPath: keyPath]! - } - - public func typedDate(calendar: Calendar = .current) -> TypedDate<(Year, Month, Day, Hour, Minute, Second, Nanosecond)> { - scope(to: \.nanosecond, calendar: calendar) + scope(to: keyPath, calendar: calendar)! } + // To test that this method does not become nil, a @_disfavoredOverload attribute is given to the public method. func scope( to keyPath: KeyPath<_TypedDateContext, TypedDate?>, calendar: Calendar = .current @@ -159,10 +162,3 @@ extension TypedDate { self.components = (Year(year), Month(month), Day(day), Hour(hour), Minute(minute), Second(second), Nanosecond(nanosecond)) } } - -//struct Hoge { -// init() { -// let hoge = TypedDate(Year(2022), Month(1)) -// hoge.erase(to: <#T##KeyPath._MonthEraseContext, (TypedDate.Type, Sendable)>#>) -// } -//} diff --git a/Sources/TypedDate/TypedDate+components.swift b/Sources/TypedDate/TypedDate+components.swift index bd224cf..e94ec38 100644 --- a/Sources/TypedDate/TypedDate+components.swift +++ b/Sources/TypedDate/TypedDate+components.swift @@ -1,126 +1,196 @@ import Foundation +/// `TypedDate` extension. This allows you to get the year component of a date. public extension TypedDate { + /// Returns the year component of `TypedDate`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func year(calendar: Calendar = .current) -> Int { calendar.dateComponents([.year], from: date).year ?? components.value } } +/// `TypedDate<(Year, Month)>` extension. This allows you to get the year and month components of a date. public extension TypedDate<(Year, Month)> { + /// Returns the year component of `TypedDate<(Year, Month)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func year(calendar: Calendar = .current) -> Int { calendar.dateComponents([.year], from: date).year ?? components.0.value } + /// Returns the month component of `TypedDate<(Year, Month)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func month(calendar: Calendar = .current) -> Int { calendar.dateComponents([.month], from: date).month ?? components.1.value } } +/// `TypedDate<(Year, Month, Day)>` extension. This allows you to get the year, month, and day components of a date. public extension TypedDate<(Year, Month, Day)> { + /// Returns the year component of `TypedDate<(Year, Month, Day)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func year(calendar: Calendar = .current) -> Int { calendar.dateComponents([.year], from: date).year ?? components.0.value } + /// Returns the month component of `TypedDate<(Year, Month, Day)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func month(calendar: Calendar = .current) -> Int { calendar.dateComponents([.month], from: date).month ?? components.1.value } + /// Returns the day component of `TypedDate<(Year, Month, Day)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func day(calendar: Calendar = .current) -> Int { calendar.dateComponents([.day], from: date).day ?? components.2.value } } +/// `TypedDate<(Year, Month, Day, Hour)>` extension. This allows you to get the year, month, day, and hour components of a date. public extension TypedDate<(Year, Month, Day, Hour)> { + /// Returns the year component of `TypedDate<(Year, Month, Day, Hour)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func year(calendar: Calendar = .current) -> Int { calendar.dateComponents([.year], from: date).year ?? components.0.value } + /// Returns the month component of `TypedDate<(Year, Month, Day, Hour)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func month(calendar: Calendar = .current) -> Int { calendar.dateComponents([.month], from: date).month ?? components.1.value } + /// Returns the day component of `TypedDate<(Year, Month, Day, Hour)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func day(calendar: Calendar = .current) -> Int { calendar.dateComponents([.day], from: date).day ?? components.2.value } + /// Returns the hour component of `TypedDate<(Year, Month, Day, Hour)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func hour(calendar: Calendar = .current) -> Int { calendar.dateComponents([.hour], from: date).hour ?? components.3.value } } +/// `TypedDate<(Year, Month, Day, Hour, Minute)>` extension. This allows you to get the year, month, day, hour, and minute components of a date. public extension TypedDate<(Year, Month, Day, Hour, Minute)> { + /// Returns the year component of `TypedDate<(Year, Month, Day, Hour, Minute)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func year(calendar: Calendar = .current) -> Int { calendar.dateComponents([.year], from: date).year ?? components.0.value } + /// Returns the month component of `TypedDate<(Year, Month, Day, Hour, Minute)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func month(calendar: Calendar = .current) -> Int { calendar.dateComponents([.month], from: date).month ?? components.1.value } + /// Returns the day component of `TypedDate<(Year, Month, Day, Hour, Minute)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func day(calendar: Calendar = .current) -> Int { calendar.dateComponents([.day], from: date).day ?? components.2.value } + /// Returns the hour component of `TypedDate<(Year, Month, Day, Hour, Minute)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func hour(calendar: Calendar = .current) -> Int { calendar.dateComponents([.hour], from: date).hour ?? components.3.value } + /// Returns the minute component of `TypedDate<(Year, Month, Day, Hour, Minute)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func minute(calendar: Calendar = .current) -> Int { calendar.dateComponents([.minute], from: date).minute ?? components.4.value } } +/// `TypedDate<(Year, Month, Day, Hour, Minute, Second)>` extension. This allows you to get the year, month, day, hour, minute, and second components of a date. public extension TypedDate<(Year, Month, Day, Hour, Minute, Second)> { + /// Returns the year component of `TypedDate<(Year, Month, Day, Hour, Minute, Second)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func year(calendar: Calendar = .current) -> Int { calendar.dateComponents([.year], from: date).year ?? components.0.value } + /// Returns the month component of `TypedDate<(Year, Month, Day, Hour, Minute, Second)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func month(calendar: Calendar = .current) -> Int { calendar.dateComponents([.month], from: date).month ?? components.1.value } + /// Returns the day component of `TypedDate<(Year, Month, Day, Hour, Minute, Second)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func day(calendar: Calendar = .current) -> Int { calendar.dateComponents([.day], from: date).day ?? components.2.value } + /// Returns the hour component of `TypedDate<(Year, Month, Day, Hour, Minute, Second)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func hour(calendar: Calendar = .current) -> Int { calendar.dateComponents([.hour], from: date).hour ?? components.3.value } + /// Returns the minute component of `TypedDate<(Year, Month, Day, Hour, Minute, Second)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. func minute(calendar: Calendar = .current) -> Int { calendar.dateComponents([.minute], from: date).minute ?? components.4.value } + /// Returns the second component of `TypedDate<(Year, Month, Day, Hour, Minute, Second)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: The second component as an Int. func second(calendar: Calendar = .current) -> Int { - calendar.dateComponents([.second], from: date).minute ?? components.5.value + calendar.dateComponents([.second], from: date).second ?? components.5.value } } public extension TypedDate<(Year, Month, Day, Hour, Minute, Second, Nanosecond)> { + /// Returns the year component of `TypedDate<(Year, Month, Day, Hour, Minute, Second)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Returns the year component of the date as an integer value. func year(calendar: Calendar = .current) -> Int { calendar.dateComponents([.year], from: date).year ?? components.0.value } + /// Returns the month component of `TypedDate<(Year, Month, Day, Hour, Minute, Second)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Returns the month component of the date as an integer value. func month(calendar: Calendar = .current) -> Int { calendar.dateComponents([.month], from: date).month ?? components.1.value } + /// Returns the day component of `TypedDate<(Year, Month, Day, Hour, Minute, Second)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Returns the day component of the date as an integer value. func day(calendar: Calendar = .current) -> Int { calendar.dateComponents([.day], from: date).day ?? components.2.value } + /// Returns the hour component of `TypedDate<(Year, Month, Day, Hour, Minute, Second)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Returns the hour component of the date as an integer value. func hour(calendar: Calendar = .current) -> Int { calendar.dateComponents([.hour], from: date).hour ?? components.3.value } + /// Returns the minute component of `TypedDate<(Year, Month, Day, Hour, Minute, Second)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Returns the minute component of the date as an integer value. func minute(calendar: Calendar = .current) -> Int { calendar.dateComponents([.minute], from: date).minute ?? components.4.value } + /// Returns the second component of `TypedDate<(Year, Month, Day, Hour, Minute, Second)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Returns the second component of the date as an integer value. func second(calendar: Calendar = .current) -> Int { - calendar.dateComponents([.second], from: date).minute ?? components.5.value + calendar.dateComponents([.second], from: date).second ?? components.5.value } + /// Returns the nanosecond component of `TypedDate<(Year, Month, Day, Hour, Minute, Second)>`. + /// - Parameter calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Returns the nanosecond component of the date as an integer value. func nanosecond(calendar: Calendar = .current) -> Int { calendar.dateComponents([.nanosecond], from: date).nanosecond ?? components.6.value } diff --git a/Sources/TypedDate/TypedDate+erase.swift b/Sources/TypedDate/TypedDate+erase.swift index 592ab08..222f476 100644 --- a/Sources/TypedDate/TypedDate+erase.swift +++ b/Sources/TypedDate/TypedDate+erase.swift @@ -2,6 +2,11 @@ import Foundation @EraseContext public extension TypedDate<(Year, Month)> { + /// Type erase TypedDate<(Year, Month)> to the specified type. + /// - Parameters: + /// - keyPath: Type erase up to the component of Date specified by KeyPath + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: type-erased instance func erase( to keyPath: KeyPath<_MonthEraseContext, (TypedDate.Type, T)>, calendar: Calendar = .current @@ -13,6 +18,11 @@ public extension TypedDate<(Year, Month)> { @EraseContext public extension TypedDate<(Year, Month, Day)> { + /// Type erase TypedDate<(Year, Month, Day)> to the specified type. + /// - Parameters: + /// - keyPath: Type erase up to the component of Date specified by KeyPath + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: type-erased instance func erase( to keyPath: KeyPath<_DayEraseContext, (TypedDate.Type, T)>, calendar: Calendar = .current @@ -24,6 +34,11 @@ public extension TypedDate<(Year, Month, Day)> { @EraseContext public extension TypedDate<(Year, Month, Day, Hour)> { + /// Type erase TypedDate<(Year, Month, Day, Hour)> to the specified type. + /// - Parameters: + /// - keyPath: Type erase up to the component of Date specified by KeyPath + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: type-erased instance func erase( to keyPath: KeyPath<_HourEraseContext, (TypedDate.Type, T)>, calendar: Calendar = .current @@ -35,6 +50,11 @@ public extension TypedDate<(Year, Month, Day, Hour)> { @EraseContext public extension TypedDate<(Year, Month, Day, Hour, Minute)> { + /// Type erase TypedDate<(Year, Month, Day, Hour, Minute)> to the specified type. + /// - Parameters: + /// - keyPath: Type erase up to the component of Date specified by KeyPath + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: type-erased instance func erase( to keyPath: KeyPath<_MinuteEraseContext, (TypedDate.Type, T)>, calendar: Calendar = .current @@ -46,6 +66,11 @@ public extension TypedDate<(Year, Month, Day, Hour, Minute)> { @EraseContext public extension TypedDate<(Year, Month, Day, Hour, Minute, Second)> { + /// Type erase TypedDate<(Year, Month, Day, Hour, Minute, Second)> to the specified type. + /// - Parameters: + /// - keyPath: Type erase up to the component of Date specified by KeyPath + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: type-erased instance func erase( to keyPath: KeyPath<_SecondEraseContext, (TypedDate.Type, T)>, calendar: Calendar = .current @@ -57,6 +82,11 @@ public extension TypedDate<(Year, Month, Day, Hour, Minute, Second)> { @EraseContext public extension TypedDate<(Year, Month, Day, Hour, Minute, Second, Nanosecond)> { + /// Type erase TypedDate<(Year, Month, Day, Hour, Minute, Second, Nanosecond)> to the specified type. + /// - Parameters: + /// - keyPath: Type erase up to the component of Date specified by KeyPath + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: type-erased instance func erase( to keyPath: KeyPath<_NanosecondEraseContext, (TypedDate.Type, T)>, calendar: Calendar = .current diff --git a/Sources/TypedDate/TypedDate+fill.swift b/Sources/TypedDate/TypedDate+fill.swift index b473d65..7b842d1 100644 --- a/Sources/TypedDate/TypedDate+fill.swift +++ b/Sources/TypedDate/TypedDate+fill.swift @@ -2,6 +2,13 @@ import Foundation @FillInContext public extension TypedDate { + /// Fill in missing Date components + /// + /// - Parameters: + /// - to: KeyPath for specifying components of a Date. + /// - arguments: Tuple of Components of Date to be filled. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Instance of TypedDate with more Date Components. func fill( to keyPath: KeyPath<_YearFillInContext,(U) -> T>, arguments: U, @@ -15,6 +22,13 @@ public extension TypedDate { @FillInContext public extension TypedDate<(Year, Month)> { + /// Fill in missing Date components + /// + /// - Parameters: + /// - to: KeyPath for specifying components of a Date. + /// - arguments: Tuple of Components of Date to be filled. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Instance of TypedDate with more Date Components. func fill( to keyPath: KeyPath<_MonthFillInContext,(U) -> T>, arguments: U, @@ -28,6 +42,13 @@ public extension TypedDate<(Year, Month)> { @FillInContext public extension TypedDate<(Year, Month, Day)> { + /// Fill in missing Date components + /// + /// - Parameters: + /// - to: KeyPath for specifying components of a Date. + /// - arguments: Tuple of Components of Date to be filled. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Instance of TypedDate with more Date Components. func fill( to keyPath: KeyPath<_DayFillInContext, (U) -> T>, arguments: U, @@ -41,6 +62,13 @@ public extension TypedDate<(Year, Month, Day)> { @FillInContext public extension TypedDate<(Year, Month, Day, Hour)> { + /// Fill in missing Date components + /// + /// - Parameters: + /// - to: KeyPath for specifying components of a Date. + /// - arguments: Tuple of Components of Date to be filled. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Instance of TypedDate with more Date Components. func fill( to keyPath: KeyPath<_HourFillInContext, (U) -> T>, arguments: U, @@ -54,6 +82,13 @@ public extension TypedDate<(Year, Month, Day, Hour)> { @FillInContext public extension TypedDate<(Year, Month, Day, Hour, Minute)> { + /// Fill in missing Date components + /// + /// - Parameters: + /// - to: KeyPath for specifying components of a Date. + /// - arguments: Tuple of Components of Date to be filled. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Instance of TypedDate with more Date Components. func fill( to keyPath: KeyPath<_MinuteFillInContext, (U) -> T>, arguments: U, @@ -67,6 +102,13 @@ public extension TypedDate<(Year, Month, Day, Hour, Minute)> { @FillInContext public extension TypedDate<(Year, Month, Day, Hour, Minute, Second)> { + /// Fill in missing Date components + /// + /// - Parameters: + /// - to: KeyPath for specifying components of a Date. + /// - arguments: Tuple of Components of Date to be filled. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Instance of TypedDate with more Date Components. func fill( to keyPath: KeyPath<_SecondFillInContext, (U) -> T>, arguments: U, diff --git a/Sources/TypedDate/TypedDate+init.swift b/Sources/TypedDate/TypedDate+init.swift index e9ac683..a7dc26e 100644 --- a/Sources/TypedDate/TypedDate+init.swift +++ b/Sources/TypedDate/TypedDate+init.swift @@ -1,6 +1,12 @@ import Foundation +/// `TypedDate` is a structure for handling dates in a type-safe manner. public extension TypedDate { + /// Initializes a `TypedDate` by specifying the year. + /// - Parameters: + /// - year: The year component of the date. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Instance of `TypedDate` with the year component. init(_ year: Year, calendar: Calendar = .current) where Components == Year { self.components = year self.date = DateComponents( @@ -9,6 +15,12 @@ public extension TypedDate { ).date! } + /// Initializes a `TypedDate` by specifying the year and month. + /// - Parameters: + /// - year: The year component of the date. + /// - month: The month component of the date. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Instance of `TypedDate` with the year and month components. init( _ year: Year, _ month: Month, @@ -22,6 +34,13 @@ public extension TypedDate { ).date! } + /// Initializes a `TypedDate` by specifying the year, month, and day. + /// - Parameters: + /// - year: The year component of the date. + /// - month: The month component of the date. + /// - day: The day component of the date. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Instance of `TypedDate` with the year, month, and day components. init( _ year: Year, _ month: Month, @@ -37,6 +56,14 @@ public extension TypedDate { ).date! } + /// Initializes a `TypedDate` by specifying the year, month, day, and hour. + /// - Parameters: + /// - year: The year component of the date. + /// - month: The month component of the date. + /// - day: The day component of the date. + /// - hour: The hour component of the date. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Instance of `TypedDate` with the year, month, day, and hour components. init( _ year: Year, _ month: Month, @@ -54,6 +81,15 @@ public extension TypedDate { ).date! } + /// Initializes a `TypedDate` by specifying the year, month, day, hour, and minute. + /// - Parameters: + /// - year: The year component of the date. + /// - month: The month component of the date. + /// - day: The day component of the date. + /// - hour: The hour component of the date. + /// - minute: The minute component of the date. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Instance of `TypedDate` with the year, month, day, hour, and minute components. init( _ year: Year, _ month: Month, @@ -73,6 +109,16 @@ public extension TypedDate { ).date! } + /// Initializes a `TypedDate` by specifying the year, month, day, hour, minute, and second. + /// - Parameters: + /// - year: The year component of the date. + /// - month: The month component of the date. + /// - day: The day component of the date. + /// - hour: The hour component of the date. + /// - minute: The minute component of the date. + /// - second: The second component of the date. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Instance of `TypedDate` with the year, month, day, hour, minute, and second components. init( _ year: Year, _ month: Month, @@ -94,6 +140,17 @@ public extension TypedDate { ).date! } + /// Initializes a `TypedDate` by specifying the year, month, day, hour, minute, second, and nanosecond. + /// - Parameters: + /// - year: The year component of the date. + /// - month: The month component of the date. + /// - day: The day component of the date. + /// - hour: The hour component of the date. + /// - minute: The minute component of the date. + /// - second: The second component of the date. + /// - nanosecond: The nanosecond component of the date. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Instance of `TypedDate` with the year, month, day, hour, minute, second, and nanosecond components. init( _ year: Year, _ month: Month, @@ -117,6 +174,16 @@ public extension TypedDate { ).date! } + /// Initializes a `TypedDate` by specifying the year, month, day, hour, minute, and fractional second. + /// - Parameters: + /// - year: The year component of the date. + /// - month: The month component of the date. + /// - day: The day component of the date. + /// - hour: The hour component of the date. + /// - minute: The minute component of the date. + /// - fractionalSecond: The fractional second component of the date. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: Instance of `TypedDate` with the year, month, day, hour, minute, and fractional second components. init( _ year: Year, _ month: Month, diff --git a/Sources/TypedDate/TypedDate+modify.swift b/Sources/TypedDate/TypedDate+modify.swift index c1f5ce4..67414fb 100644 --- a/Sources/TypedDate/TypedDate+modify.swift +++ b/Sources/TypedDate/TypedDate+modify.swift @@ -1,14 +1,13 @@ import Foundation -/// Extension for `TypedDate`. Allows for modifying the year component of a date. @ModifyContext public extension TypedDate { - /// Modifies the year component of `TypedDate` with a provided modification closure. + /// Modifies a specific date component with a provided modification closure. /// - Parameters: - /// - keyPath: KeyPath to the year modify context. - /// - modify: Closure that takes an inout parameter of type T for modification. + /// - keyPath: Specify the Components of the Date you want to change by keyPath. + /// - modify: Closure to change the value. /// - calendar: Calendar used for date calculations, defaults to the current calendar. - /// - Returns: A new `TypedDate` instance with modified year component. + /// - Returns: A new `TypedDate` instance with the modified component. func modifying( _ keyPath: KeyPath<_YearModifyContext, (T, (T) -> Components)>, calendar: Calendar = .current, @@ -20,7 +19,11 @@ public extension TypedDate { return .init(transform(target), calendar: calendar) } - /// Mutates the year component of `TypedDate` instance. + /// Mutates a specific date component of with a provided modification closure. + /// - Parameters: + /// - keyPath: Specify the Components of the Date you want to change by keyPath. + /// - modify: Closure to change the value. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. mutating func modify( _ keyPath: KeyPath<_YearModifyContext, (T, (T) -> Components)>, calendar: Calendar = .current, @@ -30,10 +33,14 @@ public extension TypedDate { } } -/// Extension for `TypedDate` combining 'Year' and 'Month'. @ModifyContext public extension TypedDate<(Year, Month)> { - /// Modifies the year and month components of `TypedDate` with a provided modification closure. + /// Modifies a specific date component with a provided modification closure. + /// - Parameters: + /// - keyPath: Specify the Components of the Date you want to change by keyPath. + /// - modify: Closure to change the value. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: A new `TypedDate` instance with the modified component. func modifying( _ keyPath: KeyPath<_MonthModifyContext, (T, (T) -> Components)>, calendar: Calendar = .current, @@ -45,7 +52,11 @@ public extension TypedDate<(Year, Month)> { return .init(transform(target), calendar: calendar) } - /// Mutates the year and month components of `TypedDate` instance. + /// Mutates a specific date component with a provided modification closure. + /// - Parameters: + /// - keyPath: Specify the Components of the Date you want to change by keyPath. + /// - modify: Closure to change the value. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. mutating func modify( _ keyPath: KeyPath<_MonthModifyContext, (T, (T) -> Components)>, calendar: Calendar = .current, @@ -55,10 +66,14 @@ public extension TypedDate<(Year, Month)> { } } -/// Extension for `TypedDate` combining 'Year', 'Month', and 'Day'. @ModifyContext public extension TypedDate<(Year, Month, Day)> { - /// Modifies the year, month, and day components of `TypedDate` with a provided modification closure. + /// Modifies a specific date component with a provided modification closure. + /// - Parameters: + /// - keyPath: Specify the Components of the Date you want to change by keyPath. + /// - modify: Closure to change the value. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: A new `TypedDate` instance with the modified component. func modifying( _ keyPath: KeyPath<_DayModifyContext, (T, (T) -> Components)>, calendar: Calendar = .current, @@ -70,7 +85,11 @@ public extension TypedDate<(Year, Month, Day)> { return .init(transform(target), calendar: calendar) } - /// Mutates the year, month, and day components of `TypedDate` instance. + /// Mutates a specific date component with a provided modification closure. + /// - Parameters: + /// - keyPath: Specify the Components of the Date you want to change by keyPath. + /// - modify: Closure to change the value. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. mutating func modify( _ keyPath: KeyPath<_DayModifyContext, (T, (T) -> Components)>, calendar: Calendar = .current, @@ -80,10 +99,14 @@ public extension TypedDate<(Year, Month, Day)> { } } -/// Extension for `TypedDate` combining 'Year', 'Month', 'Day', and 'Hour'. @ModifyContext public extension TypedDate<(Year, Month, Day, Hour)> { - /// Modifies the year, month, day, and hour components of `TypedDate` with a provided modification closure. + /// Modifies a specific date component with a provided modification closure. + /// - Parameters: + /// - keyPath: Specify the Components of the Date you want to change by keyPath. + /// - modify: Closure to change the value. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: A new `TypedDate` instance with the modified component. func modifying( _ keyPath: KeyPath<_HourModifyContext, (T, (T) -> Components)>, calendar: Calendar = .current, @@ -95,7 +118,11 @@ public extension TypedDate<(Year, Month, Day, Hour)> { return .init(transform(target), calendar: calendar) } - /// Mutates the year, month, day, and hour components of `TypedDate` instance. + /// Mutates a specific date component with a provided modification closure. + /// - Parameters: + /// - keyPath: Specify the Components of the Date you want to change by keyPath. + /// - modify: Closure to change the value. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. mutating func modify( _ keyPath: KeyPath<_HourModifyContext, (T, (T) -> Components)>, calendar: Calendar = .current, @@ -105,10 +132,14 @@ public extension TypedDate<(Year, Month, Day, Hour)> { } } -/// Extension for `TypedDate` combining 'Year', 'Month', 'Day', 'Hour', and 'Minute'. @ModifyContext public extension TypedDate<(Year, Month, Day, Hour, Minute)> { - /// Modifies the year, month, day, hour, and minute components of `TypedDate` with a provided modification closure. + /// Modifies a specific date component with a provided modification closure. + /// - Parameters: + /// - keyPath: Specify the Components of the Date you want to change by keyPath. + /// - modify: Closure to change the value. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: A new `TypedDate` instance with the modified component. func modifying( _ keyPath: KeyPath<_MinuteModifyContext, (T, (T) -> Components)>, calendar: Calendar = .current, @@ -120,7 +151,11 @@ public extension TypedDate<(Year, Month, Day, Hour, Minute)> { return .init(transform(target), calendar: calendar) } - /// Mutates the year, month, day, hour, and minute components of `TypedDate` instance. + /// Mutates a specific date component with a provided modification closure. + /// - Parameters: + /// - keyPath: Specify the Components of the Date you want to change by keyPath. + /// - modify: Closure to change the value. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. mutating func modify( _ keyPath: KeyPath<_MinuteModifyContext, (T, (T) -> Components)>, calendar: Calendar = .current, @@ -130,10 +165,14 @@ public extension TypedDate<(Year, Month, Day, Hour, Minute)> { } } -/// Extension for `TypedDate` combining 'Year', 'Month', 'Day', 'Hour', 'Minute', and 'Second'. @ModifyContext public extension TypedDate<(Year, Month, Day, Hour, Minute, Second)> { - /// Modifies the year, month, day, hour, minute, and second components of `TypedDate` with a provided modification closure. + /// Modifies a specific date component with a provided modification closure. + /// - Parameters: + /// - keyPath: Specify the Components of the Date you want to change by keyPath. + /// - modify: Closure to change the value. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: A new `TypedDate` instance with the modified component. func modifying( _ keyPath: KeyPath<_SecondModifyContext, (T, (T) -> Components)>, calendar: Calendar = .current, @@ -145,7 +184,11 @@ public extension TypedDate<(Year, Month, Day, Hour, Minute, Second)> { return .init(transform(target), calendar: calendar) } - /// Mutates the year, month, day, hour, minute, and second components of `TypedDate` instance. + /// Mutates a specific date component with a provided modification closure. + /// - Parameters: + /// - keyPath: Specify the Components of the Date you want to change by keyPath. + /// - modify: Closure to change the value. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. mutating func modify( _ keyPath: KeyPath<_SecondModifyContext, (T, (T) -> Components)>, calendar: Calendar = .current, @@ -155,10 +198,14 @@ public extension TypedDate<(Year, Month, Day, Hour, Minute, Second)> { } } -/// Extension for `TypedDate` combining 'Year', 'Month', 'Day', 'Hour', 'Minute', 'Second', and 'Nanosecond'. @ModifyContext public extension TypedDate<(Year, Month, Day, Hour, Minute, Second, Nanosecond)> { - /// Modifies the year, month, day, hour, minute, second, and nanosecond components of `TypedDate` with a provided modification closure. + /// Modifies a specific date component with a provided modification closure. + /// - Parameters: + /// - keyPath: Specify the Components of the Date you want to change by keyPath. + /// - modify: Closure to change the value. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. + /// - Returns: A new `TypedDate` instance with the modified component. func modifying( _ keyPath: KeyPath<_NanosecondModifyContext, (T, (T) -> Components)>, calendar: Calendar = .current, @@ -170,7 +217,11 @@ public extension TypedDate<(Year, Month, Day, Hour, Minute, Second, Nanosecond)> return .init(transform(target), calendar: calendar) } - /// Mutates the year, month, day, hour, minute, second, and nanosecond components of `TypedDate` instance. + /// Mutates a specific date component with a provided modification closure. + /// - Parameters: + /// - keyPath: Specify the Components of the Date you want to change by keyPath. + /// - modify: Closure to change the value. + /// - calendar: Calendar used for date calculations, defaults to the current calendar. mutating func modify( _ keyPath: KeyPath<_NanosecondModifyContext, (T, (T) -> Components)>, calendar: Calendar = .current, diff --git a/Sources/TypedDate/TypedDate.swift b/Sources/TypedDate/TypedDate.swift index 3e2c30f..cf6347d 100644 --- a/Sources/TypedDate/TypedDate.swift +++ b/Sources/TypedDate/TypedDate.swift @@ -1,7 +1,16 @@ import Foundation +/// A `TypedDate` struct that encapsulates a `Date` object with associated date components. +/// This struct provides a type-safe way to handle dates with specific components, +/// such as year, month, day, etc. The `Components` type parameter defines the date components +/// associated with this `TypedDate`. public struct TypedDate: Sendable { + /// The underlying `Date` object. + /// This property stores the actual date value represented by the `TypedDate` instance. public let date: Date + /// The components associated with the `TypedDate`. + /// These components are of the type specified by the `Components` generic parameter, + /// and they represent various aspects of the date, such as year, month, day, etc. let components: Components } diff --git a/Sources/TypedDate/runtimeWarning.swift b/Sources/TypedDate/runtimeWarning.swift deleted file mode 100644 index 63f2116..0000000 --- a/Sources/TypedDate/runtimeWarning.swift +++ /dev/null @@ -1,24 +0,0 @@ -import Foundation -import os - -func runtimeWarning(_ message: StaticString) { - #if DEBUG - var info = Dl_info() - dladdr( - dlsym( - dlopen(nil, RTLD_LAZY), - "$s10Foundation15AttributeScopesO7SwiftUIE05swiftE0AcDE0D12UIAttributesVmvg" - ), - &info - ) - os_log( - .fault, - dso: info.dli_fbase, - log: OSLog( - subsystem: "com.apple.runtime-issues", - category: "TypedDate" - ), - message - ) - #endif -}