fix(core): update
This commit is contained in:
@ -1,2 +1,3 @@
|
||||
export * from './smartfuzzy.articlesearch';
|
||||
export * from './smartfuzzy.classes.smartfuzzy';
|
||||
export * from './smartfuzzy.classes.objectsorter';
|
||||
|
64
ts/smartfuzzy.articlesearch.ts
Normal file
64
ts/smartfuzzy.articlesearch.ts
Normal file
@ -0,0 +1,64 @@
|
||||
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,
|
||||
},
|
||||
],
|
||||
});
|
||||
this.readyDeferred.resolve();
|
||||
} else {
|
||||
await this.readyDeferred.promise;
|
||||
}
|
||||
return this.fuse.search(searchStringArg);
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ export class ObjectSorter<T> {
|
||||
this.objectDictionary = objectDictionaryArg;
|
||||
}
|
||||
|
||||
sort(stringArg: string, objectKeysArg: string[]): T[] {
|
||||
sort(stringArg: string, objectKeysArg: string[]): plugins.fuseJs.FuseResult<T>[] {
|
||||
const fuseOptions = {
|
||||
shouldSort: true,
|
||||
threshold: 0.6,
|
||||
@ -18,7 +18,7 @@ export class ObjectSorter<T> {
|
||||
minMatchCharLength: 1,
|
||||
keys: objectKeysArg
|
||||
};
|
||||
const fuse = new plugins.fuseJs(this.objectDictionary, fuseOptions);
|
||||
const fuse = new plugins.fuseJs<T>(this.objectDictionary, fuseOptions);
|
||||
const result = fuse.search(stringArg);
|
||||
return result;
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ export class Smartfuzzy {
|
||||
* returns the closest match for a given string
|
||||
* @param stringArg
|
||||
*/
|
||||
getChangeScoreForString(stringArg): TDictionaryMap {
|
||||
getChangeScoreForString(stringArg: string): TDictionaryMap {
|
||||
const dictionaryMap: TDictionaryMap = {};
|
||||
for (const wordArg of this.dictionary) {
|
||||
dictionaryMap[wordArg] = plugins.leven(stringArg, wordArg);
|
||||
@ -54,7 +54,7 @@ export class Smartfuzzy {
|
||||
const fuzzyResult = fuse.search(stringArg);
|
||||
let closestMatch: string = null;
|
||||
if(fuzzyResult.length > 0) {
|
||||
closestMatch = fuzzyResult[0].name;
|
||||
closestMatch = fuzzyResult[0].item.name
|
||||
}
|
||||
return closestMatch;
|
||||
}
|
||||
|
@ -1,4 +1,19 @@
|
||||
import leven = require('leven');
|
||||
const fuseJs = require('fuse.js');
|
||||
// @pushrocks scope
|
||||
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 };
|
||||
|
Reference in New Issue
Block a user