Skip to content

Commit

Permalink
imp - Consolidated type-based info parsers
Browse files Browse the repository at this point in the history
We've consolidated type-based info parsers

---

We've made a substantial change to type-based info parsers by determining the types from the value and parsing them more efficiently. We've cut down the constants so that there are no repeat types with a difference in the delimiter.

---

Type: imp
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Aug 12, 2023
1 parent 9258ddd commit c3dae4c
Show file tree
Hide file tree
Showing 21 changed files with 741 additions and 467 deletions.
13 changes: 8 additions & 5 deletions VisualCard.Tests/ContactData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static class ContactData
BEGIN:VCARD
VERSION:3.0
FN:Rick Hood
N:Hood;Rick;;;;
N:Hood;Rick;;;
END:VCARD

"""
Expand Down Expand Up @@ -147,7 +147,7 @@ public static class ContactData
BEGIN:VCARD
VERSION:3.0
FN:John Sanders
N:Sanders;John;;;;
N:Sanders;John;;;
TEL;TYPE=CELL:495-522-3560
ADR;TYPE=HOME:;;Los Angeles;;;;USA
EMAIL;TYPE=HOME:john.s@acme.co
Expand Down Expand Up @@ -720,6 +720,7 @@ public static class ContactData
TEL;TYPE=cell:589-210-1059
TITLE:Chief Executive Officer
URL:https://sso.org/
BDAY:19890222
X-SIP-SIP:sip test
END:VCARD

Expand Down Expand Up @@ -789,7 +790,7 @@ public static class ContactData
new ImppInfo(0, Array.Empty<string>(), "aim:IM", new string[] { "HOME" }),
new ImppInfo(0, Array.Empty<string>(), "msn:Windows LIVE", new string[] { "HOME" }),
new ImppInfo(0, Array.Empty<string>(), "ymsgr:Yahoo", new string[] { "HOME" })
}
},
};
private static readonly Card multipleVcardFourContactsInstanceThree = new
(
Expand Down Expand Up @@ -827,7 +828,8 @@ public static class ContactData
ContactXNames = new XNameInfo[]
{
new XNameInfo(0, Array.Empty<string>(), "SIP-SIP", new string[] { "sip test" }, Array.Empty<string>()),
}
},
ContactBirthdate = new DateTime(1989, 2, 22),
};
private static readonly Card multipleVcardFourContactsInstanceFour = singleVcardFourContactInstance;
#endregion
Expand Down Expand Up @@ -891,7 +893,8 @@ public static class ContactData
ContactMails = new EmailInfo[]
{
new EmailInfo(0, Array.Empty<string>(), new string[] { "INTERNET" }, "deriks@Microsoft.com")
}
},
ContactBirthdate = new DateTime(1963, 9, 21),
};
private static readonly Card vcardThreeOldSampleInstanceTwo = new
(
Expand Down
38 changes: 0 additions & 38 deletions VisualCard/Converters/MeCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,6 @@ public static List<BaseVcardParser> GetContactsFromMeCardString(string meCardStr
var nameSplits = value.Substring(2).Split(',');
fullName = $"{nameSplits[1]} {nameSplits[0]}";
}

// As VisualCard doesn't support ADR: yet, why don't we just make it ADR; until we improve type detecion?
if (value.StartsWith("ADR:"))
values[i] = $"ADR;TYPE=HOME{values[i].Substring(3)}";

// As VisualCard doesn't support EMAIL: yet, why don't we just make it EMAIL; until we improve type detecion?
if (value.StartsWith("EMAIL:"))
values[i] = $"EMAIL;TYPE=HOME{values[i].Substring(5)}";

// MeCard stores birthday date in this format:
// 19700310
// Digit 1-4: Year
// Digit 5-6: Month
// Digit 7-8: Day
// However, .NET's DateTime doesn't support it, so we verify the number, split it, and make an appropriate string for this.
if (value.StartsWith("BDAY:"))
{
int birthNum = int.Parse(value.Substring(5));
var birthDigits = GetDigits(birthNum).ToList();
int birthYear = (birthDigits[0] * 1000) + (birthDigits[1] * 100) + (birthDigits[2] * 10) + birthDigits[3];
int birthMonth = (birthDigits[4] * 10) + birthDigits[5];
int birthDay = (birthDigits[6] * 10) + birthDigits[7];
var birthDate = new DateTime(birthYear, birthMonth, birthDay);
values[i] = $"BDAY:{birthDate:g}";
}
}

// Install the values!
Expand All @@ -132,18 +107,5 @@ public static List<BaseVcardParser> GetContactsFromMeCardString(string meCardStr
return cardParsers;
}

private static IEnumerable<int> GetDigits(int num)
{
int individualFactor = 0;
int tennerFactor = Convert.ToInt32(Math.Pow(10, num.ToString().Length));
while (tennerFactor > 1)
{
num -= tennerFactor * individualFactor;
tennerFactor /= 10;
individualFactor = num / tennerFactor;
yield return individualFactor;
}
}

}
}
Loading

0 comments on commit c3dae4c

Please sign in to comment.