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 🙏

© 2025 – Pkg Stats / Ryan Hefner

astrolex

v1.0.0

Published

Fuzzy keyword extraction from a controlled vocabulary

Readme

astrolex

A small, framework-agnostic Node.js package in TypeScript that provides fuzzy keyword extraction from user input, using a predefined dictionary of known terms.

Features

  • Multi-strategy matching: Exact match, startsWith, contains, and fuzzy Levenshtein distance matching
  • Normalization: Handles accents/diacritics, case-insensitive matching, and common separators
  • Multi-language support: Works with French and English keywords (and can be extended)
  • Factory pattern: Create an engine once with a dictionary, then parse multiple inputs efficiently
  • Type-safe: Full TypeScript support with strict types

Installation

npm install astrolex

Usage

Basic Example

import { createSearchEngine } from "astrolex";

const engine = createSearchEngine(["tomate", "salade", "pomme", "tomato", "apple"]);

const result = engine.parse("saladee de pome");
console.log(result.bestKeywords); // ["salade", "pomme"]

// Access detailed match information
result.matches.forEach((match) => {
  if (match.matched) {
    console.log(`${match.token.raw} -> ${match.keyword} (score: ${match.score}, strategy: ${match.strategy})`);
  }
});

With Options

const engine = createSearchEngine(
  ["tomate", "salade", "pomme"],
  {
    maxDistance: 2,           // Maximum Levenshtein distance (default: 2)
    minSimilarityScore: 0.5,  // Minimum similarity score 0..1 (default: 0.5)
    languageHint: "fr"        // Optional hint (currently not used, reserved for future)
  }
);

API

createSearchEngine(keywords: string[], options?: SearchEngineOptions): SearchEngine

Creates a search engine instance with a predefined dictionary of keywords.

Parameters:

  • keywords: Array of known keywords (French or English)
  • options: Optional configuration (see SearchEngineOptions below)

Returns: A SearchEngine instance

SearchEngine.parse(input: string): ParseOutput

Parses user input and returns structured match results.

Returns:

{
  input: string;              // Original input
  tokens: ParsedToken[];      // Tokenized input
  matches: MatchResult[];     // Match results for each token
  bestKeywords: string[];     // Unique matched keywords, sorted by score (desc)
}

SearchEngine.getKeywords(): string[]

Returns the list of original keywords provided at construction.

Matching Strategies

The engine tries multiple strategies in order for each token:

  1. Exact match: Normalized token exactly equals a keyword
  2. StartsWith: Keyword starts with the token (useful for abbreviations like "tom" -> "tomate")
  3. Contains: Keyword contains the token
  4. Fuzzy/Levenshtein: Uses edit distance to catch typos (e.g., "saladee" -> "salade")

The best matching keyword (highest score) is selected for each token.

Integration in a Dockerized Backend

This package is designed to be used as a dependency in your backend application. It runs inside your backend container, not in its own container.

Example Dockerfile

FROM node:20-alpine
WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies (including this package)
RUN npm install

# Copy your application code
COPY . .

# Build your application
RUN npm run build

# Run your application
CMD ["node", "dist/main.js"]

Usage in NestJS

// app.module.ts
import { Module } from '@nestjs/common';
import { createSearchEngine } from 'astrolex';

@Module({
  providers: [
    {
      provide: 'KEYWORD_ENGINE',
      useFactory: () => createSearchEngine(['tomate', 'salade', 'pomme']),
    },
  ],
})
export class AppModule {}
// some.service.ts
import { Inject, Injectable } from '@nestjs/common';
import { SearchEngine } from 'astrolex';

@Injectable()
export class SomeService {
  constructor(@Inject('KEYWORD_ENGINE') private engine: SearchEngine) {}

  searchUserInput(input: string) {
    return this.engine.parse(input);
  }
}

Development

# Install dependencies
npm install

# Build
npm run build

# Run tests
npm test

# Watch mode for tests
npm run test:watch

License

MIT