From 42a0e3e0a0732155b54db4c684db944cd5283a89 Mon Sep 17 00:00:00 2001 From: Johannes Kessler Date: Fri, 14 Jul 2017 12:43:06 +0200 Subject: [PATCH] Add types for string-similarity --- types/string-similarity/index.d.ts | 33 +++++++++++++++++++ .../string-similarity-tests.ts | 24 ++++++++++++++ types/string-similarity/tsconfig.json | 22 +++++++++++++ types/string-similarity/tslint.json | 1 + 4 files changed, 80 insertions(+) create mode 100644 types/string-similarity/index.d.ts create mode 100644 types/string-similarity/string-similarity-tests.ts create mode 100644 types/string-similarity/tsconfig.json create mode 100644 types/string-similarity/tslint.json diff --git a/types/string-similarity/index.d.ts b/types/string-similarity/index.d.ts new file mode 100644 index 0000000000..01a157811a --- /dev/null +++ b/types/string-similarity/index.d.ts @@ -0,0 +1,33 @@ +// Type definitions for string-similarity 1.2 +// Project: https://github.com/aceakash/string-similarity#readme +// Definitions by: 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; +} diff --git a/types/string-similarity/string-similarity-tests.ts b/types/string-similarity/string-similarity-tests.ts new file mode 100644 index 0000000000..216c1dedf2 --- /dev/null +++ b/types/string-similarity/string-similarity-tests.ts @@ -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 { +} diff --git a/types/string-similarity/tsconfig.json b/types/string-similarity/tsconfig.json new file mode 100644 index 0000000000..b95b2aeb77 --- /dev/null +++ b/types/string-similarity/tsconfig.json @@ -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" + ] +} diff --git a/types/string-similarity/tslint.json b/types/string-similarity/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/string-similarity/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }