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

search-parse

v1.0.1

Published

Parses search query syntax into AST tree

Downloads

4

Readme

NPM | Documentation

This is a package for JS applications that parses search queries with the common search operator features such as logical or/and, grouping, phrases (in quotes), etc.

This package only implements a very simple filter utility function that takes a list of items or objects with a map to string function, and returns elements matching the query.

For more advanced use-cases, you can easily implement your own filter function.
See utils.ts#filter for an example.



Example results

Input: orange OR "golden apple"

Explanation: the word "orange" or the exact phrase "golden apple"

Output:

const result = [
  {
    type: 'operator',
    value: 'or',
    left: {
      type: 'word',
      value: 'orange',
    },
    right: {
      type: 'phrase',
      value: 'golden apple',
      quote: '"',
    },
  },
]

Input: (mango banana lemon) OR apple -pineapple

Explanation: Either one of the words: "mango", "banana", or "lemon"; OR the word "apple"; exclude all results containing "pineapple"

Output:

const result = [
  {
    type: 'operator',
    value: 'or',
    left: {
      type: 'group',
      children: [
        {
          type: 'word',
          value: 'mango',
        },
        {
          type: 'word',
          value: 'banana',
        },
        {
          type: 'word',
          value: 'lemon',
        },
      ],
    },
    right: {
      type: 'word',
      value: 'apple',
    },
  },
  {
    type: 'exclude',
    value: 'pineapple',
  },
]

How to use

Install the package using pnpm, npm or yarn:

pnpm add search-parse
npm install search-parse
yarn add search-parse

Simply pass a string to the parse function to get the results.

import { parse } from 'search-parse'
const results = parse('(mango banana lemon) OR apple -pineapple')

Supported operators

This is the comprehensive list of operators and their object results:

Implemented

  • Word: example

    Any single word. Only alpha-numeric characters, dashes and underscores are considered a word. The rest is considered whitespace, which is ignored by the parser, but will cause the surrounding tokens to be broken apart.

    Object:

    {
      type: 'word', // constant
      value: 'example' // this is the actual word in the string
    }
  • Phrase: "an example" or 'an example'

    A phrase can contain one or more characters. These characters are used as is and not validated as words, so they can include all sorts of special characters.

    A phrase can start with either a single or double quote, and must terminate using the same quote. The other type of quote than the one starting this sequence is ignored and considered part of the phrase itself when it inside it.

    Object:

    {
      type: 'phrase', // constant
      value: 'an example', // the phrase contained in the quote
      quote: '"' // the quote used to start and end this sequence
    }
  • Group: (one two three)

    A group can consist of one or more words or other types of values such as phrases or exclusions. A group logically puts its contents together, usually this is meant as an implicit OR operation but you can implement it as you require.

    Object:

    {
      type: 'group', // constant
      children: [
        // all types of children nodes such as word, phrase, etc
        {
          type: 'word',
          value: 'one'
        },
        {
          type: 'word',
          value: 'two'
        },
        {
          type: 'word',
          value: 'three'
        }
      ]
    }
  • Logical operators OR and AND: a OR b or a | b, and a AND b or a & b

    Logical operators group their immediate left and immediate right in a logical operation.

    Object:

    {
      type: 'operator',
      value: 'or', // or: 'and'
      left: { // whatever is on the left of the operator - word, phrase, etc
        type: 'word',
        value: 'a'
      },
      right: { // whatever is on the right of the operator - word, phrase, etc
        type: 'word',
        value: 'b'
      }
    }

To Do

  • Exclusion: -example

    An exclusion is an indication to not include results using the given word, phrase or group.

    Object:

    {
      type: 'exclusion',
      value: { // all types of children nodes such as word, phrase, etc
        type: 'word',
        value: 'example'
      }
    }
  • Domain: example-domain:example-token

    A domain prefix signals the following token to only refer to the prefixing domain. For example, a user could search name:apple to only search the word apple within the name property of the object being searched on.

    Object:

    {
      type: 'domain',
      domain: 'example-domain',
      value: { // all types of children nodes such as word, phrase, etc
        type: 'word',
        value: 'example-token',
      }
    }
  • User: @example-user

    A user query can signal only searching content from a specific user.

    Object:

    {
      type: 'user',
      value: 'example-user',
    }