New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

string-mismatch

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

string-mismatch

All mismatch between two strings

  • 3.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
607
increased by33.41%
Maintainers
1
Weekly downloads
 
Created
Source

string-mismatch library

Build Status codecov.io contributions welcome

The library contain the next string comparison algorithms:

GreedyLevenshteinDice Coefficient
ComplexityO(n*k) (k precision)O(n^2)O(nlog n)
GoodFast algorithmAlways the optimal solutionIs based in probabilities and is a really fast algorithm
BadThe solution is not the optimalComplexity is O(n^2)Impossible to see the differences between the strings
Use n^2 memory
Methodsdifferencedifferencedistance
distancedistance
Operations for transform the stringinsertioninsertionnot apply
deletiondeletion
substitution
Class nameGreedyLevenshteinDiceCoefficient

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/

Install

npm install --save string-mismatch

Getting started

Nodejs application example

How to use the library and see the differences between two strings:

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:

{
  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:

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

<!--Greedy algorithm-->
<script src="lib/greedy.min.js" type="application/javascript"></script>
<!--Levenshtein algorithm-->
<script src="lib/levenshtein.min.js" type="application/javascript"></script>

Example with greedy algorithm:

<script type="application/javascript">
    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);
</script>

Example with the levenshtein algorithm:

<script type="application/javascript">
    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);
</script>

Testing code

npm test

Built With

  • webpack - For build the project
  • npm - Dependency Management
  • jest - Jest framework for test

Contributing

All contributions are welcome.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • Guillermo González - Initial work - wil92

CHANGELOG

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Keywords

FAQs

Package last updated on 16 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc