fix(core): update

This commit is contained in:
2021-10-03 17:12:02 +02:00
parent cbc72cd55b
commit cb5a24320c
10 changed files with 25536 additions and 429 deletions

View File

@ -1,2 +1,3 @@
export * from './smartfuzzy.articlesearch';
export * from './smartfuzzy.classes.smartfuzzy';
export * from './smartfuzzy.classes.objectsorter';

View 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);
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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 };