fix(core): update
This commit is contained in:
parent
cbc72cd55b
commit
cb5a24320c
25849
package-lock.json
generated
25849
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
13
package.json
13
package.json
@ -13,14 +13,15 @@
|
||||
"build": "(tsbuild)"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.0.22",
|
||||
"@gitzone/tstest": "^1.0.15",
|
||||
"@pushrocks/tapbundle": "^3.0.5",
|
||||
"@gitzone/tsbuild": "^2.1.27",
|
||||
"@gitzone/tstest": "^1.0.57",
|
||||
"@pushrocks/tapbundle": "^3.2.14",
|
||||
"@types/node": "^10.7.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@types/leven": "^2.1.1",
|
||||
"fuse.js": "^3.2.1",
|
||||
"leven": "^2.1.0"
|
||||
"@pushrocks/smartpromise": "^3.1.6",
|
||||
"@tsclass/tsclass": "^3.0.33",
|
||||
"fuse.js": "^6.4.6",
|
||||
"leven": "^3.1.0"
|
||||
}
|
||||
}
|
||||
|
33
test/test.articlesearch.ts
Normal file
33
test/test.articlesearch.ts
Normal file
@ -0,0 +1,33 @@
|
||||
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);
|
||||
});
|
||||
|
||||
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();
|
@ -23,21 +23,4 @@ tap.test('should get closest match', async () => {
|
||||
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();
|
@ -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 };
|
||||
|
Loading…
Reference in New Issue
Block a user