GithubHelp home page GithubHelp logo

odata-filter-to-ast's Introduction

odata-filter-to-ast

Simple OData query parser with zero dependencies.

npm version MIT license GitHub code size in bytes dependencies npm

Usage

$ npm install odata-filter-to-ast

Usage in code:

import {parseFilter, parseOrderBy} from 'odata-filter-to-ast';

parseFilter(`Name gt "Milk" or Price lt -2.55 or Size ne 3`);
parseOrderBy(`age,sum(height,width) desc`);

Result:

{
	type: 'orExpr',
	left: {
		type: 'gtExpr',
		left: {
			type: 'memberExpr',
			value: 'Name',
		},
		right: {
			type: 'primitive',
			value: 'Milk'
		},
	},
	right: {
		type: 'orExpr',
		left: {
			type: 'ltExpr',
			left: {
				type: 'memberExpr',
				value: 'Price',
			},
			right: {
				type: 'primitive',
				value: -2.55
			}
		},
		right: {
			type: 'neExpr',
			left: {
				type: 'memberExpr',
				value: 'Size',
			},
			right: {
				type: 'primitive',
				value: 3
			}
		}
	}
}
[
	{
		type: 'orderByItem',
		expr: {
			type: 'memberExpr',
			value: 'age',
		},
		dir: 'asc',
	},
	{
		type: 'orderByItem',
		expr: {
			type: 'functionExpr',
			name: 'sum',
			arguments: [
				{
					type: 'memberExpr',
					value: 'height',
				},
				{
					type: 'memberExpr',
					value: 'width',
				},
			],
		},
		dir: 'desc',
	},
]

Supported constructs

The following construct from OData specification are supported:

  • JS primitives -42, 3.14, 6.022e23, "string", true, false, null
  • field identifiers iDenT_iFi3r
  • heterogeneous arrays ["a","r","r","a","y",7,false,null]
  • primitive relations eq, ne, gt, gt, lt, le
  • array relation in
  • boolean conjunctions and, or
  • operator priority grouping ( ... )
  • function calls includes(Name,"Joe")
  • sort directions asc, desc

Related

odata-filter-to-ast's People

Contributors

bytenik avatar juliamcneill avatar petrzjunior avatar

Stargazers

 avatar  avatar

Watchers

 avatar

odata-filter-to-ast's Issues

String quotation

The OData specification mandates string literal quotation using ', but this repo uses " for literal quotation. OData uses " for identifier quotation.

I'd introduce an issue or PR to make this library OData conformant, but it would break all the dependent projects.

Maybe after increasing the major version? Maybe just adding a warning to the readme? Maybe supporting both for string literals, and not supporting quoted identifiers?

I suspect that there are more inconsistencies with the OData specification than that, especially the number parsing can be non-conformant.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.