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

exact-search

v1.2.2

Published

A search to return items from a JSON array that match exactly

Downloads

466

Readme

Exact Search

exact-search is a lightweight, 0-dependency library designed to perform exact searches on a JSON array. It allows you to specify fields to index and fields to return in the search results, making it flexible for various use cases.

Installation

To install the package, use npm:

npm install exact-search

Constructor

The createClient function is used to initialize the search client. It requires an object with the following properties:

  • data: An array of objects that you want to search through.
  • matchFields: An array of strings specifying which fields in the data should be indexed for searching.
  • dataFields: An array of strings specifying which fields should be included in the search results.

API Parameters

  • search(query: string, limit: number): Performs a search on the indexed data.
    • query: A string representing the search term.
    • limit: A number specifying the maximum number of results to return.

API Response

The search results are returned as an array of objects, each containing the following properties:

| Property | Type | Contents/Structure | Description | | -------- | ------ | ------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | match | Object | { fieldName: { data: string, index: number } } | An object containing the fields that matched the search query. Each field includes the entire original field value and the index where the match starts. | | data | Object | { fieldName: fieldValue } | An object containing the fields specified in dataFields from the original data. | | score | Number | - | A number representing the relevance of the result. The score is calculated based on the number of matches found in the matchFields and the length of the field. Specifically, it is the ratio of the number of matches to the number of words in the field. |

Example response:

[
  {
    "match": {
      "name": {
        "data": "Alice Loves",
        "index": 6
      },
      "bio": {
        "data": "Loves programming",
        "index": 0
      }
    },
    "score": 1,
    "data": {
      "id": 1,
      "name": "Alice Loves",
      "bio": "Loves programming",
      "age": 30
    }
  },
  {
    "match": {
      "bio": {
        "data": "Loves code",
        "index": 0
      }
    },
    "score": 0.5,
    "data": {
      "id": 4,
      "name": "Dave",
      "bio": "Loves code",
      "age": 35
    }
  }
]

Usage (Typescript)

Here's a basic example of how to use exact-search:

import { createClient, SearchResult } from 'exact-search';

// Define the data
const data = [
  { id: 1, name: 'Alice Loves', bio: 'Loves programming', age: 30, isActive: true },
  { id: 2, name: 'Bob', bio: 'Enjoys hiking', age: 25, isActive: false },
  { id: 3, name: 'Charlie', bio: 'Loves to travel', age: 35, isActive: true },
  { id: 4, name: 'Dave', bio: 'Loves code', age: 35, isActive: true },
];

// Define the Result type
type Result = {
  match: {
    name: string;
    bio: string;
  };
  data: {
    id: number;
    name: string;
    bio: string;
    age: number;
  };
};

// Create the search client without specifying generic parameters
const index = createClient<Result>({
  data,
  matchFields: ['name', 'bio'],
  dataFields: ['id', 'name', 'bio', 'age'],
});

// Perform a search
const searchResults: SearchResult<Result>[] = index.search('Loves', 2);

Usage (Javascript)

import { createClient } from 'exact-search';

// Define the data
const data = [
  { id: 1, name: 'Alice Loves', bio: 'Loves programming', age: 30, isActive: true },
  { id: 2, name: 'Bob', bio: 'Enjoys hiking', age: 25, isActive: false },
  { id: 3, name: 'Charlie', bio: 'Loves to travel', age: 35, isActive: true },
  { id: 4, name: 'Dave', bio: 'Loves code', age: 35, isActive: true },
];

// Create the search client without specifying generic parameters
const index = createClient({
  data,
  matchFields: ['name', 'bio'],
  dataFields: ['id', 'name', 'bio', 'age'],
});

// Perform a search
const searchResults = index.search('Loves', 10);

console.log(searchResults);