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

twitter-search-query-parser

v1.0.0

Published

A parser for Twitter search queries

Downloads

457

Readme

twitter-search-query-parser

semantic-release Build Status

WIP parser for Twitter search queries.

Use

parse turns a search query into a data structure:

import {parse} from 'twitter-search-query-parser';

parse('@jack from:twitter') ===
  [ 'And',
    [ [ 'Including', [ 'Text', '@jack' ] ],
      [ 'Including', [ 'Pair', 'from', 'twitter' ] ] ] ]

stringify turns a data structure back into a query:

import {stringify} from 'twitter-search-query-parser';

stringify(
  [ 'And',
    [ [ 'Including', [ 'Text', '@jack' ] ],
      [ 'Including', [ 'Pair', 'from', 'twitter' ] ] ] ]
) === '@jack from:twitter'

simplify squashes the data structure by turning complex types into text. This makes it easier to build an interface for the query, and it stringifies just the same.

import {parse, simplify} from 'twitter-search-query-parser';

const parsed = parse('@jack OR @twitter -(@support OR @twitterdev)');

parsed ===
  [ 'And',
    [ [ 'Including',
        [ 'Or',
          [ [ 'Including', [ 'Text', '@jack' ] ],
            [ 'Including', [ 'Text', '@twitter' ] ] ] ] ],
      [ 'Excluding',
        [ 'Group',
          [ 'And',
            [ [ 'Including',
                [ 'Or',
                  [ [ 'Including', [ 'Text', '@support' ] ],
                    [ 'Including', [ 'Text', '@twitterdev' ] ] ] ] ] ] ] ] ] ] ]

const simplified = simplify(parsed, {
  disallow: ['Group', 'Or']
});

simplified ===
  [ 'And',
    [ [ 'Including', [ 'Text', '@jack OR @twitter' ] ],
      [ 'Excluding', [ 'Text', '(@support OR @twitterdev)' ] ] ] ]

Terms

The parsed data stucture follows the format: [Type, ...values]. The nature of the values depends on the Type.

Text

Represents a text value, including hashtags, mentions and cashtags. For example:

#x

Contains a single string value.

['Text', 'x']

Exactly

Represents an exact text match, in quotes.

"b c"

Contains one string value.

['Exactly', 'b c']

And

Represents a conjunction of other terms, and is the root of the query and grouped sub-queries. For example:

x #y

Contains an Array value of other terms.

['And', [ ... ]]

Or

Represents a disjunction of other terms. For example:

x OR #y

Contains an Array value of other terms.

['Or', [ ... ]]

Including

Represents the inclusion of the contained term. Every term is either included or excluded.

Contains a single value, another term.

['Including', ['Text', 'x']]

Excluding

Represents the exclusion of the contained term. Every term is either included or excluded.

Contains a single value, another term.

['Excluding', ['Text', 'x']]

Group

Represents a group of terms. For example:

(a b) OR (c d)

Contains a single value, another term.

['Group', [ ... ]]

Pair

Represents a search facet operator. For example:

filter:vine exclude:retweets

Contains two string values: they key and the value.

['Pair', 'filter', 'vine']

List

Represents a list facet operator.

list:NASA/astronauts-in-space-now

Contains two values: user name and list slug.

['List', 'NASA', 'astronauts-in-space-now']

Development

$ npm install
$ npm run gen
$ npm run try "some -search from:twitter @jack #tagged OR \"exactly this\""

Commit messages should follow the Angular commit message guidelines.

Release

This repository uses semantic-release. Changes will automatically be released to npm.