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

fuzzyjs

v5.0.1

Published

Simple fuzzy matching

Downloads

3,877

Readme

fuzzyjs

NPM NPM GitHub Workflow Status

fuzzyjs is a fuzzy search algorithm in javascript.

Usage

test

Tests a query against a source using fuzzy matching

import { test } from "fuzzyjs";

test("ssjs", "Set Syntax: JavaScript");
true;
function test(query: string, source: string, opts?: TestOptions): boolean;

type TestOptions = {
  caseSensitive?: boolean; // (default: false)
};

match

Matches a query against a source using fuzzy matching, returns information about the result

import { match } from 'fuzzyjs'

match('ssjav', 'Set Syntax: JavaScript')
{
  match: true,
  score: 22,
  ranges: [
    { start: 0, stop: 1 },
    { start: 4, stop: 5 },
    { start: 12, stop: 15 }
  ]
}
function match(query: string, source: string, opts?: MatchOptions): MatchResult;

type MatchOptions = {
  caseSensitive?: boolean;
  strategy?: ScoreStrategy; // (default: defaultStrategy, see below)
  withScore?: boolean; // (default: true)
};

type MatchResult = {
  match: boolean;
  score: number; // only if `withScore` is true, else undefined
  ranges: MatchRange[];
};

Utilities

surround

Surround parts of the string that matched with prefix and suffix

import { match, surround } from "fuzzyjs";

const result = match("ssjav", "Set Syntax: JavaScript");

surround("Set Syntax: JavaScript", {
  result,
  prefix: "<strong>",
  suffix: "</strong>",
});
("<strong>S</strong>et <strong>S</strong>yntax: <strong>Jav</strong>aScript");
function surround(source: string, options: SurroundOptions): string;

type SurroundOptions = {
  result: {
    ranges: MatchRange[];
  };
  prefix?: string; // (default: '')
  suffix?: string; // (default: '')
};

filter

Can be used as a Array.prototype.filter callback.

import { filter as fuzzy } from "fuzzyjs";

const sources = [
  "Set Syntax: JavaScript",
  "Set Syntax: CSS",
  "Set Syntax: HTML",
];

sources.filter(fuzzy("ssjs", { iterator: (item) => item }));
["Set Syntax: JavaScript"];

const sources = [
  { name: { foo: "Set Syntax: JavaScript" } },
  { name: { foo: "Set Syntax: CSS" } },
  { name: { foo: "Set Syntax: HTML" } },
];

sources.filter(fuzzy("ssjs", { iterator: (source) => source.name.foo }));
[{ name: { foo: "Set Syntax: JavaScript" } }];
function filter<TItem>(
  query: string,
  options: FilterOptions<TItem>
): (source: TItem) => boolean;

type FilterOptions<TItem> = {
  caseSensitive?: boolean;
  iterator: (source: TItem) => string;
};

sort

Can be used as a Array.prototype.sort callback. If you have a large array of objects, you might want to pass idAccessor as it creates a memoization table which reduces drastically how many times the fuzzy matching algorithm will be called.

import { sort as fuzzy } from "fuzzyjs";

const sources = [
  "Set Syntax: CSS",
  "Set Syntax: HTML",
  "Set Syntax: JavaScript",
];

sources.sort(fuzzy("ssjs", { iterator: (item) => item }));
[("Set Syntax: JavaScript", "Set Syntax: CSS", "Set Syntax: HTML")];

const sources = [
  { name: { id: 0, foo: "Set Syntax: CSS" } },
  { name: { id: 1, foo: "Set Syntax: HTML" } },
  { name: { id: 2, foo: "Set Syntax: JavaScript" } },
];

sources.sort(fuzzy("ssjs", { iterator: (source) => source.name.foo }));
[
  { name: { id: 2, foo: "Set Syntax: JavaScript" } },
  { name: { id: 0, foo: "Set Syntax: CSS" } },
  { name: { id: 1, foo: "Set Syntax: HTML" } },
];
function sort<TItem>(
  query: string,
  options?: SortOptions<TItem>
): (leftSource: TItem, rightSource: TItem) => 0 | 1 | -1;

type SortOptions<TItem> = {
  caseSensitive?: boolean;
  iterator: (item: TItem) => string;
};

Scoring function

A scoring function is a function that given two context, returns a number (either positive or negative) that will be added the the match score.

A leading character is a character that matters more than others. These are made of capitals and letters following -_ ./\.

function pushScore(
  previousContext: ScoreContext | undefined,
  context: ScoreContext
): number;

type ScoreContext = {
  currentScore: number; // the current match score
  character: string; // the current character
  match: boolean; // is the character matching the source string
  leading: boolean; // is the character leading
};

Link to default strategy: here.

License

fuzzyjs is licensed under MIT License.