https://github.com/rcasto/edits
A little library to calculate the edit distance between 2 strings.
https://github.com/rcasto/edits
edit-distance
Last synced: 19 days ago
JSON representation
A little library to calculate the edit distance between 2 strings.
- Host: GitHub
- URL: https://github.com/rcasto/edits
- Owner: rcasto
- License: mit
- Created: 2021-09-01T00:04:31.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-09-13T13:25:44.000Z (over 3 years ago)
- Last Synced: 2025-01-20T05:38:21.622Z (12 months ago)
- Topics: edit-distance
- Language: TypeScript
- Homepage: https://rcasto.github.io/edits/
- Size: 541 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# edits
A little library to calculate the [edit distance](https://en.wikipedia.org/wiki/Edit_distance) between 2 strings, and allow returning the edit record path, transforming string1 -> string2.
## Usage
```typescript
import { editDistance } from 'edits';
const {
distance,
records
} = editDistance('art', 'bat', {
returnEditRecords: true,
});
console.log(distance);
console.log(JSON.stringify(records || [], null, '\t'));
/* Console Output
2
[
{
"type": "replace",
"str1": {
"index": 0,
"value": "a"
},
"str2": {
"index": 0,
"value": "b"
},
"transformIndex": 0
},
{
"type": "replace",
"str1": {
"index": 1,
"value": "r"
},
"str2": {
"index": 1,
"value": "a"
},
"transformIndex": 1
},
{
"type": "match",
"str1": {
"index": 2,
"value": "t"
},
"str2": {
"index": 2,
"value": "t"
},
"transformIndex": 2
}
]
*/
```
### Browser
If you are using `edits` in the browser, then you will want to reference the browser global `Edits`.
```javascript
console.log(Edits.editDistance('foo', 'bar'));
```
### Demos/Examples:
- https://rcasto.github.io/edits/
- https://codepen.io/rcasto/full/MWGjPzR
- https://codepen.io/rcasto/pen/qBjZOga?editors=0012
## Edit Records
Edit records represent individual operations taken along determining the edit distance between string1 and string2.
The operations or edit record types are:
- Add
- Delete
- Match
- Replace
A `Match` operation does not contribute to the edit distance between the 2 strings.
All of the edit records are in reference to the transformation of string1 advancing towards becoming string2. In that context an add operation/edit record means adding the current character from string2 to string1.