Skip to content

Commit

Permalink
refactor: further segregation of responsibility
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon committed Dec 6, 2024
1 parent 346e744 commit f35b52d
Showing 1 changed file with 39 additions and 35 deletions.
74 changes: 39 additions & 35 deletions src/utils/metadataDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,15 @@ export default class MetadataDiff {
this.toContent = toContent
this.fromContent = fromContent

const comparator = new MetadataComparator(this.extractor)

const added = comparator.compare(
this.toContent,
this.fromContent,
this.compareAdded()
)
const deleted = comparator.compare(
const comparator = new MetadataComparator(
this.extractor,
this.fromContent,
this.toContent,
this.compareDeleted()
this.toContent
)

const added = comparator.getChanges()
const deleted = comparator.getDeletion()

return { added, deleted }
}

Expand All @@ -76,29 +72,6 @@ export default class MetadataDiff {
isEmpty: this.extractor.isContentEmpty(prunedContent),
}
}

private compareAdded() {
return (
meta: XmlContent[],
keySelector: KeySelectorFn,
elem: XmlContent
) => {
const elemKey = keySelector(elem)
const match = meta.find(el => keySelector(el) === elemKey)
return !match || !isEqual(match, elem)
}
}

private compareDeleted() {
return (
meta: XmlContent[],
keySelector: KeySelectorFn,
elem: XmlContent
) => {
const elemKey = keySelector(elem)
return !meta.some(el => keySelector(el) === elemKey)
}
}
}

class MetadataExtractor {
Expand Down Expand Up @@ -150,9 +123,21 @@ class MetadataExtractor {
}

class MetadataComparator {
constructor(private extractor: MetadataExtractor) {}
constructor(
private extractor: MetadataExtractor,
private fromContent: XmlContent,
private toContent: XmlContent
) {}

getChanges() {
return this.compare(this.toContent, this.fromContent, this.compareAdded)
}

compare(
getDeletion() {
return this.compare(this.fromContent, this.toContent, this.compareDeleted)
}

private compare(
baseContent: XmlContent,
targetContent: XmlContent,
elementMatcher: (
Expand Down Expand Up @@ -186,6 +171,25 @@ class MetadataComparator {
return manifest
}, new Map())
}

private compareAdded = (
meta: XmlContent[],
keySelector: KeySelectorFn,
elem: XmlContent
) => {
const elemKey = keySelector(elem)
const match = meta.find(el => keySelector(el) === elemKey)
return !match || !isEqual(match, elem)
}

private compareDeleted = (
meta: XmlContent[],
keySelector: KeySelectorFn,
elem: XmlContent
) => {
const elemKey = keySelector(elem)
return !meta.some(el => keySelector(el) === elemKey)
}
}

class JsonTransformer {
Expand Down

0 comments on commit f35b52d

Please sign in to comment.