Skip to content

Commit c53990d

Browse files
committed
Added some additional document source comparison helpers
1 parent 1e6226f commit c53990d

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

RELEASE.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 1.0.0-beta.72
22

3+
- Added a `DocumentSourceComparer` class that implements `IEqualityComparer<IDocument>` and can be used to compare documents by source path.
4+
- Added a `IEnumerable<IDocument>.ContainsBySource()` extension method to check if a collection of documents contains a document with a given source path.
35
- Added an improved warning message and early exit out of recursive settings expansion.
46
- Added a `MediaTypes.IsMediaType()` method to help determine if a given path matches specified media type(s).
57

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections.Generic;
2+
3+
namespace Statiq.Common
4+
{
5+
/// <summary>
6+
/// A simple <see cref="IEqualityComparer{IDocument}"/> that compares documents by <see cref="IDocument.Source"/>.
7+
/// </summary>
8+
public class DocumentSourceComparer : IEqualityComparer<IDocument>
9+
{
10+
public static DocumentSourceComparer Instance { get; } = new DocumentSourceComparer();
11+
12+
public bool Equals(IDocument x, IDocument y) =>
13+
(x is null && y is null) || (x is object && y is object && x.Source.Equals(y.Source));
14+
15+
public int GetHashCode(IDocument obj) => obj?.Source.GetHashCode() ?? 0;
16+
}
17+
}

src/core/Statiq.Common/Documents/IDocumentEnumerableExtensions.cs

+20
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,29 @@ public static TDocument FirstOrDefaultDestination<TDocument>(this IEnumerable<TD
208208
where TDocument : IDocument =>
209209
documents.FirstOrDefaultDestination((IEnumerable<string>)patterns);
210210

211+
/// <summary>
212+
/// Determines whether a document is contained in a document collection by ID.
213+
/// </summary>
214+
/// <remarks>
215+
/// Note that the document ID my get "out of sync" when caching is being used. For example, if comparing
216+
/// the ID of documents read from disk on a subsequent execution against the same documents cached from
217+
/// an earlier execution, the IDs won't match even if the document is the same.
218+
/// </remarks>
219+
/// <param name="documents">The documents to check.</param>
220+
/// <param name="document">The document to check for.</param>
221+
/// <returns><c>true</c> if the document is contained in the collection, <c>false</c> otherwise.</returns>
211222
public static bool ContainsById(this IEnumerable<IDocument> documents, IDocument document) =>
212223
documents.Contains(document, DocumentIdComparer.Instance);
213224

225+
/// <summary>
226+
/// Determines whether a document is contained in a document collection by source.
227+
/// </summary>
228+
/// <param name="documents">The documents to check.</param>
229+
/// <param name="document">The document to check for.</param>
230+
/// <returns><c>true</c> if the document is contained in the collection, <c>false</c> otherwise.</returns>
231+
public static bool ContainsBySource(this IEnumerable<IDocument> documents, IDocument document) =>
232+
documents.Contains(document, DocumentSourceComparer.Instance);
233+
214234
/// <summary>
215235
/// Flattens a tree structure.
216236
/// </summary>

0 commit comments

Comments
 (0)