npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

search-fuzzy

v1.1.2

Published

A simple fuzzy search algorithm that uses the Levenshtein distance algorithm to find the closest match to a given string.

Downloads

11

Readme

Overview Search-Fuzzy

Searching for a specific data in a large dataset can be a challenging task. The searchFuzzy utility provides a powerful solution for this problem. This utility allows you to search for a specific data in a large dataset by providing a query string and a set of fields to search in. The utility returns an array of objects that match the query.

SearchFuzzy JS Integration

const { fuzzySearch } = require("search-fuzzy/fuzzy/fuzzy");

const data = [
  {
    name: "John",
    age: 20,
  },
  {
    name: "Jane",
    age: 21,
  },
];

fuzzySearch(data, ["name", "age"], { query: "jane" }).then((res) => {
  console.log(res)[
    // output:
    ({ name: "Jane", age: 21 }, { name: "John", age: 20 })
  ];
});

JS API integration

const { fuzzySearch } = require("search-fuzzy/fuzzy/fuzzy");

const fields = [];
// Content from free api
const query = { query: "consequatur qui cupiditate rerum quia soluta" };

const apiConfig = {
  url: "https://jsonplaceholder.typicode.com/posts",
  useApi: true,
  headers: {
    Authorization: "Bearer your-token",
    Accept: "application/json",
    "Custom-Header": "custom-value", // Additional custom headers
  },
  //  This will set param to the url like
  //  https://jsonplaceholder.typicode.com/posts?id=1
  params: {
    id: 1,
  },
  // We can use without param as well
};

fuzzySearch(data, fields, query, apiConfig, { maxResults: 3 })
  .then((results) => {
    console.log(results);
  })
  .catch((error) => {
    console.error(error);
  });

SearchFuzzy Angular Integration

This project demonstrates the integration of the powerful searchFuzzy utility with Angular applications. It provides a robust mechanism for performing fuzzy searches on datasets, allowing for approximate matches in both local and remote data.

Installation

1. Install the search-fuzzy package

npm install search-fuzzy
  1. Add to your Angular project In your Angular project, import the searchFuzzy function where needed:
import { fuzzySearch, Query } from "search-fuzzy/fuzzy/fuzzy";
  • Usage Example
fuzzySearch(data, fields, query, apiConfig)
  .then((res) => {
    console.log(res);
  })
  .catch((err) => {
    console.log(err);
  });

Parameters:

data: Array An array of objects to search through.

fields: Array An array of string field names to search within each object.

query: string The search query string.

apiConfig: Object (Optional)

Configuration for searching in angular

import { Component } from "@angular/core";
import { FormsModule } from "@angular/forms";
import { fuzzySearch, Query } from "search-fuzzy/fuzzy/fuzzy";

@Component({
  selector: "app-search",
  standalone: true,
  imports: [FormsModule],
  template: `
    <section class="container mt-20 grid justify-center">
      <input [(ngModel)]="searchQuery.query" (input)="onSearch()" />
      <ul>
        @for (result of searchResults; track $index) {
        <li>{{ result.name }}</li>
        }
      </ul>
    </section>
  `,
})
export class SearchComponent {
  searchQuery: Query = { query: "" };
  searchResults: any[] = [];
  data = [
    { name: "John Doe", email: "[email protected]" },
    { name: "Jane Smith", email: "[email protected]" },
    // ... more data
  ];

  onSearch() {
    fuzzySearch(this.data, ["name", "email"], this.searchQuery).then((res) => {
      this.searchResults = res;
      // console.log(res);
    });
  }
}

API Integration

To integrate the searchFuzzy utility with your Angular application, follow these steps:

import { Component } from "@angular/core";
import { FormsModule } from "@angular/forms";
import { ApiConfig, fuzzySearch, Query } from "search-fuzzy/fuzzy/fuzzy";

@Component({
  selector: "app-search",
  standalone: true,
  imports: [FormsModule],
  template: `
    <section class="container mt-20 grid justify-center">
      <input [(ngModel)]="searchQuery.query" (input)="onSearch()" />
      <ul>
        @for (result of searchResults; track $index) {
        <li>{{ result.name }}</li>
        }
      </ul>
    </section>
  `,
})
export class SearchComponent {
  searchQuery: Query = { query: "" };
  searchResults: any[] = [];
  private apiConfig: ApiConfig = {
    url: "https://jsonplaceholder.typicode.com/posts",
    useApi: true,
    // Optinal credentials for the request
    token: "your-token",
    headers: {
      Authorization: "Bearer your-token",
      Accept: "application/json",
      "Custom-Header": "custom-value", // Additional custom headers
    },
    params: {
      // Optinal params for the request
      id: "1",
    },
  };

  onSearch() {
    fuzzySearch([], ["name", "title"], this.searchQuery, this.apiConfig)
      .then((res) => {
        this.searchResults = res;
        // console.log(res);
      })
      .catch((err) => {
        console.log(err);
      });
  }
}

Advanced Configuration

The fuzzySearch function accepts additional options for fine-tuning:

import { ApiConfig, fuzzySearch, Query } from "search-fuzzy/fuzzy/fuzzy";

const options = {
  threshold: 0.6,
  limit: 10,
};

fuzzySearch([], ["name", "email"], this.searchQuery, options)
  .then((res) => {
    console.log(res);
  })
  .catch((err) => {
    console.log(err);
  });

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.