Skip to content

Commit

Permalink
add some documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryu0118 committed Nov 12, 2023
1 parent 753f88c commit f2d7d92
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 63 deletions.
22 changes: 9 additions & 13 deletions Sources/TypedDate/Date+scope.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import Foundation

extension Date {
@_disfavoredOverload // To test the one with a return value of TypedDate<T>?
/// Returns a `TypedDate<T>` 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<T>` representing the specified date components.
@_disfavoredOverload
public func scope<T>(
to keyPath: KeyPath<_TypedDateContext, TypedDate<T>?>,
calendar: Calendar = .current
) -> TypedDate<T> {
_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<T>(
to keyPath: KeyPath<_TypedDateContext, TypedDate<T>?>,
calendar: Calendar = .current
Expand Down Expand Up @@ -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<TypedDate<(Year, Month)>._MonthEraseContext, (TypedDate<Sendable>.Type, Sendable)>#>)
// }
//}
74 changes: 72 additions & 2 deletions Sources/TypedDate/TypedDate+components.swift
Original file line number Diff line number Diff line change
@@ -1,126 +1,196 @@
import Foundation

/// `TypedDate<Year>` extension. This allows you to get the year component of a date.
public extension TypedDate<Year> {
/// Returns the year component of `TypedDate<Year>`.
/// - 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
}
Expand Down
30 changes: 30 additions & 0 deletions Sources/TypedDate/TypedDate+erase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
to keyPath: KeyPath<_MonthEraseContext, (TypedDate<T>.Type, T)>,
calendar: Calendar = .current
Expand All @@ -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<T>(
to keyPath: KeyPath<_DayEraseContext, (TypedDate<T>.Type, T)>,
calendar: Calendar = .current
Expand All @@ -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<T>(
to keyPath: KeyPath<_HourEraseContext, (TypedDate<T>.Type, T)>,
calendar: Calendar = .current
Expand All @@ -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<T>(
to keyPath: KeyPath<_MinuteEraseContext, (TypedDate<T>.Type, T)>,
calendar: Calendar = .current
Expand All @@ -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<T>(
to keyPath: KeyPath<_SecondEraseContext, (TypedDate<T>.Type, T)>,
calendar: Calendar = .current
Expand All @@ -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<T>(
to keyPath: KeyPath<_NanosecondEraseContext, (TypedDate<T>.Type, T)>,
calendar: Calendar = .current
Expand Down
42 changes: 42 additions & 0 deletions Sources/TypedDate/TypedDate+fill.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import Foundation

@FillInContext
public extension TypedDate<Year> {
/// 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<T, U>(
to keyPath: KeyPath<_YearFillInContext,(U) -> T>,
arguments: U,
Expand All @@ -15,6 +22,13 @@ public extension TypedDate<Year> {

@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<T, U>(
to keyPath: KeyPath<_MonthFillInContext,(U) -> T>,
arguments: U,
Expand All @@ -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<T, U>(
to keyPath: KeyPath<_DayFillInContext, (U) -> T>,
arguments: U,
Expand All @@ -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<T, U>(
to keyPath: KeyPath<_HourFillInContext, (U) -> T>,
arguments: U,
Expand All @@ -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<T, U>(
to keyPath: KeyPath<_MinuteFillInContext, (U) -> T>,
arguments: U,
Expand All @@ -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<T, U>(
to keyPath: KeyPath<_SecondFillInContext, (U) -> T>,
arguments: U,
Expand Down
Loading

0 comments on commit f2d7d92

Please sign in to comment.