Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6f85841
Fixing for angular 10 typings issues
mickael-moreau Apr 9, 2021
e90d99c
Fixing for angular 10 typings issues
mickael-moreau Apr 9, 2021
cdad899
Fixing for angular 10 typings issues
mickael-moreau Apr 9, 2021
a79d17a
Fixing for angular 10 typings issues
mickael-moreau Apr 9, 2021
75fdfce
Fixing for angular 10 typings issues
mickael-moreau Apr 9, 2021
c367f40
Fixing for angular 10 typings issues
mickael-moreau Apr 9, 2021
3ece9db
Building files for package ready as nodemodule git ref
mickael-moreau Apr 9, 2021
fa1f17b
Building files for package ready as nodemodule git ref
mickael-moreau Apr 9, 2021
b64faf4
Building files for package ready as nodemodule git ref
mickael-moreau Apr 9, 2021
416cc9c
Update README.md
mickael-moreau Apr 9, 2021
41483b0
Add highlightTag feature to chose injected highlight tag
mickael-moreau Apr 9, 2021
28621df
Merge branch 'master' of github.com:MonwooServices/angular-fusejs
mickael-moreau Apr 9, 2021
ae1e8b9
Cleanning prod files
mickael-moreau Apr 9, 2021
88f233f
Cleanning prod files
mickael-moreau Apr 9, 2021
ba0ed8f
Adding build script for production files
mickael-moreau Apr 26, 2021
976752b
Prod build in progress
mickael-moreau Apr 26, 2021
5f3e965
Prod build in progress
mickael-moreau Apr 26, 2021
eb94736
Prod build in progress, bump version to test
mickael-moreau Apr 26, 2021
91da6ee
Prod build in progress, trying to solve missing @NgModule annotation …
mickael-moreau Apr 26, 2021
3b772a2
Prod build in progress, trying to solve missing @NgModule annotation …
mickael-moreau Apr 26, 2021
35b8813
Prod build, package build ok
mickael-moreau Apr 26, 2021
075a72c
Prod build, package build ok
mickael-moreau Apr 26, 2021
ed85cf8
Update Readme with fork updates
mickael-moreau Apr 27, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ yarn-error.log
*.d.ts

*.js.map
npm-debug.log
npm-debug.log
yarn.lock
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"workbench.colorCustomizations": {
"statusBar.background": "#BDB76B",
"statusBar.debuggingBackground": "#BDB76B",
"statusBar.noFolderBackground": "#BDB76B",
"statussBar.prominentBackground": "#BDB76B"
}
}
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,39 @@ Also easily **highlight** matched terms which is the true added value of this mo

Demo: <https://maleblond.github.io/angular-fusejs/>

## Forked by Miguel Monwoo for Angular 10 compatibilities
Watting for Author updates, did fix missing type to fit angular-fusejs with last fusejs lib and fix some production issue, all seem ok with Angular 10.

Production build inspired from :
https://github.com/malick-dev/angular-fusejs/commit/89d005037f62728c734904a155f996699ab882bb

https://github.com/ng-packagr/ng-packagr/issues/355

https://github.com/abbazabacto/ngpackagr-barrel-issue/blob/master/package.json

https://www.typescriptlang.org/tsconfig#allowSyntheticDefaultImports

And fixed with ng-packagr

```bash
# Just use this fork dist build in your project :
yarn upgrade angular-fusejs@git+https://github.com/MonwooServices/angular-fusejs.git#DistBuild


# Build the fork sources for production (in ./dist):
yarn install
yarn clean && yarn package

```


## How to install?

This module is available through npm. It is compatible with module bundlers (webpack / browserify ...).

