Real simple string diffing.
MIT License
Simple string diffing. Given two strings, striff
will return an object noting which characters were added or removed, and at which indices.
Run npm install striff
. Or stick in on a page via CDN.
Import it, pass a couple of strings, and do whatever you want with the results.
import striff from "striff";
const result = striff("string #1", "string #2");
// {
// added: [
// ...added characters
// ],
// removed: [
// ...removed characters
// ]
// }
Here's the kind of result you'll get with different types of diffing.
const str1 = "abc";
const str2 = "abcde";
const result = striff(str1, str2);
{
added: [
{
value: "d",
index: 3
},
{
value: "e",
index: 4
}
],
removed: []
}
const str1 = "abc";
const str2 = "a";
const result = striff(str1, str2);
{
added: [],
removed: [
{
value: "b",
index: 1
},
{
value: "c",
index: 2
}
]
}
For strings whose characters were changed at the end, the indices will be grouped together at the end of the string.
const str1 = "abbbc";
const str2 = "ab";
const result = striff(str1, str2);
{
added: [],
removed: [
{
value: "b",
index: 2
},
{
value: "b",
index: 3
},
{
value: "c",
index: 4
}
]
}
For those whose characters were changed at the beginning, the indices will be grouped together at the beginning.
const str1 = "abbbc";
const str2 = "bc";
const result = striff(str1, str2);
{
added: [].
removed: [
{
value: "a",
index: 0
},
{
value: "b",
index: 1
},
{
value: "b",
index: 2
}
]
}
Make an issue or a pull request!