Skip to content

Commit

Permalink
fix: Sort additions and deletions together (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbmorley authored Feb 25, 2025
1 parent e18fbf9 commit eb3ccd9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 21 deletions.
46 changes: 46 additions & 0 deletions Sources/ReporterCore/Model/Change.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2024-2025 Jason Morley
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Foundation

public struct Change {

enum Kind {
case addition
case deletion
}

let kind: Kind
let source: Item
let destination: Item?

let isAddition: Bool
let isDeletion: Bool

init(kind: Kind, source: Item, destination: Item? = nil) {
self.kind = kind
self.source = source
self.destination = destination

self.isAddition = kind == .addition
self.isDeletion = kind == .deletion
}

}
23 changes: 8 additions & 15 deletions Sources/ReporterCore/Model/Changes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,23 @@ import Foundation

public struct Changes: CustomStringConvertible {

public let additions: [Item]
public let deletions: [Item]
public let changes: [Change]

public let isEmpty: Bool

public var description: String {
return (
"\(additions.count) additions\n" +
additions
.map { " \($0.path)\n" }
.joined() +
"\(deletions.count) deletions\n" +
deletions
.map { " \($0.path)\n" }
"\(changes.count) changes\n" +
changes
.map { " \($0.source.path)\n" }
.joined()
)
}

public init(additions: [Item], deletions: [Item]) {
self.additions = additions
.sorted { $0.path.localizedStandardCompare($1.path) == .orderedAscending }
self.deletions = deletions
.sorted { $0.path.localizedStandardCompare($1.path) == .orderedAscending }
self.isEmpty = additions.isEmpty && deletions.isEmpty
public init(changes: [Change]) {
self.changes = changes
.sorted { $0.source.path.localizedStandardCompare($1.source.path) == .orderedAscending }
self.isEmpty = changes.isEmpty
}

}
4 changes: 3 additions & 1 deletion Sources/ReporterCore/Model/Snapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ public struct Snapshot: Codable {

public func changes(from initialState: Snapshot) -> Changes {
let additions = items.subtracting(initialState.items)
.map { Change(kind: .addition, source: $0) }
let deletions = initialState.items.subtracting(items)
return Changes(additions: Array(additions), deletions: Array(deletions))
.map { Change(kind: .deletion, source: $0) }
return Changes(changes: Array(additions) + Array(deletions))
}

}
11 changes: 6 additions & 5 deletions Sources/ReporterCore/Reporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,12 @@ public class Reporter {
</header>
{% if item.changes.isEmpty %}{% else %}
<ul class="changes">
{% for addition in item.changes.additions %}
<li class="addition">{{ addition.path }}</li>
{% endfor %}
{% for deletion in item.changes.deletions %}
<li class="deletion">{{ deletion.path }}</li>
{% for change in item.changes.changes %}
{% if change.isAddition %}
<li class="addition">{{ change.source.path }}</li>
{% else %}
<li class="deletion">{{ change.source.path }}</li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
Expand Down

0 comments on commit eb3ccd9

Please sign in to comment.