Socket
Socket
Sign inDemoInstall

damerau-levenshtein

Package Overview
Dependencies
0
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.1 to 1.0.2

12

index.js

@@ -60,10 +60,14 @@ // TheSpanishInquisition

function prepare(steps) {
var distance = {
var length = Math.max(thisLength, thatLength)
var relative = length === 0
? 0
: (steps / length);
var similarity = 1 - relative
return {
steps: steps,
relative: (steps / Math.max(thisLength, thatLength)) || 0
relative: relative,
similarity: similarity
};
distance.similarity = (distance.relative !== 0) ?(1 - distance.relative) : 0;
return distance;
}
};
{
"name": "damerau-levenshtein",
"version": "1.0.1",
"version": "1.0.2",
"description": "Damerau - Levenshtein distance by The Spanish Inquisition + relative distance",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -0,1 +1,3 @@

[![NPM](https://nodei.co/npm/damerau-levenshtein.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/damerau-levenshtein/)
I use algorithm kindly provided by TheSpanishInquisition here: <http://jsperf.com/damerau-levenshtein-distance>.

@@ -10,3 +12,3 @@

steps: 5, // Levenstein demerau distance
relative: 0.7, // steps / length of bigger of two words
relative: 0.7, // steps / length of the longer string
similarity: 0.3 // 1 - relative

@@ -16,1 +18,2 @@ }

Please see [tests](./test/test.js) for more insights.
var levenshtien = require("./../index");
var assert = require('assert');
var assert = require("assert");
describe('Damerau - Levenshtein', function() {
describe('#Equality.', function() {
it('Should return 0 step when Test is equals Test.', function() {
assert.equal(0, levenshtien("test", "test").steps);
});
describe("Damerau - Levenshtein", function() {
describe("Equality", function() {
it("returns 0 steps for equal strings", function() {
assert.deepEqual(levenshtien("test", "test"), {
steps: 0,
relative: 0,
similarity: 1
});
});
describe("#Additions.",function(){
it('should return 1 step when append one char.', function() {
assert.equal(1, levenshtien ("test", "tests").steps);
});
it('should return 1 step when prepend one char.', function() {
assert.equal(1, levenshtien ("test", "stest").steps);
});
it('should return 2 steps when append two char.', function() {
assert.equal(2, levenshtien ("test", "mytest").steps);
});
it('should return 7 steps when append seven char.', function() {
assert.equal(7, levenshtien ("test", "mycrazytest").steps);
});
});
describe("Additions", function() {
it("returns 1 step when appending one char", function() {
assert.deepEqual(levenshtien("test", "tests"), {
steps: 1,
relative: 1 / 5,
similarity: 1 - 1 / 5
});
});
describe("#Additions prepend and append.",function(){
it('should return 9 steps when prepend two chars and append seven chars.', function() {
assert.equal(9, levenshtien ("test", "mytestiscrazy").steps);
});
it("returns 1 step when prepending one char", function() {
assert.deepEqual(levenshtien("test", "stest"), {
steps: 1,
relative: 1 / 5,
similarity: 1 - 1 / 5
});
});
describe("#Addition of repeated chars.",function(){
it('should return 1 step when adding another "e"', function() {
assert.equal(1, levenshtien ("test", "teest").steps);
});
it("returns 2 steps when appending two char", function() {
assert.deepEqual(levenshtien("test", "mytest"), {
steps: 2,
relative: 2 / 6,
similarity: 1 - 2 / 6
});
});
describe("#Deletion.",function(){
it('should return 1 step when remove "e" char.', function() {
assert.equal( 1, levenshtien ("test", "tst").steps);
});
it("returns 7 steps when appending seven char", function() {
assert.deepEqual(levenshtien("test", "mycrazytest"), {
steps: 7,
relative: 7 / 11,
similarity: 1 - 7 / 11
});
});
describe("#Transposition.",function(){
it('should return 1 step when transposition "s" char.', function() {
assert.equal( 1, levenshtien ("test", "tset").steps);
});
it("returns 9 steps when prepend two chars and append seven chars", function() {
assert.deepEqual(levenshtien("test", "mytestiscrazy"), {
steps: 9,
relative: 9 / 13,
similarity: 1 - 9 / 13
});
});
});
describe("#Addition with transposition.",function(){
it('should return 1 step when transposition "s" char and append another "s".', function() {
assert.equal( 2, levenshtien ("test", "tsets").steps);
});
describe("Addition of repeated chars", function() {
it("returns 1 step when repeating a character", function() {
assert.deepEqual(levenshtien("test", "teest"), {
steps: 1,
relative: 1 / 5,
similarity: 1 - 1 / 5
});
});
describe("#Transposition of repeated chars.",function(){
it('should return 1 step when transposition "n" and "a".', function() {
assert.equal( 1, levenshtien("banana", "banaan").steps);
});
it("returns 2 step when repeating a character twice", function() {
assert.deepEqual(levenshtien("test", "teeest"), {
steps: 2,
relative: 2 / 6,
similarity: 1 - 2 / 6
});
});
});
it('should return 1 step when transposition "b" and "a".', function() {
assert.equal( 1, levenshtien("banana", "abnana").steps);
});
it('should return 1 step when transposition "b" and "a" and "n" and "b".', function() {
assert.equal( 2, levenshtien("banana", "nabana").steps);
});
describe("#Deletion", function() {
it("returns 1 step when removing one char", function() {
assert.deepEqual(levenshtien("test", "tst"), {
steps: 1,
relative: 1 / 4,
similarity: 1 - 1 / 4
});
});
});
describe("#Empty strings.",function(){
it('should return 0 step when both are empty.', function() {
assert.equal( 0, levenshtien("", "").steps);
});
it('should return 4 steps when second string are empty.', function() {
assert.equal( 4, levenshtien("test", "").steps);
});
describe("Transposition", function() {
it("returns 1 step when transposing one char", function() {
assert.deepEqual(levenshtien("test", "tset"), {
steps: 1,
relative: 1 / 4,
similarity: 1 - 1 / 4
});
});
});
it('should return 4 steps when first string are empty.', function() {
assert.equal( 4, levenshtien("", "test").steps);
});
describe("Addition with transposition", function() {
it("returns 2 step when transposing one char and append another", function() {
assert.deepEqual(levenshtien("test", "tsets"), {
steps: 2,
relative: 2 / 5,
similarity: 1 - 2 / 5
});
});
it("returns 2 step when transposing a char and repeating it", function() {
assert.deepEqual(levenshtien("test", "tsset"), {
steps: 2,
relative: 2 / 5,
similarity: 1 - 2 / 5
});
});
});
describe("Transposition of multiple chars", function() {
it("returns 1 step when transposing two neighbouring characters", function() {
assert.deepEqual(levenshtien("banana", "banaan"), {
steps: 1,
relative: 1 / 6,
similarity: 1 - 1 / 6
});
});
it("returns 2 step when transposing two neighbouring characters by two places", function() {
assert.deepEqual(levenshtien("banana", "nabana"), {
steps: 2,
relative: 2 / 6,
similarity: 1 - 2 / 6
});
});
it("returns 2 step when transposing two pairs of characters", function() {
assert.deepEqual(levenshtien("banana", "abnaan"), {
steps: 2,
relative: 2 / 6,
similarity: 1 - 2 / 6
});
});
});
describe("Empty strings", function() {
it("returns 0 step and 0 relative when both are empty", function() {
assert.deepEqual(levenshtien("", ""), {
steps: 0,
relative: 0,
similarity: 1
});
});
it("returns steps equal to first string lenght when second string is empty", function() {
assert.deepEqual(levenshtien("test", ""), {
steps: 4,
relative: 4 / 4,
similarity: 0
});
});
it("returns steps equal to second string lenght when first string is empty", function() {
assert.deepEqual(levenshtien("", "test"), {
steps: 4,
relative: 1,
similarity: 0
});
});
});
});
SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc