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

simple-search-query

v0.0.4

Published

Simple search query matcher

Downloads

7

Readme

simple-search-query

Simple logic search query matcher. Execute query like:

  • XML and HTML Looking for both keywords XML and HTML.
  • English and (not GB) Looking for English and no GB present.

Why

This is a simple practice to know how to use PEG.js. And my target is to build a logical query can help people filter data. Using common syntax people can easily understand or already known.

Usage

import query from 'simple-search-query';

const q = query('XML or HTML or "Foo Bar"');

q('XML/HTML'); // true
q('XSLT'); // false
q('XHTML'); // false, default matcher use word boundary

Reference

function query

Default export from simple-search-query.

syntax

query(query_string)
query(query_string, options)

Return a q function can be used to search by the given query_string.

parameters

  • query_string: The logic query string from the user or other input.
  • options: Optional default options object.

function q

syntax

q(text);
q(text, options);

parameters

  • text: Text to search.
  • options: Optional options object.

Options Object

  • i: Ignore case, default true.

  • matcher: Matcher function, default matcher function use regexp and word boundary like:

    () => !!(new RegExp(`\\b${query}\\b`)).test(entry)

    There are two more built-in matcher functions:

    has: Not care about word boundary, use indexOf. Since CJK characters do not work well with word boundary. has might be a good choice to search them.
    eqeq: Full match, use ===.

    If you want to assign the default matcher in options, use default.

    It's also possible to send custom matcher function, and the function will receive following arguments in order:

    1. entry: Text to search.

    2. query: The item from parse query string. For example: XML or HTML or "Foo Bar". The matcher function will be called three times with different query argument. One is XML, second is HTML. The last one is Foo Bar.

    3. options: Options object.

    Custom matcher function needs to handle ignore case setting in options object itself. The custom matcher function should return a boolean value, then q function will handle the rest (logic part of the query string).

Query String Syntax

Supports logic:

  • and, & infix
  • or, | infix
  • not, ! prefix

Supports subquery, use () to wrap subexpression.

Supports quoted string, both double and single quoted string.