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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@dichioniccolo/odata-filter-parser

v0.0.10

Published

OData filter parser

Readme

OData Filter Parser

This library parses an odata complaint string and returns an object.

Installation

Installation with npm

npm i @dichioniccolo/odata-filter-parser

Installation with yarn

yarn add @dichioniccolo/odata-filter-parser

Usage

import { parse } from '@dichioniccolo/odata-filter-parser'

const filter = "contains(a/b,'c') and contains(d/e, 'f') and g eq 1 and h eq false";

const parsed = parse(filter);

/*
The result will be an object like this:
{
  a: { b: { contains: "c" } },
  d: { e: { contains: "f" } },
  g: { equals: 1 },
  h: { equals: false },
}
*/
console.log(parsed);

Custom parsers can also be used. They will have precedence over builtin ones. Every custom parser should extend from the Parser class provided by the package

import { parse, Parser, Operators } from '@dichioniccolo/odata-filter-parser'

class DateParser extends Parser {
  // This is an example regex. A good regex would also check for the date based on the month.
  static DATE_REGEX =
    /(.*) (eq) ((19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])T\d{2}:\d{2}:\d{2}.\d{3}Z)/;

  constructor(protected value: string) {
    super(value, "");
  }

  static isOfType(value: string): boolean {
    return !!value.match(DateParser.DATE_REGEX);
  }

  parse(previous: unknown): Record<string, any> {
    const match = this.value.match(DateParser.DATE_REGEX);

    if (!match) {
      return {};
    }

    const [, left, operator, right] = match;

    const methods = [Operators.GREATER_THAN_EQUAL, Operators.LESS_THAN_EQUAL];
    const values = [right, `${right.substring(0, 10)}T23:59:59.999Z`];

    return this.setDeepValue(previous, this.leftValue(left), methods, values);
  }

  setDeepValue(
    obj: any,
    [prop, ...path]: Array<string>,
    methods: string[],
    values: string[]
  ) {
    if (!path.length) {
      methods.forEach((method, index) => {
        obj[prop] = {
          ...obj[prop],
          [method]: values[index],
        };
      });
    } else {
      if (!(prop in obj)) {
        obj[prop] = {};
      }

      this.setDeepValue(obj[prop], path, methods, values);
    }

    return obj;
  }
}

const filter = "createdAt eq 2021-11-29T00:00:00.000Z and contains(a,'string')";

const parsed = parse(filter, { DateParser });

/*
The result will be an object like this:
{
  createdAt: {
    gte: '2021-11-29T00:00:00.000Z',
    lte: '2021-11-29T23:59:59.999Z',
  },
  a: {
    contains: 'string'
  }
}
*/
console.log(parsed);

Roadmap

[] Add all available operators

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.