Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
cbc72cd55b | |||
a2d8f9575d | |||
6da6c6cca9 | |||
dc856dd5c7 | |||
fdf9ec6358 | |||
8021e9f96f |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "smartfuzzy",
|
"name": "smartfuzzy",
|
||||||
"version": "1.0.2",
|
"version": "1.1.1",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "smartfuzzy",
|
"name": "@pushrocks/smartfuzzy",
|
||||||
"version": "1.0.2",
|
"version": "1.1.1",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "fuzzy match strings against word dictionaries/arrays",
|
"description": "fuzzy match strings against word dictionaries/arrays",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
23
test/test.ts
23
test/test.ts
@ -14,11 +14,30 @@ tap.test('should create an instance of Smartfuzzy', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should compute a score', 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 () => {
|
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<Car>;
|
||||||
|
|
||||||
|
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();
|
tap.start();
|
||||||
|
@ -1 +1,2 @@
|
|||||||
export * from './smartfuzzy.classes.smartfuzzy';
|
export * from './smartfuzzy.classes.smartfuzzy';
|
||||||
|
export * from './smartfuzzy.classes.objectsorter';
|
||||||
|
26
ts/smartfuzzy.classes.objectsorter.ts
Normal file
26
ts/smartfuzzy.classes.objectsorter.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import * as plugins from './smartfuzzy.plugins';
|
||||||
|
|
||||||
|
export class ObjectSorter<T> {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -2,6 +2,8 @@ import * as plugins from './smartfuzzy.plugins';
|
|||||||
|
|
||||||
export let standardExport = 'Hi there! :) This is an exported string';
|
export let standardExport = 'Hi there! :) This is an exported string';
|
||||||
|
|
||||||
|
export type TDictionaryMap = { [key: string]: number };
|
||||||
|
|
||||||
export class Smartfuzzy {
|
export class Smartfuzzy {
|
||||||
dictionary: string[];
|
dictionary: string[];
|
||||||
constructor(dictionary: string[]) {
|
constructor(dictionary: string[]) {
|
||||||
@ -24,22 +26,22 @@ export class Smartfuzzy {
|
|||||||
* returns the closest match for a given string
|
* returns the closest match for a given string
|
||||||
* @param stringArg
|
* @param stringArg
|
||||||
*/
|
*/
|
||||||
getChangeScoreForString(stringArg) {
|
getChangeScoreForString(stringArg): TDictionaryMap {
|
||||||
const dictionaryMap: { [key: string]: number } = {};
|
const dictionaryMap: TDictionaryMap = {};
|
||||||
for (const wordArg of this.dictionary) {
|
for (const wordArg of this.dictionary) {
|
||||||
dictionaryMap[wordArg] = plugins.leven(stringArg, wordArg);
|
dictionaryMap[wordArg] = plugins.leven(stringArg, wordArg);
|
||||||
}
|
}
|
||||||
console.log(dictionaryMap);
|
return dictionaryMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
getClosestMatchForString(stringArg: string) {
|
getClosestMatchForString(stringArg: string): string {
|
||||||
const fuseDictionary: { name: string }[] = [];
|
const fuseDictionary: { name: string }[] = [];
|
||||||
for (const wordArg of this.dictionary) {
|
for (const wordArg of this.dictionary) {
|
||||||
fuseDictionary.push({
|
fuseDictionary.push({
|
||||||
name: wordArg
|
name: wordArg
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const options = {
|
const fuseOptions = {
|
||||||
shouldSort: true,
|
shouldSort: true,
|
||||||
threshold: 0.6,
|
threshold: 0.6,
|
||||||
location: 0,
|
location: 0,
|
||||||
@ -48,8 +50,12 @@ export class Smartfuzzy {
|
|||||||
minMatchCharLength: 1,
|
minMatchCharLength: 1,
|
||||||
keys: ['name']
|
keys: ['name']
|
||||||
};
|
};
|
||||||
const fuse = new plugins.fuseJs(fuseDictionary, options);
|
const fuse = new plugins.fuseJs(fuseDictionary, fuseOptions);
|
||||||
const result = fuse.search(stringArg);
|
const fuzzyResult = fuse.search(stringArg);
|
||||||
console.log(result);
|
let closestMatch: string = null;
|
||||||
|
if(fuzzyResult.length > 0) {
|
||||||
|
closestMatch = fuzzyResult[0].name;
|
||||||
|
}
|
||||||
|
return closestMatch;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user