---
title: "@push.rocks/smartfuzzy"
---
# @push.rocks/smartfuzzy

fuzzy match strings against word dictionaries/arrays

## Install

To install `@push.rocks/smartfuzzy`, use the following npm command. It's recommended to do this in a project where TypeScript is already configured:

```bash
npm install @push.rocks/smartfuzzy --save
```

## Usage

`@push.rocks/smartfuzzy` is a versatile library designed to help you perform fuzzy searches and sorts on arrays of strings and objects. Whether you're building a search feature, organizing data, or implementing autocomplete functionality, `@push.rocks/smartfuzzy` offers you the tools needed to achieve efficient and intuitive search results. Below are various scenarios to cover a broad set of features of the module, ensuring you can integrate it effectively into your TypeScript projects.

### Setting Up

First, ensure you import the necessary components:

```typescript
import {
  Smartfuzzy,
  ObjectSorter,
  ArticleSearch
} from '@push.rocks/smartfuzzy';
```

### Basic String Matching

For scenarios where you have an array of strings and you wish to find a match for a search term:

```typescript
const myDictionary = ['Sony', 'Deutsche Bahn', 'Apple Inc.', "Trader Joe's"];
const mySmartFuzzy = new Smartfuzzy(myDictionary);

// Adding additional dictionary entries
mySmartFuzzy.addToDictionary('Microsoft');
mySmartFuzzy.addToDictionary(['Google', 'Facebook']);

// Getting the closest match
const searchResult = mySmartFuzzy.getClosestMatchForString('Appl');
console.log(searchResult); // Output: "Apple Inc."
```

This example demonstrates how to instantiate the `Smartfuzzy` class with a list of strings (dictionary) and add more entries to it. You can then use it to get the closest match for a given search string.

### Advanced Object Sorting

Imagine you are managing a list of objects, and you wish to sort them based on the resemblance of one or more of their properties to a search term:

```typescript
interface ICar {
  brand: string;
  model: string;
}

const carList: ICar[] = [
  { brand: 'BMW', model: 'M3' },
  { brand: 'Mercedes Benz', model: 'E-Class' },
  { brand: 'Volvo', model: 'XC90' }
];

const carSorter = new ObjectSorter<ICar>(carList);

// Search and sort based on brand similarity
const searchResults = carSorter.sort('Benz', ['brand']);
console.log(searchResults); // Results will be sorted by relevance to 'Benz'
```

This scenario shows how to use `ObjectSorter` for sorting an array of objects based on how closely one of their string properties matches a search term. This is particularly useful for filtering or autocomplete features where relevance is key.

### Searching Within Articles

If your application involves searching through articles or similar textual content, `ArticleSearch` allows for a weighted search across multiple fields:

```typescript
import { IArticle } from '@tsclass/tsclass/content';

const articles: IArticle[] = [
  { title: 'History of Berlin', content: 'Berlin has a rich history...', tags: ['history', 'Berlin'], timestamp: Date.now(), featuredImageUrl: null, url: null },
  { title: 'Tourism in Berlin', content: 'Discover the vibrant city of Berlin...', tags: ['travel', 'Berlin'], timestamp: Date.now(), featuredImageUrl: null, url: null }
];

const articleSearch = new ArticleSearch(articles);

// Perform a search across titles, content, and tags
const searchResult = await articleSearch.search('rich history');
console.log(searchResult); // Array of matches with relevance to 'rich history'
```

The `ArticleSearch` class showcases how to implement a search feature across a collection of articles with prioritization across different fields (e.g., title, content, tags). This ensures more relevant search results and creates a better experience for users navigating through large datasets or content libraries.

### Conclusion

`@push.rocks/smartfuzzy` offers a robust set of functionalities for integrating fuzzy searching and sorting capabilities into your TypeScript applications. By following the examples demonstrated, you can effectively utilize the module to enhance user experience where text search is a critical component of the application.

Remember to always consider the specific requirements of your project when implementing these features, as adjustments to configurations such as threshold levels and keys to search on can significantly impact the effectiveness of your search functionality.

## License and Legal Information

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. 

**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

### Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

### Company Information

Task Venture Capital GmbH  
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
