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

@elmordo/query-filters

v1.1.1

Published

Main purpose of the `query-filter` is to provide functionality to generate URL query strings for filtering, sorting and pagination.

Downloads

6

Readme

query-filter

Main purpose of the query-filter is to provide functionality to generate URL query strings for filtering, sorting and pagination.

Filtering

Filters are defined by threee properties of interface Filter:

  • field - name of the field (e.g. "username")
  • operator - operator identifier (e.g. "like")
  • value - value for the operator (e.g. `"John Doe")

Value cannot be null or undefined because we do not know, if the null value is realy NULL or string with content "NULL".

There are two styles of filter formats supported:

  1. Left hand operator (by LeftHandedStyleFilter class) - username[like]=John Doe
  2. Right hand operator (by RightHandStyleFilter class) - username=like:John Doe

Sorting

Sorts are defined by object with two properties:

  • field - name of the field (e.g. username)
  • direction - optional sort direction (asc or desc, default is asc).

There are three types of sort formats supported:

  1. Function like format (by SortByFunction class) - asc(username), desc(username)
  2. Sign style format (by SortBySign) - __sort=+username, __sort=-username
  3. Property style format (by SortByProperty) - __sort=username.asc, __sort=username.desc

Pagination

Pagination is defined by object with two properties:

  • page - page index (e.g. 5)
  • perPage - number of records per one page (e.g. 20)

There is only one pagination style at this time:

  1. Pagination defined by two keys (by PaginationByTwoKeys) - __page=5&__perPage=20

Final query and query builder

All three parts described above are used in the FilterQuery. This object has three properties:

  • filters - list of filter definitions
  • sorts - list of sort definitions
  • pagination - pagination definition

When some property is missing (is null or undefined) this part of query string won't be built.

Final query string is built by object implementing the QueryBuilderInterface. There are two query builders:

  • AbstractQueryBuilder with abstract properties (slots) for partial builders
  • QueryBuilder with partial builders passed by constructor.

If some partial builder is not set (the null or the undefined value is passed), this part of query string won't be built.

Example


let query: QueryFilter = {
  filters: [
    {field: "username", operator: "like", value: "John Doe"},
    {field: "age", operator: "gt", value: 18},
  ],
  sorts: [
    {field: "registered_at"},
    {field: "nickname", direction: "desc"}
  ],
  pagination: {page: 0, perPage: 20}
}

let queryBuilder = new QueryBuilder(
  new RightHandStyleFilter(),
  new SortBySign(),
  new PaginationByTwoKeys()
);

let parts = queryBuilder.build(query);  // list of string key-value pairs
liet queryString = queryBuilder.buildString(query)  // all key-value pairs merged into query string

Customizing builders

All builders can be customized by implementing builder interfaces. Each partial builder interface have only one method with one argument (list of items to be built) and returning list of strings.

Final query builder has two methods. Each take one argument - an object implementing QueryFilter interface. But return value is different:

  • The build method returns list of strings (like partial builders)
  • The buildString returns one string containing list of strings returned by build method and joined by & character.