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

Feature - improved best match implementation of content type id #61

Merged
merged 1 commit into from
Jan 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions src/lib/PnP.Framework/Extensions/FieldAndContentTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1943,12 +1943,72 @@ public static ContentTypeId BestMatch(this ContentTypeCollection contentTypes, s
var ctx = contentTypes.Context;
contentTypes.EnsureProperties(c => c.Include(ct => ct.Id));

var res = contentTypes.Where(c => c.Id.StringValue.StartsWith(contentTypeId, StringComparison.InvariantCultureIgnoreCase)).OrderBy(c => c.Id.StringValue.Length).FirstOrDefault();
if (res != null)
return BestMatch(contentTypeId, contentTypes);

}

/// <summary>
/// Searches for the content type with the closest match to the specified content type ID.
/// If the search finds two matches, the shorter ID is returned.
/// </summary>
/// <param name="contentTypes">Content type collection to search</param>
/// <param name="contentTypeId">Content type id for the content type to search</param>
/// <returns>Content type Id object or null if was not found</returns>
public static ContentTypeId BestMatch(this ContentTypeCollection contentTypes, ContentTypeId contentTypeId)
{
if (contentTypeId == null)
{
return res.Id;
throw new ArgumentNullException(nameof(contentTypeId));
}
return null;

return BestMatch(contentTypes, contentTypeId.StringValue);
}

internal static int CountCommonBytes(this ContentTypeId thisId, string id)
{
string thisIdValue = thisId.StringValue.Substring(2).ToLower();
string otherIdValue = id.Substring(2).ToLower();

int index = 0;
while ((index * 2 + 1 < thisIdValue.Length) && ((index * 2 + 1 < otherIdValue.Length) && (thisIdValue[index * 2] == otherIdValue[index * 2]) && (thisIdValue[index * 2 + 1] == otherIdValue[index * 2 + 1])))
{
index++;
}
return index;
}

/// <summary>
/// Searches for the content type with the closest match to this content type id.
/// If the search finds two matches, the shorter ID is returned.
/// </summary>
/// <param name="contentTypeId">Content type id for the content type to search</param>
/// <param name="contentTypeCollection">Content type collection to search</param>
/// <returns>Content type Id object or null if was not found</returns>

public static ContentTypeId BestMatch(ContentTypeId contentTypeId, IEnumerable<ContentType> contentTypeCollection)
{
return BestMatch(contentTypeId.StringValue, contentTypeCollection);
}

private static ContentTypeId BestMatch(string contentTypeId, IEnumerable<ContentType> contentTypeCollection)
{
ContentTypeId bestMatch = null;
int bestMatchCommonBytes = 0;
foreach (ContentType contentType in contentTypeCollection)
{
int commonBytes = contentType.Id.CountCommonBytes(contentTypeId);
if (commonBytes > bestMatchCommonBytes)
{
bestMatch = contentType.Id;
bestMatchCommonBytes = commonBytes;
continue;
}
if ((commonBytes == bestMatchCommonBytes) && (contentType.Id.StringValue.Length < bestMatch.StringValue.Length))
{
bestMatch = contentType.Id;
}
}
return bestMatch;
}

/// <summary>
Expand Down