Skip to content
This repository was archived by the owner on May 1, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Finds degree of similarity between two strings, based on [Dice's Coefficient](ht
* [Release Notes](#release-notes)
* [2.0.0](#200)
* [3.0.0](#300)
* [3.0.1](#301)


## Usage
Expand Down Expand Up @@ -119,6 +120,9 @@ stringSimilarity.findBestMatch('Olive-green table for sale, in extremely good co
* The algorithm has been tweaked slightly to disregard spaces and word boundaries. This will change the rating values slightly but not enough to make a significant difference
* Adding a `bestMatchIndex` to the results for `findBestMatch(..)` to point to the best match in the supplied `targetStrings` array

### 3.0.1
* Refactoring: removed unused functions; used `substring` instead of `substr`
* Updated dependencies

![Build status](https://codeship.com/projects/2aa453d0-0959-0134-8a76-4abcb29fe9b4/status?branch=master)
[![Known Vulnerabilities](https://snyk.io/test/github/aceakash/string-similarity/badge.svg)](https://snyk.io/test/github/aceakash/string-similarity)
19 changes: 2 additions & 17 deletions compare-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function compareTwoStrings(first, second) {

let firstBigrams = new Map();
for (let i = 0; i < first.length - 1; i++) {
const bigram = first.substr(i, 2);
const bigram = first.substring(i, i + 2);
const count = firstBigrams.has(bigram)
? firstBigrams.get(bigram) + 1
: 1;
Expand All @@ -25,7 +25,7 @@ function compareTwoStrings(first, second) {

let intersectionSize = 0;
for (let i = 0; i < second.length - 1; i++) {
const bigram = second.substr(i, 2);
const bigram = second.substring(i, i + 2);
const count = firstBigrams.has(bigram)
? firstBigrams.get(bigram)
: 0;
Expand Down Expand Up @@ -60,25 +60,10 @@ function findBestMatch(mainString, targetStrings) {
return { ratings, bestMatch, bestMatchIndex };
}

function flattenDeep(arr) {
return Array.isArray(arr) ? arr.reduce((a, b) => a.concat(flattenDeep(b)), []) : [arr];
}

function areArgsValid(mainString, targetStrings) {
if (typeof mainString !== 'string') return false;
if (!Array.isArray(targetStrings)) return false;
if (!targetStrings.length) return false;
if (targetStrings.find(s => typeof s !== 'string')) return false;
return true;
}

function letterPairs(str) {
const pairs = [];
for (let i = 0, max = str.length - 1; i < max; i++) pairs[i] = str.substring(i, i + 2);
return pairs;
}

function wordLetterPairs(str) {
const pairs = str.toUpperCase().split(' ').map(letterPairs);
return flattenDeep(pairs);
}
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "string-similarity",
"version": "3.0.0",
"version": "3.0.1",
"description": "Finds degree of similarity between strings, based on Dice's Coefficient, which is mostly better than Levenshtein distance.",
"main": "compare-strings.js",
"scripts": {
Expand Down Expand Up @@ -29,6 +29,6 @@
"author": "Akash Kurdekar <npm@kurdekar.com> (http://untilfalse.com/)",
"license": "ISC",
"devDependencies": {
"jasmine": "^3.3.0"
"jasmine": "^3.4.0"
}
}