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

@hypericon/locust

v0.4.2

Published

Local client-side search for static sites

Downloads

10

Readme

Locust 🦗

Local client-side search for static sites.

Inspired by Algolia, uses Fuse.js internally.

Locust screenshot with example data

Sample data from: https://github.com/benoitvallon/100-best-books

Quickstart

Install from NPM, including Typescript types: npm install @hypericon/locust

Prepare JSON data

Generate the JSON data with: npx locust -i ./data/input-file.json -o ./site/locust-data.json

More options and config file format are detailed below.

Search the data

Include the client script, for example:

<button id="locust-search">Search</button>

<script defer src="https://unpkg.com/@hypericon/locust/dist/lib/locust.umd.cjs"></script>
<link rel="stylesheet" href="unpkg.com/@hypericon/locust/dist/lib/style.css">

<script defer>
  setupLocustSearch({
    // Where to load the prepared data from
    jsonData: "/locust-data.json",
    // Which keys in the data objects should be indexed
    searchKeys: [
      { name: "title", weight: 2 },
      "author",
      "language",
    ],
    // How the results should be displayed
    modalResult: {
      thumbnail: "imageLink",
      fields: [
        { key: "title", style: "large" },
        { key: "author" },
        { key: "language", style: "small" },
        { text: (record) => `Year: ${record.year}`, style: "small" },
      ],
    },
  });
</script>

More options are detailed below.

Note: If you only want to consume the search results programmatically and use your own UI, you're probably better off just using Fuse.js on its own.

Overview

Locust includes two components:

  • CLI tool to prepare data
  • UI component to perform searches

Once the data is prepared, the JSON file is generally written to the the output directory of the static site. It can also be made available over HTTP from an existing server (not included).

The UI component uses the generated data to serve search requests. The UI component can load the data lazily (default) or aggressively.

Prepare Data

Locust includes tools to extract data from the following sources:

  • Javascript objects already in memory
  • HTML, Markdown, or JSON files stored locally
  • HTML, Markdown, or JSON files retrieved via HTTP

Search Data

Development

Clone the repo, install dependencies with npm install

The CLI and UI components are built separately

  • npm run dev-cli to start compiling the Typescript CLI source in watch mode
  • npm run dev-lib to start a hot-reloading local dev server serving an example page containing the UI
  • npm run dev to run both of the above scripts concurrently

The build scripts are similar:

  • npm run build-cli to compile the CLI Typescript, including types and maps in the output
  • npm run build-lib to build and minify the UI library
  • npm run build to run both of the above scripts sequentially

Dev Todo

  • Page > narrow screen mode

  • Grouping results

    • apply a function specified in the client settings to group results
    • results are grouped visually to tidy up the result list
    • How are results grouped if they differ in relevance?
  • Add option to reload the data each time the modal is opened, or after a timeout

    • for more dynamic data loaded from an API instead of a static JSON file
  • Correctly build types for UI component as Vite doesn't when in library mode