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

asearch.ts

v1.0.3

Published

TypeScript implementation of node-asearch

Downloads

7

Readme

Asearch.ts

Test and Publish

日本語で読む

This library is a TypeScript implementation based on asearch-ruby and node-asearch.

However, there are some implementation differences due to the author's intentions.

Install

npm install asearch.ts

Usage

import { Asearch, MatchMode } from "asearch.ts";

// Compare "hello" and "helo"
const a = new Asearch("hello");
console.log(a.match("helo")); // false. By default, ambiguity is 0
console.log(a.match("helo", 1)); // true. Allowed with ambiguity of 1
console.log(a.match("helo", 2)); // true. Also allowed with ambiguity of 2

// Check if "cat" is contained in the text
const b = new Asearch("cat", MatchMode.Include);
console.log(b.match("I am a cat")); // true

new Asearch(pattern: string, mode?: MatchMode): Asearch

  • Creates an instance.

  • pattern

    • Pattern string
  • mode

    • How the comparison is done
    • The default is MatchMode.Exact for an exact match
    • Setting it to MatchMode.Include changes it to a partial match (it's enough if the pattern is contained in the query)
    • Setting it to MatchMode.WildcardSpace treats spaces in the pattern string as wildcards

asearch.match(query: string, ambig?: number): boolean

  • Determines if the query string matches. Ambiguity can be specified using Levenshtein distance.

  • In other words, it specifies how many characters can be incorrect.

  • Returns true if it matches within the given ambiguity, otherwise false.

  • query

    • Query string for comparison
  • ambig

    • Degree of ambiguity
    • Must be an integer of 0 or higher. The default is 0

Differences from the Original

Disabling Wildcard Characters by Default

In the original library, spaces ( ) in the pattern string are treated as "wildcards."

Space characters (0x20) in the search pattern become wildcards. (Matches any sequence of characters, similar to ".*" in regular expressions.) http://www.pitecan.com/Index/asearch.html

This feature is turned off by default, so if you want to use it, specify MatchMode.WildcardSpace.

If you want partial matches, use MatchMode.Include.

Support for Emojis 👌✨

The library supports emojis by splitting the string using Array.from().

Behavior When Extra Characters are Inserted at the Beginning of the Pattern String

In node-asearch, if extra characters are inserted at the beginning of the pattern string (in other words, if the beginning of the query string is deleted), the ambiguity is increased by +1 than usual. This library treats the beginning in the same way.

It's faster to look at the sample code.

const Asearch = require("asearch"); // node-asearch

const a = new Asearch("asearch");
console.log(1, a.match("search", 1)); // This becomes false
console.log(2, a.match("search", 2)); // This is true

sandbox

This is because the state transition machine compares the first character before transitioning to a node that can transition from the initial state with an empty string (ε).

In this library, we decided to perform an empty string transition at the initialization of the state transition machine.

import { Asearch } from "asearch.ts"; // asearch.ts

const a = new Asearch("asearch");
console.log(1, a.match("search", 1)); // true
console.log(2, a.match("search", 2)); // true

Using named export

It's easier to use in TypeScript, so we've made it a named export.