Skip to content

Commit

Permalink
Add special minor change use case
Browse files Browse the repository at this point in the history
The percent threshold naturally does not work well for short strings.
For use cases where only two characters are changes, condider it a
minor change, too.
  • Loading branch information
HeavyWombat committed May 15, 2018
1 parent 97c0859 commit 8df201c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 12 additions & 4 deletions pkg/dyff/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ var Error = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
// possibilities to find unique enough keys, which might no qualify as such.
var NonStandardIdentifierGuessCountThreshold = 3

// MinorChangeThreshold specifies how many percent of the text needs to be
// changed so that it still qualifies as being a minor string change.
var MinorChangeThreshold = 0.1

// Constants to distinguish between the different kinds of differences
const (
ADDITION = '+'
Expand Down Expand Up @@ -742,12 +746,16 @@ func max(a, b int) int {

func isMinorChange(from string, to string) bool {
levenshteinDistance := levenshtein.DistanceForStrings([]rune(from), []rune(to), levenshtein.DefaultOptions)
referenceLength := min(len(from), len(to))

distanceVsLengthFactor := float64(levenshteinDistance) / float64(referenceLength)
threshold := 0.1
// Special case: Consider it a minor change if only two runes/characters were
// changed, which results in a default distance of four, two removals and two
// additions each.
if levenshteinDistance <= 4 {
return true
}

return distanceVsLengthFactor < threshold
referenceLength := min(len(from), len(to))
return float64(levenshteinDistance)/float64(referenceLength) < MinorChangeThreshold
}

func isMultiLine(from string, to string) bool {
Expand Down
6 changes: 3 additions & 3 deletions pkg/dyff/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ import (
var _ = Describe("Human readable report", func() {
Context("reporting differences", func() {
It("should show a nice string difference", func() {
content := singleDiff("/some/yaml/structure/string", MODIFICATION, "foobar", "Foobar")
content := singleDiff("/some/yaml/structure/string", MODIFICATION, "fOObar?", "Foobar!")
Expect(humanDiff(content)).To(BeEquivalentTo(`
some.yaml.structure.string
± value change
- foobar
+ Foobar
- fOObar?
+ Foobar!
`))
})
Expand Down

0 comments on commit 8df201c

Please sign in to comment.