Skip to content

Commit

Permalink
Implement birthdayMatches(date:) method.
Browse files Browse the repository at this point in the history
  • Loading branch information
dragosrobertn committed Jan 15, 2020
1 parent 3411df7 commit 290da8d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion KNContacts.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "KNContacts"
s.version = "1.2.1"
s.version = "1.2.2"
s.summary = "KNContacts is a wrapper framework for CNContacts for easier access, scheduling and ordering."

s.description = <<-DESC
Expand Down
24 changes: 19 additions & 5 deletions KNContacts/KNContact.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ KNContact class is a wrapper class which gives quick access to helper methods fo
birthday and age information, first email address or phone number.

- Author: dragosrobertn
- Version: 1.2.1
- Version: 1.2.2
**/
public struct KNContact {

Expand Down Expand Up @@ -165,12 +165,25 @@ public struct KNContact {
Helper method to find out if current date matches the contact's birthday.

- Author: dragosrobertn
- Returns: Returns a boolean value representing whether the today is the contact's birthday.
- Returns: Returns a boolean value representing whether current date is the contact's birthday.
- Version: 1.0.0
*/
public func isBirthdayToday() -> Bool {
let todayFormattedString = KNDatesUtils.string(from: Date(), format: .dayAndMonth)
return self.formatBirthday() == todayFormattedString
return self.birthdayMatches(date: Date())
}

/**
Helper method to find out if current date matches the contact's birthday.

- Author: dragosrobertn
- Parameters:
- date: The date to which to compare the contact's birthday
- Returns: Returns a boolean value representing whether the passed date matches the contact's birthday
- Version: 1.2.2
*/
public func birthdayMatches(date: Date) -> Bool {
let formattedDate = KNDatesUtils.string(from: date, format: .dayAndMonth)
return self.formatBirthday() == formattedDate
}

/**
Expand All @@ -181,7 +194,8 @@ public struct KNContact {
- in: The number of days as an integer representing the number of days to check if the birthday is upcoming
- startingDate: The date from which to start checking if the birthday is upcoming. Default is today's date and the starting date will be excluded..

- Returns: Returns a bool representing whether the today is the contact's birthday. False if the contact doesn't have birthday information available.
- Returns: Returns a bool representing whether the contact's birthday is in the interval between the starting date and the number of following days provided by the days param.
False if the contact doesn't have birthday information available.
- Version: 1.2.1
*/
public func isBirthdayComing(in days: Int, startingDate: Date = Date()) -> Bool {
Expand Down
26 changes: 26 additions & 0 deletions KNContactsTests/KNContactTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,32 @@ class KNContactTests: XCTestCase {
XCTAssertTrue(contact.isBirthdayToday())
}

func testBirthdayMatchesCurrentDateIsTrue() {
let mutableContact = UnitTestsContactHelpers.getMutableContact()

let today = Date()
let todayComponents = calendar.dateComponents([.day, .month], from: today)
mutableContact.birthday?.day = todayComponents.day
mutableContact.birthday?.month = todayComponents.month

let contact = KNContact(for: mutableContact)

XCTAssertTrue(contact.birthdayMatches(date: today))
}

func testBirthdayMatchesTomorrowsDateIsFalse() {
let mutableContact = UnitTestsContactHelpers.getMutableContact()

let tomorrow = calendar.date(byAdding: .day, value: 1, to: Date())!
let todayComponents = calendar.dateComponents([.day, .month], from: Date())
mutableContact.birthday?.day = todayComponents.day
mutableContact.birthday?.month = todayComponents.month

let contact = KNContact(for: mutableContact)

XCTAssertFalse(contact.birthdayMatches(date: tomorrow))
}

func testGetFirstEmailAddress() {
let contact = UnitTestsContactHelpers.getKNContact()

Expand Down

0 comments on commit 290da8d

Please sign in to comment.