Merge pull request #18059 from ragtime/string-similarity

[string-similarity] Add types for string-similarity
This commit is contained in:
Ryan Cavanaugh
2017-07-14 13:55:31 -07:00
committed by GitHub
4 changed files with 80 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
// Type definitions for string-similarity 1.2
// Project: https://github.com/aceakash/string-similarity#readme
// Definitions by: ragtime <https://github.com/ragtime>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/**
* Returns a fraction between 0 and 1, which indicates the degree of similarity between the two strings.
* 0 indicates completely different strings, 1 indicates identical strings. The comparison is case-insensitive.
*
* @param string1 The first string
* @param string2 The second string
* @returns A fraction from 0 to 1, both inclusive. Higher number indicates more similarity.
*/
export function compareTwoStrings(string1: string, string2: string): number;
/**
* Compares mainString against each string in targetStrings.
*
* @argument mainString: The string to match each target string against.
* @argument targetStrings: Each string in this array will be matched against the main string.
* @returns An object with a ratings property, which gives a similarity rating for each target string, and a bestMatch property,
* which specifies which target string was most similar to the main string.
*/
export function findBestMatch(mainString: string, targetStrings: string[]): BestMatch;
export interface Rating {
target: string;
rating: number;
}
export interface BestMatch {
ratings: Rating[];
bestMatch: Rating;
}
@@ -0,0 +1,24 @@
import stringSimilarity = require('string-similarity');
stringSimilarity.compareTwoStrings('test', 'test');
const match: stringSimilarity.BestMatch = stringSimilarity.findBestMatch('Olive-green table for sale, in extremely good condition.', [
'For sale: green Subaru Impreza, 210,000 miles',
'For sale: table in very good condition, olive green in colour.',
'Wanted: mountain bike with at least 21 gears.'
]);
// ratings accessible
for (const rating of match.ratings){
isString(rating.target);
isNumber(rating.rating);
}
// bestMatch accessible
match.bestMatch.rating;
match.bestMatch.target;
function isString(x: string): void {
}
function isNumber(x: number): void {
}
+22
View File
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"string-similarity-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }