Skip to content

Commit

Permalink
Cleanup of return for single line statements
Browse files Browse the repository at this point in the history
  • Loading branch information
malcommac committed Sep 10, 2022
1 parent d981727 commit 4dc40e7
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 123 deletions.
20 changes: 10 additions & 10 deletions Sources/SwiftDate/Date/Date+Compare.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public extension Date {
/// - precision: The precision of the comparison (default is 5 minutes, or 300 seconds).
/// - Returns: A boolean; true if close by, false otherwise.
func compareCloseTo(_ refDate: Date, precision: TimeInterval = 300) -> Bool {
return (abs(timeIntervalSince(refDate)) < precision)
(abs(timeIntervalSince(refDate)) < precision)
}

// MARK: - Extendend Compare
Expand All @@ -35,7 +35,7 @@ public extension Date {
/// - Parameter compareType: comparison type.
/// - Returns: `true` if comparison succeded, `false` otherwise
func compare(_ compareType: DateComparisonType) -> Bool {
return inDefaultRegion().compare(compareType)
inDefaultRegion().compare(compareType)
}

/// Returns a ComparisonResult value that indicates the ordering of two given dates based on
Expand All @@ -45,7 +45,7 @@ public extension Date {
/// - parameter granularity: The smallest unit that must, along with all larger units be less for the given dates
/// - returns: `ComparisonResult`
func compare(toDate refDate: Date, granularity: Calendar.Component) -> ComparisonResult {
return inDefaultRegion().compare(toDate: refDate.inDefaultRegion(), granularity: granularity)
inDefaultRegion().compare(toDate: refDate.inDefaultRegion(), granularity: granularity)
}

/// Compares whether the receiver is before/before equal `date` based on their components down to a given unit granularity.
Expand All @@ -56,7 +56,7 @@ public extension Date {
/// - granularity: smallest unit that must, along with all larger units, be less for the given dates
/// - Returns: Boolean
func isBeforeDate(_ refDate: Date, orEqual: Bool = false, granularity: Calendar.Component) -> Bool {
return inDefaultRegion().isBeforeDate(refDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity)
inDefaultRegion().isBeforeDate(refDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity)
}

/// Compares whether the receiver is after `date` based on their components down to a given unit granularity.
Expand All @@ -67,7 +67,7 @@ public extension Date {
/// - granularity: Smallest unit that must, along with all larger units, be greater for the given dates.
/// - Returns: Boolean
func isAfterDate(_ refDate: Date, orEqual: Bool = false, granularity: Calendar.Component) -> Bool {
return inDefaultRegion().isAfterDate(refDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity)
inDefaultRegion().isAfterDate(refDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity)
}

/// Returns a value between 0.0 and 1.0 or nil, that is the position of current date between 2 other dates.
Expand All @@ -77,7 +77,7 @@ public extension Date {
/// - endDate: range lower bound date
/// - Returns: `nil` if current date is not between `startDate` and `endDate`. Otherwise returns position between `startDate` and `endDate`.
func positionInRange(date startDate: Date, and endDate: Date) -> Double? {
return inDefaultRegion().positionInRange(date: startDate.inDefaultRegion(), and: endDate.inDefaultRegion())
inDefaultRegion().positionInRange(date: startDate.inDefaultRegion(), and: endDate.inDefaultRegion())
}

/// Return true if receiver date is contained in the range specified by two dates.
Expand All @@ -89,7 +89,7 @@ public extension Date {
/// - granularity: smallest unit that must, along with all larger units, be greater for the given dates.
/// - Returns: Boolean
func isInRange(date startDate: Date, and endDate: Date, orEqual: Bool = false, granularity: Calendar.Component = .nanosecond) -> Bool {
return inDefaultRegion().isInRange(date: startDate.inDefaultRegion(), and: endDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity)
inDefaultRegion().isInRange(date: startDate.inDefaultRegion(), and: endDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity)
}

/// Compares equality of two given dates based on their components down to a given unit
Expand All @@ -101,7 +101,7 @@ public extension Date {
///
/// - returns: `true` if the dates are the same down to the given granularity, otherwise `false`
func isInside(date: Date, granularity: Calendar.Component) -> Bool {
return (compare(toDate: date, granularity: granularity) == .orderedSame)
(compare(toDate: date, granularity: granularity) == .orderedSame)
}

// MARK: - Date Earlier/Later
Expand All @@ -111,15 +111,15 @@ public extension Date {
/// - Parameter date: The date to compare to self
/// - Returns: The date that is earlier
func earlierDate(_ date: Date) -> Date {
return timeIntervalSince(date) <= 0 ? self : date
timeIntervalSince(date) <= 0 ? self : date
}

/// Return the later of two dates, between self and a given date.
///
/// - Parameter date: The date to compare to self
/// - Returns: The date that is later
func laterDate(_ date: Date) -> Date {
return timeIntervalSince(date) >= 0 ? self : date
timeIntervalSince(date) >= 0 ? self : date
}

}
Expand Down
10 changes: 5 additions & 5 deletions Sources/SwiftDate/Date/Date+Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ public extension Date {

/// Indicates whether the month is a leap month.
var isLeapMonth: Bool {
return inDefaultRegion().isLeapMonth
inDefaultRegion().isLeapMonth
}

/// Indicates whether the year is a leap year.
var isLeapYear: Bool {
return inDefaultRegion().isLeapYear
inDefaultRegion().isLeapYear
}

/// Julian day is the continuous count of days since the beginning of
/// the Julian Period used primarily by astronomers.
var julianDay: Double {
return inDefaultRegion().julianDay
inDefaultRegion().julianDay
}

/// The Modified Julian Date (MJD) was introduced by the Smithsonian Astrophysical Observatory
/// in 1957 to record the orbit of Sputnik via an IBM 704 (36-bit machine)
/// and using only 18 bits until August 7, 2576.
var modifiedJulianDay: Double {
return inDefaultRegion().modifiedJulianDay
inDefaultRegion().modifiedJulianDay
}

/// Return elapsed time expressed in given components since the current receiver and a reference date.
Expand All @@ -44,6 +44,6 @@ public extension Date {
/// - component: time unit to extract.
/// - Returns: value
func getInterval(toDate: Date?, component: Calendar.Component) -> Int64 {
return inDefaultRegion().getInterval(toDate: toDate?.inDefaultRegion(), component: component)
inDefaultRegion().getInterval(toDate: toDate?.inDefaultRegion(), component: component)
}
}
24 changes: 12 additions & 12 deletions Sources/SwiftDate/Date/Date+Create.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public extension Date {
/// - increment: components to add
/// - Returns: array of dates
static func enumerateDates(from startDate: Date, to endDate: Date, increment: DateComponents) -> [Date] {
return Date.enumerateDates(from: startDate, to: endDate, increment: { _ in
Date.enumerateDates(from: startDate, to: endDate, increment: { _ in
return increment
})
}
Expand All @@ -78,15 +78,15 @@ public extension Date {
/// - Parameter style: rounding mode.
/// - Returns: rounded date
func dateRoundedAt(at style: RoundDateMode) -> Date {
return inDefaultRegion().dateRoundedAt(style).date
inDefaultRegion().dateRoundedAt(style).date
}

/// Returns a new DateInRegion that is initialized at the start of a specified unit of time.
///
/// - Parameter unit: time unit value.
/// - Returns: instance at the beginning of the time unit; `self` if fails.
func dateAtStartOf(_ unit: Calendar.Component) -> Date {
return inDefaultRegion().dateAtStartOf(unit).date
inDefaultRegion().dateAtStartOf(unit).date
}

/// Return a new DateInRegion that is initialized at the start of the specified components
Expand All @@ -95,7 +95,7 @@ public extension Date {
/// - Parameter units: sequence of transformations as time unit components
/// - Returns: new date at the beginning of the passed components, intermediate results if fails.
func dateAtStartOf(_ units: [Calendar.Component]) -> Date {
return units.reduce(self) { (currentDate, currentUnit) -> Date in
units.reduce(self) { (currentDate, currentUnit) -> Date in
return currentDate.dateAtStartOf(currentUnit)
}
}
Expand All @@ -106,7 +106,7 @@ public extension Date {
///
/// - returns: A new Moment instance.
func dateAtEndOf(_ unit: Calendar.Component) -> Date {
return inDefaultRegion().dateAtEndOf(unit).date
inDefaultRegion().dateAtEndOf(unit).date
}

/// Return a new DateInRegion that is initialized at the end of the specified components
Expand All @@ -115,7 +115,7 @@ public extension Date {
/// - Parameter units: sequence of transformations as time unit components
/// - Returns: new date at the end of the passed components, intermediate results if fails.
func dateAtEndOf(_ units: [Calendar.Component]) -> Date {
return units.reduce(self) { (currentDate, currentUnit) -> Date in
units.reduce(self) { (currentDate, currentUnit) -> Date in
return currentDate.dateAtEndOf(currentUnit)
}
}
Expand All @@ -125,7 +125,7 @@ public extension Date {
/// - Parameter components: components to alter with their new values.
/// - Returns: new altered `DateInRegion` instance
func dateBySet(_ components: [Calendar.Component: Int]) -> Date? {
return DateInRegion(self, region: SwiftDate.defaultRegion).dateBySet(components)?.date
DateInRegion(self, region: SwiftDate.defaultRegion).dateBySet(components)?.date
}

/// Create a new date by altering specified time components.
Expand All @@ -147,15 +147,15 @@ public extension Date {
/// - Parameter components: components to truncate.
/// - Returns: new date with truncated components.
func dateTruncated(_ components: [Calendar.Component]) -> Date? {
return DateInRegion(self, region: SwiftDate.defaultRegion).dateTruncated(at: components)?.date
DateInRegion(self, region: SwiftDate.defaultRegion).dateTruncated(at: components)?.date
}

/// Creates a new instance by truncating the components starting from given components down the granurality.
///
/// - Parameter component: The component to be truncated from.
/// - Returns: new date with truncated components.
func dateTruncated(from component: Calendar.Component) -> Date? {
return DateInRegion(self, region: SwiftDate.defaultRegion).dateTruncated(from: component)?.date
DateInRegion(self, region: SwiftDate.defaultRegion).dateTruncated(from: component)?.date
}

/// Offset a date by n calendar components.
Expand All @@ -166,23 +166,23 @@ public extension Date {
/// - component: component to offset.
/// - Returns: new altered date.
func dateByAdding(_ count: Int, _ component: Calendar.Component) -> DateInRegion {
return DateInRegion(self, region: SwiftDate.defaultRegion).dateByAdding(count, component)
DateInRegion(self, region: SwiftDate.defaultRegion).dateByAdding(count, component)
}

/// Return related date starting from the receiver attributes.
///
/// - Parameter type: related date to obtain.
/// - Returns: instance of the related date.
func dateAt(_ type: DateRelatedType) -> Date {
return inDefaultRegion().dateAt(type).date
inDefaultRegion().dateAt(type).date
}

/// Create a new date at now and extract the related date using passed rule type.
///
/// - Parameter type: related date to obtain.
/// - Returns: instance of the related date.
static func nowAt(_ type: DateRelatedType) -> Date {
return Date().dateAt(type)
Date().dateAt(type)
}

/// Return the dates for a specific weekday inside given month of specified year.
Expand Down
10 changes: 5 additions & 5 deletions Sources/SwiftDate/Date/Date+Math.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ import Foundation
/// let difference = lhs - rhs
/// rhs + difference = lhs
public func - (lhs: Date, rhs: Date) -> DateComponents {
return SwiftDate.defaultRegion.calendar.dateComponents(DateComponents.allComponentsSet, from: rhs, to: lhs)
SwiftDate.defaultRegion.calendar.dateComponents(DateComponents.allComponentsSet, from: rhs, to: lhs)
}

/// Adds date components to a date and returns a new date.
public func + (lhs: Date, rhs: DateComponents) -> Date {
return rhs.from(lhs)!
rhs.from(lhs)!
}

/// Adds date components to a date and returns a new date.
public func + (lhs: DateComponents, rhs: Date) -> Date {
return (rhs + lhs)
(rhs + lhs)
}

/// Subtracts date components from a date and returns a new date.
public func - (lhs: Date, rhs: DateComponents) -> Date {
return (lhs + (-rhs))
(lhs + (-rhs))
}

public func + (lhs: Date, rhs: TimeInterval) -> Date {
return lhs.addingTimeInterval(rhs)
lhs.addingTimeInterval(rhs)
}
26 changes: 6 additions & 20 deletions Sources/SwiftDate/Date/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@

import Foundation

#if os(Linux)

#else
internal enum AssociatedKeys: String {
case customDateFormatter = "SwiftDate.CustomDateFormatter"
}
#endif

extension Date: DateRepresentable {

Expand All @@ -27,18 +23,9 @@ extension Date: DateRepresentable {

/// For absolute Date object the default region is obtained from the global `defaultRegion` variable.
public var region: Region {
return SwiftDate.defaultRegion
SwiftDate.defaultRegion
}

#if os(Linux)
public var customFormatter: DateFormatter? {
get {
debugPrint("Not supported on Linux")
return nil
}
set { debugPrint("Not supported on Linux") }
}
#else
/// Assign a custom formatter if you need a special behaviour during formatting of the object.
/// Usually you will not need to do it, SwiftDate uses the local thread date formatter in order to
/// optimize the formatting process. By default is `nil`.
Expand All @@ -51,11 +38,10 @@ extension Date: DateRepresentable {
set(associatedValue: newValue, key: AssociatedKeys.customDateFormatter.rawValue, object: self as AnyObject)
}
}
#endif

/// Extract the date components.
public var dateComponents: DateComponents {
return region.calendar.dateComponents(DateComponents.allComponentsSet, from: self)
region.calendar.dateComponents(DateComponents.allComponentsSet, from: self)
}

/// Initialize a new date object from string expressed in given region.
Expand Down Expand Up @@ -134,29 +120,29 @@ extension Date: DateRepresentable {
///
/// - Returns: `DateInRegion`
public func inDefaultRegion() -> DateInRegion {
return DateInRegion(self, region: SwiftDate.defaultRegion)
DateInRegion(self, region: SwiftDate.defaultRegion)
}

/// Express given absolute date in the context of passed region.
///
/// - Parameter region: destination region.
/// - Returns: `DateInRegion`
public func `in`(region: Region) -> DateInRegion {
return DateInRegion(self, region: region)
DateInRegion(self, region: region)
}

/// Return a date in the distant past.
///
/// - Returns: Date instance.
public static func past() -> Date {
return Date.distantPast
Date.distantPast
}

/// Return a date in the distant future.
///
/// - Returns: Date instance.
public static func future() -> Date {
return Date.distantFuture
Date.distantFuture
}

}
Loading

0 comments on commit 4dc40e7

Please sign in to comment.