1. `npm install angular-fusejs`
2. Import `FusejsModule` into your module:
1. `# npm install angular-fusejs`
2. `npm upgrade angular-fusejs@git+https://github.com/MonwooServices/angular-fusejs.git#DistBuild`
3. Import `FusejsModule` into your module:
```
import {FusejsModule} from 'angular-fusejs'
@NgModule({
Expand Down Expand Up @@ -71,4 +98,4 @@ Replace `whateverAttribute` with your attribute name, trust it as HTML and you a

I made this just for fun, to learn a bit more about fuzzy search + angular. Fuzzy search + detecting what should be highlighted is 100% handled by [fuse.js](http://fusejs.io/).

If you encounter any bug, please check who is the culprit before submitting an issue - it might be a bug in fuse.js library.
If you encounter any bug, please check who is the culprit before submitting an issue - it might be a bug in fuse.js library.
35 changes: 0 additions & 35 deletions index.js

This file was deleted.

16 changes: 1 addition & 15 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
import { NgModule } from '@angular/core'
import { FusejsPipe } from './src/fusejs.pipe'
import { FusejsService } from './src/fusejs.service'

@NgModule({
providers: [
FusejsService
],
declarations: [
FusejsPipe,
],
exports: [
FusejsPipe,
]
})
export class FusejsModule {}
export * from './src/fusejs.module';
export * from './src/fusejs.service';
export * from './src/fusejs.pipe';
45 changes: 34 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,45 @@
{
"$schema": "./node_modules/ng-packagr/package.schema.json",
"name": "angular-fusejs",
"version": "1.0.1",
"version": "1.0.6",
"description": "Angular plugin using fuse.js to search through a list of elements with a string. Supports highlight.",
"main": "index.js",
"type": "module",
"ngPackage": {
"lib": {
"entryFile": "index.ts"
},
"allowedNonPeerDependencies": [
"."
]
},
"dependencies": {
"fuse.js": "^3.2.0",
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2"
"@angular/core": "10.2.0",
"fuse.js": "^3.6.1",
"lodash-es": "^4.17.21",
"rxjs": "^6.6.7",
"tslib": "^2.1.0",
"zone.js": "~0.10.3"
},
"peerDependencies": {
"@angular/core": "^2.0.0"
"@angular/core": "10.2.0"
},
"devDependencies": {
"@angular/compiler": "10.2.0",
"@angular/compiler-cli": "10.2.0",
"@types/lodash-es": "^4.17.4",
"@types/lodash.get": "^4.4.2",
"@types/lodash.set": "^4.3.2",
"@types/node": "^13.1.8",
"ng-packagr": "^11.2.4",
"typescript": "3.9.7"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"clean": "rm -rf ./dist",
"copy-files": "cp README.md LICENSE package.json tsconfig.json dist/",
"package": "ng-packagr -p package.json",
"build": "tsc -p . && yarn copy-files"
},
"repository": {
"type": "git",
Expand All @@ -28,9 +55,5 @@
"bugs": {
"url": "https://github.com/maleblond/angular-local-search/issues"
},
"homepage": "https://github.com/maleblond/angular-local-search#readme",
"devDependencies": {
"@types/lodash.get": "^4.4.2",
"@types/lodash.set": "^4.3.2"
}
"homepage": "https://github.com/maleblond/angular-local-search#readme"
}
16 changes: 16 additions & 0 deletions src/fusejs.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NgModule } from '@angular/core'
import { FusejsPipe } from './fusejs.pipe'
import { FusejsService } from './fusejs.service'

@NgModule({
providers: [
FusejsService
],
declarations: [
FusejsPipe,
],
exports: [
FusejsPipe,
]
})
export class FusejsModule {};
10 changes: 5 additions & 5 deletions src/fusejs.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import {FusejsService, AngularFusejsOptions} from './fusejs.service';


@Pipe({name: 'fusejs'})
export class FusejsPipe implements PipeTransform {
export class FusejsPipe<T> implements PipeTransform {
constructor(
private FusejsService: FusejsService
private FusejsService: FusejsService<T>
) {}

transform(elements: Array<Object>,
transform(elements: Array<T>,
searchTerms: string,
options: AngularFusejsOptions = {}) {
options: AngularFusejsOptions<T> = {}) {
return this.FusejsService.searchList(elements, searchTerms, options);
}
}
}
53 changes: 29 additions & 24 deletions src/fusejs.service.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import {Injectable} from '@angular/core'
import * as Fuse from 'fuse.js'
import FuseOptions = Fuse.FuseOptions;
import {Injectable} from '@angular/core';
import * as Fuse from 'fuse.js';
import {FuseOptions} from 'fuse.js'

import _set = require('lodash.set');
import _get = require('lodash.get');
import {set as _set} from 'lodash-es';
import {get as _get} from 'lodash-es';


export interface AngularFusejsOptions extends FuseOptions {
export interface AngularFusejsOptions<T> extends FuseOptions<T> {
supportHighlight?: boolean;
fusejsHighlightKey?: string;
fusejsScoreKey?: string;
minSearchTermLength?: number;
minSearchTermLength?: number; // = 0;
maximumScore?: number;
highlightTag?: string;
}

@Injectable()
export class FusejsService {
private defaultOptions: AngularFusejsOptions = {
export class FusejsService<T> {
private defaultOptions: AngularFusejsOptions<T> = {
supportHighlight: true,
shouldSort: false,
threshold: 0.6,
Expand All @@ -30,11 +31,13 @@ export class FusejsService {
fusejsScoreKey: 'fuseJsScore',
};

searchList(list: Array<any>, searchTerms: string, options: AngularFusejsOptions = {}) {
const fuseOptions: AngularFusejsOptions = Object.assign({}, this.defaultOptions, options);
let result = [];
searchList(list: Array<T>, searchTerms: string, options: AngularFusejsOptions<T> = {}) {
// https://stackoverflow.com/questions/35959372/property-assign-does-not-exist-on-type-objectconstructor
// TODO : remove (<any>Object) hack by using right lib or polyfill ?
const fuseOptions: AngularFusejsOptions<T> = (<any>Object).assign({}, this.defaultOptions, options);
let result:any = [];

if (searchTerms && searchTerms.length >= fuseOptions.minSearchTermLength) {
if (searchTerms && searchTerms.length >= (fuseOptions?.minSearchTermLength || 0)) {
if (fuseOptions.supportHighlight) {
fuseOptions.includeMatches = true;
}
Expand All @@ -49,7 +52,7 @@ export class FusejsService {

if (fuseOptions.supportHighlight) {
result.forEach((element) => {
element[fuseOptions.fusejsHighlightKey] = this.deepClone(element);
element[fuseOptions.fusejsHighlightKey || '_'] = this.deepClone(element);
});
}
}
Expand All @@ -67,40 +70,42 @@ export class FusejsService {
return _out;
}

private handleHighlight(result, options: AngularFusejsOptions) {
private handleHighlight(result, options: AngularFusejsOptions<T>) {
if (options.maximumScore && options.includeScore) {
result = result.filter((matchObject) => {
return matchObject.score <= options.maximumScore;
return matchObject.score <= (options.maximumScore||0);
})
}

return result.map((matchObject) => {
const item = this.deepClone(matchObject.item);
item[options.fusejsHighlightKey] = this.deepClone(item);
item[options.fusejsScoreKey] = matchObject.score;
item[options.fusejsHighlightKey || "_"] = this.deepClone(item);
item[options.fusejsScoreKey || "_"] = matchObject.score;
for (let match of matchObject.matches) {
const indices: number[][] = match.indices;

let highlightOffset: number = 0;

let key: string = match.key;
if(_get(item[options.fusejsHighlightKey], key).constructor === Array) {
if(_get(item[options.fusejsHighlightKey || "_"], key).constructor === Array) {
key += `[${match.arrayIndex}]`
}

for (let indice of indices) {
let initialValue: string = _get(item[options.fusejsHighlightKey], key) as string;
let initialValue: string = _get(item[options.fusejsHighlightKey || "_"], key) as string;

const startOffset = indice[0] + highlightOffset;
const endOffset = indice[1] + highlightOffset + 1;
const tagStart = "<" + (options.highlightTag ?? "em") + ">";
const tagEnd = "</" + (options.highlightTag ?? "em") + ">";
let highlightedTerm = initialValue.substring(startOffset, endOffset);
let newValue = initialValue.substring(0, startOffset) + '<em>' + highlightedTerm + '</em>' + initialValue.substring(endOffset);
highlightOffset += '<em></em>'.length;
_set(item[options.fusejsHighlightKey], key, newValue);
let newValue = initialValue.substring(0, startOffset) + tagStart + highlightedTerm + tagEnd + initialValue.substring(endOffset);
highlightOffset += (tagStart + tagEnd).length;
_set(item[options.fusejsHighlightKey || "_"], key, newValue);
}
}

return item;
});
}
}
}
27 changes: 23 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,34 @@
"exclude": [
"node_modules"
],
"include": [
"src/**/*"
],
"lib": [
"es2015",
"es2017",
"esnext",
"dom"
],
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"target": "es6",
"esModuleInterop": true,
"noImplicitAny": false,
"sourceMap": false,
"declaration": true,
"declarationMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"moduleResolution": "node",
"skipLibCheck": true
}
}
"skipLibCheck": false,
"outDir": "./dist",
"baseUrl": "./",
"rootDir": "./",
"allowSyntheticDefaultImports": true,
"strict": true
},
"angularCompilerOptions": {
"enableIvy": false
}
}
Loading