Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
de87e314c0 | |||
cddd1163ec | |||
0fb86bc21b | |||
cb5a24320c | |||
cbc72cd55b | |||
a2d8f9575d | |||
6da6c6cca9 | |||
dc856dd5c7 | |||
fdf9ec6358 | |||
8021e9f96f |
25791
package-lock.json
generated
25791
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
17
package.json
17
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "smartfuzzy",
|
"name": "@pushrocks/smartfuzzy",
|
||||||
"version": "1.0.2",
|
"version": "1.1.3",
|
||||||
"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",
|
||||||
@ -13,14 +13,15 @@
|
|||||||
"build": "(tsbuild)"
|
"build": "(tsbuild)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.0.22",
|
"@gitzone/tsbuild": "^2.1.27",
|
||||||
"@gitzone/tstest": "^1.0.15",
|
"@gitzone/tstest": "^1.0.57",
|
||||||
"@pushrocks/tapbundle": "^3.0.5",
|
"@pushrocks/tapbundle": "^3.2.14",
|
||||||
"@types/node": "^10.7.1"
|
"@types/node": "^10.7.1"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/leven": "^2.1.1",
|
"@pushrocks/smartpromise": "^3.1.6",
|
||||||
"fuse.js": "^3.2.1",
|
"@tsclass/tsclass": "^3.0.33",
|
||||||
"leven": "^2.1.0"
|
"fuse.js": "^6.4.6",
|
||||||
|
"leven": "^3.1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
34
test/test.articlesearch.ts
Normal file
34
test/test.articlesearch.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
|
import * as tsclass from '@tsclass/tsclass';
|
||||||
|
import * as smartfuzzy from '../ts/index';
|
||||||
|
|
||||||
|
tap.test('should sort objects', async () => {
|
||||||
|
const articleArray: tsclass.content.IArticle[] = [
|
||||||
|
{
|
||||||
|
title: 'Berlin has a ambivalent history',
|
||||||
|
content: 'it is known that Berlin has an interesting history',
|
||||||
|
author: null,
|
||||||
|
tags: ['city', 'Europe', 'hello'],
|
||||||
|
timestamp: Date.now(),
|
||||||
|
featuredImageUrl: null,
|
||||||
|
url: null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Washington is a great city',
|
||||||
|
content: 'it is known that Washington is one of the greatest cities in the world',
|
||||||
|
author: null,
|
||||||
|
tags: ['city', 'USA', 'hello'],
|
||||||
|
timestamp: Date.now(),
|
||||||
|
featuredImageUrl: null,
|
||||||
|
url: null
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const testArticleSearch = new smartfuzzy.ArticleSearch(articleArray);
|
||||||
|
|
||||||
|
const result = await testArticleSearch.search('USA');
|
||||||
|
console.log(result);
|
||||||
|
console.log(result[0].matches)
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.start();
|
21
test/test.objectsorter.ts
Normal file
21
test/test.objectsorter.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
|
import * as smartfuzzy from '../ts/index';
|
||||||
|
|
||||||
|
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();
|
@ -14,11 +14,13 @@ 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.start();
|
tap.start();
|
@ -1 +1,3 @@
|
|||||||
|
export * from './smartfuzzy.articlesearch';
|
||||||
export * from './smartfuzzy.classes.smartfuzzy';
|
export * from './smartfuzzy.classes.smartfuzzy';
|
||||||
|
export * from './smartfuzzy.classes.objectsorter';
|
||||||
|
65
ts/smartfuzzy.articlesearch.ts
Normal file
65
ts/smartfuzzy.articlesearch.ts
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
import * as plugins from './smartfuzzy.plugins';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* an article search that searches articles in a weighted manner
|
||||||
|
*/
|
||||||
|
export class ArticleSearch {
|
||||||
|
public articles: plugins.tsclass.content.IArticle[] = [];
|
||||||
|
public needsUpdate: boolean = false;
|
||||||
|
|
||||||
|
private readyDeferred = plugins.smartpromise.defer();
|
||||||
|
private fuse: plugins.fuseJs<plugins.tsclass.content.IArticle>;
|
||||||
|
|
||||||
|
constructor(articleArrayArg?: plugins.tsclass.content.IArticle[]) {
|
||||||
|
this.fuse = new plugins.fuseJs(this.articles);
|
||||||
|
this.readyDeferred.resolve();
|
||||||
|
if (articleArrayArg) {
|
||||||
|
for (const article of articleArrayArg) {
|
||||||
|
this.addArticle(article);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* allows adding an article
|
||||||
|
*/
|
||||||
|
addArticle(articleArg: plugins.tsclass.content.IArticle) {
|
||||||
|
this.articles.push(articleArg);
|
||||||
|
this.needsUpdate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* allows searching an article
|
||||||
|
*/
|
||||||
|
public async search(searchStringArg: string) {
|
||||||
|
if (this.needsUpdate) {
|
||||||
|
const oldDeferred = this.readyDeferred;
|
||||||
|
this.readyDeferred = plugins.smartpromise.defer();
|
||||||
|
this.needsUpdate = false;
|
||||||
|
if (oldDeferred.status !== 'fulfilled') {
|
||||||
|
this.readyDeferred.promise.then(oldDeferred.resolve);
|
||||||
|
}
|
||||||
|
this.fuse = new plugins.fuseJs(this.articles, {
|
||||||
|
keys: [
|
||||||
|
{
|
||||||
|
name: 'title',
|
||||||
|
weight: 3,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'tags',
|
||||||
|
weight: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'content',
|
||||||
|
weight: 1,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
includeMatches: true
|
||||||
|
});
|
||||||
|
this.readyDeferred.resolve();
|
||||||
|
} else {
|
||||||
|
await this.readyDeferred.promise;
|
||||||
|
}
|
||||||
|
return this.fuse.search(searchStringArg);
|
||||||
|
}
|
||||||
|
}
|
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[]): plugins.fuseJs.FuseResult<T>[] {
|
||||||
|
const fuseOptions = {
|
||||||
|
shouldSort: true,
|
||||||
|
threshold: 0.6,
|
||||||
|
location: 0,
|
||||||
|
distance: 100,
|
||||||
|
maxPatternLength: 32,
|
||||||
|
minMatchCharLength: 1,
|
||||||
|
keys: objectKeysArg
|
||||||
|
};
|
||||||
|
const fuse = new plugins.fuseJs<T>(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: string): 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].item.name
|
||||||
|
}
|
||||||
|
return closestMatch;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,19 @@
|
|||||||
import leven = require('leven');
|
// @pushrocks scope
|
||||||
const fuseJs = require('fuse.js');
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
|
||||||
|
export {
|
||||||
|
smartpromise
|
||||||
|
}
|
||||||
|
|
||||||
|
// @tsclass scope
|
||||||
|
import * as tsclass from '@tsclass/tsclass';
|
||||||
|
|
||||||
|
export {
|
||||||
|
tsclass
|
||||||
|
}
|
||||||
|
|
||||||
|
// third party scope
|
||||||
|
import leven from 'leven';
|
||||||
|
import fuseJs from 'fuse.js';
|
||||||
|
|
||||||
export { leven, fuseJs };
|
export { leven, fuseJs };
|
||||||
|
Reference in New Issue
Block a user