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 🙏

© 2026 – Pkg Stats / Ryan Hefner

crnk-filtering

v4.0.0

Published

Zero-dependency TypeScript library for generating CRNK / JSON:API filter, sort & pagination query strings

Readme

crnk-filtering

Zero-dependency TypeScript library for generating CRNK / JSON:API filter, sort & pagination query strings.

Works with any HTTP client — Angular, fetch, Axios, etc.

CI npm version npm downloads code style: prettier

Features

Installation

npm install crnk-filtering

Quick start

import {
  BasicFilter,
  FilterSpec,
  FilterOperator,
  SortDirection,
} from 'crnk-filtering';

const query = new BasicFilter({
  filterSpecs: FilterSpec.of(
    ['user.name', 'Dino', FilterOperator.Like],
    ['user.age', 25, FilterOperator.GreaterOrEquals],
  ),
  relatedResources: 'client',
})
  .sortBy('user.name', SortDirection.ASC)
  .paginate(0, 20)
  .toString();

// include=client&filter[user.name][LIKE]=Dino%&filter[user.age][GE]=25&sort=user.name&page[limit]=20&page[offset]=0

Using with any HTTP client

The library returns plain objects and strings — no framework lock-in:

// Angular HttpClient
this.http.get('/api/users', { params: filter.getParams() });

// fetch
fetch(`/api/users?${filter.toString()}`);

// Axios
axios.get('/api/users', { params: filter.getParams() });

API reference

| Export | Description | |-|-| | FilterSpec | A single filter (path + value + operator) | | FilterSpec.of() | Static factory — creates FilterSpec[] from tuples | | BasicFilter | Builds flat CRNK filter query params | | NestedFilter | Builds JSON-based nested filter query params | | FilterOperator | Enum: EQ, NEQ, LIKE, LT, LE, GT, GE | | NestingOperator | Enum: AND, OR, NOT | | SortSpec | Sorting specification (path + direction) | | SortDirection | Enum: ASC, DESC | | PaginationSpec | Offset/limit pagination | | toQueryString() | Converts Record<string, string> to a query string | | PageEvent | Interface: { pageIndex, pageSize, length } | | FilterSpecDef | Tuple type: [path, value, operator?, nullable?] |

FilterSpec

A filter is represented by a FilterSpec. It holds the path to the attribute, the filter value, and the operator.

// Individual instance
new FilterSpec('user.name', 'Dino', FilterOperator.Like);

// Nullable — allows null as a valid value
new FilterSpec('user.deletedAt', null, FilterOperator.Equals, true);

FilterSpec.of() — batch creation (recommended)

Create multiple filters in one call using concise tuples:

FilterSpec.of(
  ['user.id', 12],                                  // defaults to EQ
  ['user.name', 'Dino', FilterOperator.Like],       // explicit operator
  ['user.deletedAt', null, FilterOperator.Equals, true], // nullable
)
// Returns FilterSpec[]

You can also pass tuples directly to filterSpecs:

new BasicFilter({
  filterSpecs: [
    ['user.id', 12],
    ['user.name', 'Dino', FilterOperator.Like],
  ],
})

Basic filtering

const result = new BasicFilter({
  filterSpecs: FilterSpec.of(
    ['user.id', 12],
    ['user.name', 'Dino', FilterOperator.Like],
    ['user.age', 25, FilterOperator.GreaterOrEquals],
  ),
}).toString();

// filter[user.id][EQ]=12&filter[user.name][LIKE]=Dino%&filter[user.age][GE]=25

Nested filtering

const result = new NestedFilter({
  filterSpecs: FilterSpec.of(
    ['user.id', 12],
    ['user.name', 'Dino', FilterOperator.Like],
  ),
  nestingCondition: NestingOperator.And,
}).toString();

// filter={"AND": [{"user": {"EQ": {"id": "12"}}}, {"user": {"LIKE": {"name": "Dino%"}}}]}

Nesting filters inside filters

const innerFilter = new NestedFilter({
  filterSpecs: innerSpecs,
  nestingCondition: NestingOperator.Or,
});

const result = new NestedFilter({
  filterSpecs: outerSpecs,
  nestingCondition: NestingOperator.And,
  innerNestedFilter: innerFilter.buildFilterString(), // accepts string or string[]
}).toString();

Sorting

All filter classes support fluent .sortBy() — pass inline args or SortSpec instances:

// Inline (recommended)
new BasicFilter({ filterSpecs })
  .sortBy('user.name', SortDirection.ASC)
  .toString();

// Multiple sort fields via SortSpec
new BasicFilter({ filterSpecs })
  .sortBy([
    new SortSpec('user.name', SortDirection.ASC),
    new SortSpec('user.id', SortDirection.DESC),
  ])
  .toString();

Inclusion of Related Resources

new BasicFilter({
  filterSpecs,
  relatedResources: ['client', 'car'],
}).toString();

// include=client,car&filter[...]...

Sparse fieldsets

new NestedFilter({
  filterSpecs,
  sparseFieldsets: ['user.id', 'user.name'],
}).toString();

// ...&fields=user.id,user.name

Pagination

PaginationSpec is framework-agnostic — no Angular Material dependency.

// Inline (recommended)
new BasicFilter({ filterSpecs })
  .paginate(0, 20)
  .toString();
// ...&page[limit]=20&page[offset]=0

// Via PaginationSpec instance
new BasicFilter({ filterSpecs })
  .paginate(new PaginationSpec(0, 20))
  .toString();

// Standalone — merge with existing params
const pagination = new PaginationSpec(0, 20);
const allParams = pagination.withParams(filter.getParams());
const queryString = toQueryString(allParams);

Integrating with a paginator component

const pagination = new PaginationSpec(); // default: page 0, size 10

// On page change event from any paginator UI:
function onPageChange(event: { pageIndex: number; pageSize: number; length: number }) {
  pagination.setPagination(event);
  // Re-fetch data
}

// Reset
pagination.resetPaginator();

Migration from v3 → v4

| v3 (Angular-based) | v4 (framework-agnostic) | |-|-| | filter.getHttpParams() | filter.getParams() returns Record<string, string> | | decodeURI(filter.getHttpParams().toString()) | filter.toString() | | paginationSpec.setHttpParams(filter.getHttpParams()) | filter.paginate(paginationSpec).toString() or paginationSpec.withParams(filter.getParams()) | | import { PageEvent } from '@angular/material/paginator' | import { PageEvent } from 'crnk-filtering' | | basicFilter.sortBy(...) (void) | basicFilter.sortBy(...) (returns this — chainable) | | @angular/common/http peer dependency | No dependencies |

License

Apache License 2.0

Copyright (c) 2021-2026 Dino Klicek