diff --git a/test/test.ts b/test/test.ts index 229f9b2..3c249e4 100644 --- a/test/test.ts +++ b/test/test.ts @@ -14,11 +14,30 @@ tap.test('should create an instance of Smartfuzzy', async () => { }); tap.test('should compute a score', async () => { - testSmartfuzzy.getChangeScoreForString('Apple'); + const result = testSmartfuzzy.getChangeScoreForString('Apple'); + console.log(result); }); tap.test('should get closest match', async () => { - testSmartfuzzy.getClosestMatchForString('Apple'); + const result = testSmartfuzzy.getClosestMatchForString('Apple'); + console.log(result); +}); + +tap.test('should sort objects', async () => { + class Car { + constructor(public brand: string) {} + } + + let testObjectSorter: smartfuzzy.ObjectSorter; + + testObjectSorter = new smartfuzzy.ObjectSorter([ + new Car('BMW'), + new Car('Mercedes Benz'), + new Car('Volvo') + ]); + + const result = testObjectSorter.sort('Volvo', ['brand']); + console.log(result); }); tap.start(); diff --git a/ts/index.ts b/ts/index.ts index 8234a0e..df0f24a 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1 +1,2 @@ export * from './smartfuzzy.classes.smartfuzzy'; +export * from './smartfuzzy.classes.objectsorter'; diff --git a/ts/smartfuzzy.classes.objectsorter.ts b/ts/smartfuzzy.classes.objectsorter.ts new file mode 100644 index 0000000..e7f2b8a --- /dev/null +++ b/ts/smartfuzzy.classes.objectsorter.ts @@ -0,0 +1,26 @@ +import * as plugins from './smartfuzzy.plugins'; + +export class ObjectSorter { + public objectDictionary: T[]; + + + constructor(objectDictionaryArg: T[] = []) { + this.objectDictionary = objectDictionaryArg; + } + + sort(stringArg: string, objectKeysArg: string[]): T[] { + const fuseOptions = { + shouldSort: true, + threshold: 0.6, + location: 0, + distance: 100, + maxPatternLength: 32, + minMatchCharLength: 1, + keys: objectKeysArg + }; + const fuse = new plugins.fuseJs(this.objectDictionary, fuseOptions); + const result = fuse.search(stringArg); + return result; + } + +} \ No newline at end of file diff --git a/ts/smartfuzzy.classes.smartfuzzy.ts b/ts/smartfuzzy.classes.smartfuzzy.ts index 5179576..5a2d3ea 100644 --- a/ts/smartfuzzy.classes.smartfuzzy.ts +++ b/ts/smartfuzzy.classes.smartfuzzy.ts @@ -2,6 +2,8 @@ import * as plugins from './smartfuzzy.plugins'; export let standardExport = 'Hi there! :) This is an exported string'; +export type TDictionaryMap = { [key: string]: number }; + export class Smartfuzzy { dictionary: string[]; constructor(dictionary: string[]) { @@ -24,12 +26,12 @@ export class Smartfuzzy { * returns the closest match for a given string * @param stringArg */ - getChangeScoreForString(stringArg) { - const dictionaryMap: { [key: string]: number } = {}; + getChangeScoreForString(stringArg): TDictionaryMap { + const dictionaryMap: TDictionaryMap = {}; for (const wordArg of this.dictionary) { dictionaryMap[wordArg] = plugins.leven(stringArg, wordArg); } - console.log(dictionaryMap); + return dictionaryMap; } getClosestMatchForString(stringArg: string) { @@ -39,7 +41,7 @@ export class Smartfuzzy { name: wordArg }); } - const options = { + const fuseOptions = { shouldSort: true, threshold: 0.6, location: 0, @@ -48,8 +50,8 @@ export class Smartfuzzy { minMatchCharLength: 1, keys: ['name'] }; - const fuse = new plugins.fuseJs(fuseDictionary, options); + const fuse = new plugins.fuseJs(fuseDictionary, fuseOptions); const result = fuse.search(stringArg); - console.log(result); + return result; } }