Skip to content

Commit b345fb6

Browse files
Add isIdentity() and isIdentity(by:)
1 parent 028b915 commit b345fb6

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

Sources/LightTableDelta/Delta.swift

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,25 @@ extension Delta where Element: ~Copyable {
199199
try coalesce(source, target)
200200
}
201201
}
202+
203+
/// Returns whether the delta is of the transition case and a predicate is true given the source and target elements.
204+
///
205+
/// A source delta or target delta always returns `false` without invoking `predicate`.
206+
///
207+
/// - Parameter predicate: The return value of this function is returned by `isIdentity(by:)`.
208+
@inlinable
209+
public func isIdentity<E>(
210+
by predicate: (_ source: borrowing Element, _ target: borrowing Element) throws(E) -> Bool
211+
) throws(E) -> Bool {
212+
switch self {
213+
case .source(_):
214+
false
215+
case .target(_):
216+
false
217+
case .transition(let source, let target):
218+
try predicate(source, target)
219+
}
220+
}
202221
}
203222

204223
extension Delta: Copyable where Element: Copyable {
@@ -257,7 +276,24 @@ extension Delta: Copyable where Element: Copyable {
257276
}
258277
}
259278

260-
extension Delta: Equatable where Element: Equatable {}
279+
extension Delta: Equatable where Element: Equatable {
280+
/// Returns whether the delta is of the transition case with the source equal to the target.
281+
///
282+
/// Whether this is an identity delta is determined using the equality of `Equatable`, not reference identity (`===`).
283+
///
284+
/// A source delta or target delta always returns `false`.
285+
@inlinable
286+
public func isIdentity() -> Bool {
287+
switch self {
288+
case .source(_):
289+
false
290+
case .target(_):
291+
false
292+
case .transition(let source, let target):
293+
source == target
294+
}
295+
}
296+
}
261297

262298
extension Delta: Hashable where Element: Hashable {}
263299

Sources/LightTableDelta/Documentation.docc/Delta.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@
3535
### Identity Delta
3636

3737
- ``identity(_:)``
38+
- ``isIdentity()``
39+
- ``isIdentity(by:)``

0 commit comments

Comments
 (0)