https://github.com/wil92/string-mismatch
This library share functions for compare two strings and see the differences
https://github.com/wil92/string-mismatch
algorithm hacktoberfest javascript string
Last synced: about 1 month ago
JSON representation
This library share functions for compare two strings and see the differences
- Host: GitHub
- URL: https://github.com/wil92/string-mismatch
- Owner: wil92
- License: apache-2.0
- Created: 2018-03-28T13:13:53.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-09-09T08:33:01.000Z (7 months ago)
- Last Synced: 2024-09-09T10:17:44.274Z (7 months ago)
- Topics: algorithm, hacktoberfest, javascript, string
- Language: TypeScript
- Homepage:
- Size: 518 KB
- Stars: 9
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
- cuban-opensource - string-mismatch.js
README
# string-mismatch library
[](https://travis-ci.org/wil92/string-mismatch)
[](http://codecov.io/github/wil92/string-mismatch?branch=master)
[](https://github.com/wil92/string-mismatch/issues)The library contain the next string comparison algorithms:
| |Greedy |[Levenshtein](https://en.wikipedia.org/wiki/Levenshtein_distance)|[Dice Coefficient](https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient)|
|---------------------------------------|-------------------------------|-----------------------------------------------------------------|----------------------------------------------------------------------------------------|
|**Complexity** |O(n*k) (**k** precision) |O(n^2) |O(nlog n) |
|**Good** |Fast algorithm |Always the optimal solution |Is based in probabilities and is a really fast algorithm |
|**Bad** |The solution is not the optimal|Complexity is O(n^2) |Impossible to see the differences between the strings |
| | |Use n^2 memory | |
|**Methods** |*difference* |*difference* |*distance* |
| |*distance* |*distance* | |
|**Operations for transform the string**|*insertion* |*insertion* |*not apply* |
| |*deletion* |*deletion* | |
| | |*substitution* | |
|**Class name** |`Greedy` |`Levenshtein` |`DiceCoefficient` |Why use string-mismatch:
- Ease to install and start using it
- Modular library (use only what you want to use).
- Support for browser and node applications.
- Compatible with es5
- Not external dependencies.
- Completely documented.
- Coverage over 95%.## Library documentation
[https://wil92.github.io/string-mismatch/](https://wil92.github.io/string-mismatch/)
## Install
```
npm install --save string-mismatch
```## Getting started
### Nodejs application example
How to use the library and see the differences between two strings:
```es5
const sm = require("string-mismatch");
const greedyInstance = new sm.Greedy();var start = 'This is a test for see how work the library',
end = 'This is a test for know how work the new library';console.log(greedyInstance.differences(start, end));
```The result is an object array with the mismatch result. Each object with the next structure:
```es5
{
type: string, // type of sub-string:
// 'sub' -> substitution
// 'ins' -> insertion
// 'del' -> deletion
// 'eql' -> equal
value: string // value of the current sub-string
}
```The resulting string can be concatenated like the next example:
```es5
const sm = require("string-mismatch");
const greedyInstance = new sm.Greedy();var start = 'This is a test for see how work the library',
end = 'This is a test for know how work the new library';function showResult(diffs) {
return diffs.reduce(function (text, value) {
switch (value.type) {
case 'del':
return text + '(-' + value.value + ')';
case 'ins':
return text + '(+' + value.value + ')';
case 'sub':
return text + '(-+' + value.value + ')';
case 'eql':
return text + value.value;
}
}, '');
}console.log(showResult(greedyInstance.differences(start, end)));
/*
result:
This is a test for (-see)(+know) how work the (+new )library
*/
```This code can be tested in the project's examples. To run the examples use the next command:
```
npm start
```### Web application example
Import the library
```html5
```
Example with greedy algorithm:
```html5
var start = 'This is a test for see how work the library';
var end = 'This is a test for know how work the new library';
var alg = new Greedy(options);
var diffs = alg.differences(start, end);
console.log(diffs);```
Example with the levenshtein algorithm:
```html5
var start = 'This is a test for see how work the library';
var end = 'This is a test for know how work the new library';
var alg = new Levenshtein(options);
var diffs = alg.differences(start, end);
console.log(diffs);```
## Testing code
```
npm test
```## Built With
* [webpack](https://webpack.js.org/) - For build the project
* [npm](https://www.npmjs.com/) - Dependency Management
* [jest](https://jestjs.io/) - Jest framework for test## Contributing
All contributions are welcome.
## Versioning
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/wil92/string-mismatch/tags).
## Authors
* **Guillermo González** - *Initial work* - [wil92](https://github.com/wil92)
## [CHANGELOG](https://github.com/wil92/string-mismatch/blob/master/CHANGELOG.md)
## License
This project is licensed under the MIT License - see the [LICENSE.md](https://gitlab.com/wil92/wankar-server/blob/development/LICENSE) file for details