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 🙏

© 2026 – Pkg Stats / Ryan Hefner

trgm-js

v0.0.4

Published

Set of utilities for trigram comparison for JavaScript and TypeScript projects.

Readme

trgm-js

Provides utilities for working with trigrams in JavaScript and TypeScript. Comes with a firs-class TypeScript support and a simple API for comparing, sorting and searching strings based on their trigram similarity. Inspired by the pg_trgm PostgreSQL extension.

Installation

npm install trgm-js

Usage

You can use this package to compare and sort texts based on their trigram similarity.

Comparing strings

To compare two strings based on their trigram similarity, use the similarity function:

import { similarity } from "trgm-js";

const sim = similarity("dog", "dawg");
console.log(sim); // 0.125

By default, trgm-js caps decimals places at 7. You can override this setting by passing a third argument to the similarity function:

import { similarity } from "trgm-js";

// Cap decimal places at 10
const sim = similarity("this is a longer text", "this is a longer string", 10);
console.log(sim); // 0.5714285714

The result will always be stripped of trailing zeros, this setting only affects the cases in which the result has more decimal places than the specified cap.

Sorting

You can sort an array based on a similarity with a search term using the toSorted method.

import { toSorted } from "trgm-js";

const items = ["cat", "dawg", "dog", "pog"];
const sortedItems = toSorted(items, "dog");
console.log(sortedItems); // ['dog', 'dawg', 'pog', 'cat']

toSorted creates a copy of the argument array and doesn't modify it. The default sort order is descending – the most similar words (i. e. those with the highest similarity scores) will appear first, less similar words later – but ordering can be changed to ascending by passing a third argument into the method.

import { toSorted } from "trgm-js";

const items = ["dog", "dawg"];
const sortedItems = toSorted(items, "dog", "asc");
console.log(sortedItems); // ["dawg", "dog"]

Determining similarity

The areSimilar method uses the similarity treshold to determine whether two texts are similar enough. This treshold is a value of 0.3 by default, but you can change it by passing a third argument into the method.

import { areSimilar } from "trgm-js";

const result = areSimilar("Rosa", "Roska"); // Similarity of ~0.4
console.log(result); // true

const result2 = areSimilar("Rosa", "Roseberry"); // Similarity of ~0.2
console.log(result2); //false