Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ExactMoon #39

Closed
mannylopez opened this issue Jul 23, 2024 · 0 comments · Fixed by #40
Closed

Refactor ExactMoon #39

mannylopez opened this issue Jul 23, 2024 · 0 comments · Fixed by #40
Assignees

Comments

@mannylopez
Copy link
Owner

API should be ExactMoon.moonPhase, ExactMoon.name, and ExactMoon.emoji instead of the verbose ExactMoon.exactMoonPhase, ExactMoon.exactName, and ExactMoon.exactEmoji.

extension TinyMoon {

  public struct ExactMoon: Hashable {

    // MARK: Lifecycle

    init(date: Date, phaseFraction: Double) {
      self.date = date
      exactMoonPhase = ExactMoon.exactMoonPhase(phaseFraction: phaseFraction)
      exactName = exactMoonPhase.rawValue
      exactEmoji = exactMoonPhase.emoji
    }

    // MARK: Public

    public let exactMoonPhase: MoonPhase
    public let exactName: String
    public let exactEmoji: String
    public let date: Date

    // MARK: Internal

    static func exactMoonPhase(phaseFraction: Double) -> MoonPhase {
      if phaseFraction < 0.02 {
        .newMoon
      } else if phaseFraction < 0.23 {
        .waxingCrescent
      } else if phaseFraction < 0.27 {
        .firstQuarter
      } else if phaseFraction < 0.48 {
        .waxingGibbous
      } else if phaseFraction < 0.52 {
        .fullMoon
      } else if phaseFraction < 0.73 {
        .waningGibbous
      } else if phaseFraction < 0.77 {
        .lastQuarter
      } else if phaseFraction < 0.98 {
        .waningCrescent
      } else {
        .newMoon
      }
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant