11 Commits

Author SHA1 Message Date
4eead78c55 1.1.6 2021-10-04 13:43:57 +02:00
fa262af6a4 fix(core): update 2021-10-04 13:43:57 +02:00
58f7458acd 1.1.5 2021-10-03 17:38:36 +02:00
34d8c86856 1.1.4 2021-10-03 17:37:03 +02:00
d4cea26fb5 fix(core): update 2021-10-03 17:37:03 +02:00
de87e314c0 1.1.3 2021-10-03 17:24:17 +02:00
cddd1163ec fix(core): update 2021-10-03 17:24:17 +02:00
0fb86bc21b 1.1.2 2021-10-03 17:12:03 +02:00
cb5a24320c fix(core): update 2021-10-03 17:12:02 +02:00
cbc72cd55b 1.1.1 2018-08-20 00:49:01 +02:00
a2d8f9575d fix(Smartfuzzy.getClosestMatchForString() now returns the cloesest string directly): update 2018-08-20 00:49:01 +02:00
18 changed files with 25768 additions and 533 deletions

16
.gitignore vendored
View File

@ -1,6 +1,20 @@
.nogit/ .nogit/
node_modules/
# artifacts
coverage/ coverage/
public/ public/
pages/ pages/
# installs
node_modules/
# caches
.yarn/ .yarn/
.cache/
.rpt2_cache
# builds
dist/
dist_*/
# custom

View File

@ -1,16 +1,16 @@
# gitzone standard # gitzone ci_default
image: hosttoday/ht-docker-node:npmci image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
cache: cache:
paths: paths:
- .npmci_cache/ - .npmci_cache/
key: "$CI_BUILD_STAGE" key: '$CI_BUILD_STAGE'
stages: stages:
- security - security
- test - test
- release - release
- metadata - metadata
# ==================== # ====================
# security stage # security stage
@ -18,109 +18,115 @@ stages:
mirror: mirror:
stage: security stage: security
script: script:
- npmci git mirror - npmci git mirror
only:
- tags
tags: tags:
- docker - lossless
- notpriv - docker
- notpriv
snyk: auditProductionDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security stage: security
script: script:
- npmci command npm install -g snyk - npmci npm prepare
- npmci command npm install --ignore-scripts - npmci command npm install --production --ignore-scripts
- npmci command snyk test - npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=prod --production
tags: tags:
- docker - docker
- notpriv allow_failure: true
auditDevDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security
script:
- npmci npm prepare
- npmci command npm install --ignore-scripts
- npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=dev
tags:
- docker
allow_failure: true
# ==================== # ====================
# test stage # test stage
# ==================== # ====================
testLEGACY:
stage: test
script:
- npmci node install legacy
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
allow_failure: true
testLTS: testStable:
stage: test stage: test
script: script:
- npmci node install lts - npmci npm prepare
- npmci npm install - npmci node install stable
- npmci npm test - npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/ coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- docker - docker
- notpriv
testBuild:
testSTABLE:
stage: test stage: test
script: script:
- npmci node install stable - npmci npm prepare
- npmci npm install - npmci node install stable
- npmci npm test - npmci npm install
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/ coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- docker - docker
- notpriv
release: release:
stage: release stage: release
script: script:
- npmci node install stable - npmci node install stable
- npmci npm publish - npmci npm publish
only: only:
- tags - tags
tags: tags:
- docker - lossless
- notpriv - docker
- notpriv
# ==================== # ====================
# metadata stage # metadata stage
# ==================== # ====================
codequality: codequality:
stage: metadata stage: metadata
image: docker:stable
allow_failure: true allow_failure: true
services: only:
- docker:stable-dind - tags
script: script:
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/') - npmci command npm install -g tslint typescript
- docker run - npmci npm prepare
--env SOURCE_CODE="$PWD" - npmci npm install
--volume "$PWD":/code - npmci command "tslint -c tslint.json ./ts/**/*.ts"
--volume /var/run/docker.sock:/var/run/docker.sock
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
artifacts:
paths: [codeclimate.json]
tags: tags:
- docker - lossless
- priv - docker
- priv
trigger: trigger:
stage: metadata stage: metadata
script: script:
- npmci trigger - npmci trigger
only: only:
- tags - tags
tags: tags:
- docker - lossless
- notpriv - docker
- notpriv
pages: pages:
image: hosttoday/ht-docker-node:npmci
stage: metadata stage: metadata
script: script:
- npmci command npm install -g typedoc typescript - npmci node install lts
- npmci command npm install -g @gitzone/tsdoc
- npmci npm prepare
- npmci npm install - npmci npm install
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/ - npmci command tsdoc
tags: tags:
- lossless
- docker - docker
- notpriv - notpriv
only: only:
@ -128,15 +134,5 @@ pages:
artifacts: artifacts:
expire_in: 1 week expire_in: 1 week
paths: paths:
- public - public
allow_failure: true
windowsCompatibility:
image: stefanscherer/node-windows:10-build-tools
stage: metadata
script:
- npm install & npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- windows
allow_failure: true allow_failure: true

11
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,11 @@
{
"version": "0.2.0",
"configurations": [
{
"command": "npm test",
"name": "Run npm test",
"request": "launch",
"type": "node-terminal"
}
]
}

26
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,26 @@
{
"json.schemas": [
{
"fileMatch": ["/npmextra.json"],
"schema": {
"type": "object",
"properties": {
"npmci": {
"type": "object",
"description": "settings for npmci"
},
"gitzone": {
"type": "object",
"description": "settings for gitzone",
"properties": {
"projectType": {
"type": "string",
"enum": ["website", "element", "service", "npm", "wcc"]
}
}
}
}
}
}
]
}

19
license Normal file
View File

@ -0,0 +1,19 @@
Copyright (c) 2014 Lossless GmbH (hello@lossless.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -1,6 +1,17 @@
{ {
"gitzone": {
"projectType": "npm",
"module": {
"githost": "gitlab.com",
"gitscope": "pushrocks",
"gitrepo": "smartfuzzy",
"shortDescription": "search things easily",
"npmPackagename": "@pushrocks/smartfuzzy",
"license": "MIT"
}
},
"npmci": { "npmci": {
"npmGlobalTools": [], "npmGlobalTools": [],
"npmAccessLevel": "public" "npmAccessLevel": "public"
} }
} }

25788
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +1,10 @@
{ {
"name": "@pushrocks/smartfuzzy", "name": "@pushrocks/smartfuzzy",
"version": "1.1.0", "version": "1.1.6",
"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_ts/index.js",
"typings": "dist/index.d.ts", "typings": "dist_ts/index.d.ts",
"author": "Lossless GmbH", "author": "Lossless GmbH",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
@ -13,14 +13,30 @@
"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": "^16.10.2"
}, },
"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"
},
"browserslist": [
"last 1 chrome versions"
],
"files": [
"ts/**/*",
"ts_web/**/*",
"dist/**/*",
"dist_*/**/*",
"dist_ts/**/*",
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
"npmextra.json",
"readme.md"
]
} }

39
readme.md Normal file
View File

@ -0,0 +1,39 @@
# @pushrocks/smartfuzzy
search things easily
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartfuzzy)
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartfuzzy)
* [github.com (source mirror)](https://github.com/pushrocks/smartfuzzy)
* [docs (typedoc)](https://pushrocks.gitlab.io/smartfuzzy/)
## Status for master
Status Category | Status Badge
-- | --
GitLab Pipelines | [![pipeline status](https://gitlab.com/pushrocks/smartfuzzy/badges/master/pipeline.svg)](https://lossless.cloud)
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/pushrocks/smartfuzzy/badges/master/coverage.svg)](https://lossless.cloud)
npm | [![npm downloads per month](https://badgen.net/npm/dy/@pushrocks/smartfuzzy)](https://lossless.cloud)
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/pushrocks/smartfuzzy)](https://lossless.cloud)
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@pushrocks/smartfuzzy)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@pushrocks/smartfuzzy)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@pushrocks/smartfuzzy)](https://lossless.cloud)
Platform support | [![Supports Windows 10](https://badgen.net/badge/supports%20Windows%2010/yes/green?icon=windows)](https://lossless.cloud) [![Supports Mac OS X](https://badgen.net/badge/supports%20Mac%20OS%20X/yes/green?icon=apple)](https://lossless.cloud)
## Usage
Use TypeScript for best in class intellisense.
## Contribution
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
For further information read the linked docs at the top of this readme.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)

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

View File

@ -8,7 +8,7 @@ tap.test('should create an instance of Smartfuzzy', async () => {
'Sony', 'Sony',
'Deutsche Bahn', 'Deutsche Bahn',
'Apple Inc.', 'Apple Inc.',
"Trader Joe's" "Trader Joe's",
]); ]);
expect(testSmartfuzzy).to.be.instanceof(smartfuzzy.Smartfuzzy); expect(testSmartfuzzy).to.be.instanceof(smartfuzzy.Smartfuzzy);
}); });
@ -23,21 +23,4 @@ tap.test('should get closest match', async () => {
console.log(result); 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();

View File

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

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

View File

@ -3,12 +3,11 @@ import * as plugins from './smartfuzzy.plugins';
export class ObjectSorter<T> { export class ObjectSorter<T> {
public objectDictionary: T[]; public objectDictionary: T[];
constructor(objectDictionaryArg: T[] = []) { constructor(objectDictionaryArg: T[] = []) {
this.objectDictionary = objectDictionaryArg; this.objectDictionary = objectDictionaryArg;
} }
sort(stringArg: string, objectKeysArg: string[]): T[] { sort(stringArg: string, objectKeysArg: string[]): plugins.fuseJs.FuseResult<T>[] {
const fuseOptions = { const fuseOptions = {
shouldSort: true, shouldSort: true,
threshold: 0.6, threshold: 0.6,
@ -16,11 +15,10 @@ export class ObjectSorter<T> {
distance: 100, distance: 100,
maxPatternLength: 32, maxPatternLength: 32,
minMatchCharLength: 1, minMatchCharLength: 1,
keys: objectKeysArg keys: objectKeysArg,
}; };
const fuse = new plugins.fuseJs(this.objectDictionary, fuseOptions); const fuse = new plugins.fuseJs<T>(this.objectDictionary, fuseOptions);
const result = fuse.search(stringArg); const result = fuse.search(stringArg);
return result; return result;
} }
}
}

View File

@ -26,7 +26,7 @@ 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): TDictionaryMap { getChangeScoreForString(stringArg: string): TDictionaryMap {
const dictionaryMap: TDictionaryMap = {}; 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);
@ -34,11 +34,11 @@ export class Smartfuzzy {
return 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 fuseOptions = { const fuseOptions = {
@ -48,10 +48,14 @@ export class Smartfuzzy {
distance: 100, distance: 100,
maxPatternLength: 32, maxPatternLength: 32,
minMatchCharLength: 1, minMatchCharLength: 1,
keys: ['name'] keys: ['name'],
}; };
const fuse = new plugins.fuseJs(fuseDictionary, fuseOptions); const fuse = new plugins.fuseJs(fuseDictionary, fuseOptions);
const result = fuse.search(stringArg); const fuzzyResult = fuse.search(stringArg);
return result; let closestMatch: string = null;
if (fuzzyResult.length > 0) {
closestMatch = fuzzyResult[0].item.name;
}
return closestMatch;
} }
} }

View File

@ -1,4 +1,15 @@
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 };

View File

@ -1,7 +1,17 @@
{ {
"extends": ["tslint:latest", "tslint-config-prettier"], "extends": ["tslint:latest", "tslint-config-prettier"],
"rules": { "rules": {
"semicolon": [true, "always"] "semicolon": [true, "always"],
"no-console": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"member-ordering": {
"options":{
"order": [
"static-method"
]
}
} }
} },
"defaultSeverity": "warning"
}