Code Monkey home page Code Monkey logo

odata-filter-builder's Introduction

ODataFilterBuilder is util to build $filter part for OData URL query options

npm version Build Status Coverage Status

Documentation

Installation

The fastest way to get started is to serve JavaScript from the unpkg:

<!-- NOTE: See https://unpkg.com how to use specific vesion. -->
<!-- NOTE: not minified version - https://unpkg.com/[email protected]/dist/odata-filter-builder.js -->
<script src="https://unpkg.com/[email protected]/dist/odata-filter-builder.min.js"></script>
// now you can find the library on window.ODataFilterBuilder
var f = ODataFilterBuilder;

And it's just as easy with npm:

$ npm install --save odata-filter-builder
// using ES6 modules
import f from 'odata-filter-builder';
// using CommonJS modules
var f = require('odata-filter-builder').ODataFilterBuilder;

Also you can try it in your browser

How to use

f([condition='and'])

Constructor for ODataFilterBuilder class. Create new instance of filter builder. Can be used without new operator.

parameter type description
[condition='and'] string optional: base condition ('and'/'or').

Examples

// can be used without "new" operator
// default base condition is 'and'
f()
 .eq('TypeId', '1')
 .contains(x => x.toLower('Name'), 'a')
 .toString();
// (TypeId eq '1') and (contains(tolower(Name), 'a'))
// there are different constructors for 'and' as base condition
// f(), f('and') and f.and()
f('and')
 .eq('Type/Id', 3)
 .eq(x => x.concat(y => y.concat('City',', '), 'Country', false), 'Berlin, Germany')
 .toString();
// (Type/Id eq 3) and (concat(concat(City, ', '), Country) eq 'Berlin, Germany')

f.or()

ODataFilterBuilder constructor alias. Same as f('or'). Creates a new OData filter builder with 'or' as base condition.

Examples

// 'or' condition as base condition
f('or')
  .eq(x => x.toLower('Name'), 'foo')
  .eq(x => x.toLower('Name'), 'bar')
  .toString();
// (tolower(Name) eq 'foo') or (tolower(Name) eq 'bar')
// combination of 'or' and 'and'
f('or')
  .contains(x => x.toLower('Name'), 'google')
  .contains(x => x.toLower('Name'), 'yandex')
  .and(x => x.eq('Type/Name', 'Search Engine'))
  .toString();
// ((contains(tolower(Name), 'google')) or (contains(tolower(Name), 'yandex'))) and (Type/Name eq 'Search Engine')

More examples

You can find more examples in test/ODataFilterBuilder_spec.js.

To use with AngularJS see http://stackoverflow.com/a/36128276/223750.

Build your copy

Clone repository and use npm scripts

Tests

$ npm test
$ npm run test:watch
$ npm run test:cov
$ npm run lint

Build

$ npm run build

JSDoc

$ npm run jsdoc

odata-filter-builder's People

Contributors

bodia-uz avatar npmcdn-to-unpkg-bot avatar renovate[bot] avatar tangallan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

odata-filter-builder's Issues

Empty values (null, undefined) in 'contains' method

I use odata-builder for building large filter from table columns.
Each value can be empty string or undefined value and i want to skip 'contains' operator if value === undefined.

For example:

const filter = {
  name: 'MyName',
  age: undefined  // init value
};

f.build('and').contains('name', filter.name).contains('age', filter.age).toString(); 
// (contains(name, 'MyName')) and (contains(age));

I need to skip contains(age) operator because age === undefined, how to solve this problem?

Support ES5

The library is distributed with ES6 targeted, but to support IE11, would it be possible to target ES5?

Percent encoding of filter values

I just wanted to confirm that the filter builder does not do any sort of encoding on the output string. I'm not seeing any when looking through the source, but just want to verify.

According to OData:

http://docs.oasis-open.org/odata/odata/v4.0/errata03/os/complete/part2-url-conventions/odata-v4.0-errata03-os-part2-url-conventions-complete.html#_Toc453752335

Note also that the rules in [OData-ABNF] assume that URIs and URI parts have been percent-encoding normalized as described in section 6.2.2.2 of [RFC3986] before applying the grammar to them, i.e. all characters in the unreserved set (see rule unreserved in [OData-ABNF]) are plain literals and not percent-encoded.

Question is, should we add an option to perform the encoding or not?

Thanks!

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • Replace dependency rollup-plugin-terser with @rollup/plugin-terser 0.1.0
  • Update dependency eslint to v9
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

npm
package.json
  • @babel/cli 7.22.10
  • @babel/core 7.22.10
  • @babel/preset-env 7.22.10
  • @babel/register 7.22.5
  • babel-core 7.0.0-bridge.0
  • @babel/eslint-parser 7.11.0
  • babel-jest 29.6.3
  • coveralls 3.1.1
  • eslint 8.47.0
  • jest 29.6.3
  • jsdoc 3.6.10
  • rollup 3.28.1
  • @rollup/plugin-babel 6.0.3
  • rollup-plugin-terser 7.0.2
  • tsd 0.13.1
  • typescript 5.1.6
travis
.travis.yml
  • node 12

  • Check this box to trigger a request for Renovate to run again on this repository

ODataV4 Datetime filters not supported

filter.gt('eventDateTime', someDate, true) doesn't produce a filter in the correct OData format. This applies to all filters, not just filter.gt.

ODataV3-V4 datetime filters should be in the following format
(eventDateTime gt 2016-11-16T00:00:00Z) - ISO 8601 and no quotes

SubstringOf

Please, can you implement a legacy functional for covering such command as substringof, etc.? Unfortunately, it's not always possible to use the latest oData on backend part. Thank you!

Support for ES2015 module exports

Providing only default exports from a module doesn't play well with other module systems. Would it be possible to also export the ODataFilterBuilder as a named export so one might import like:

import { ODataFilterBuilder } from 'odata-filter-builder';

I currently use webpack with a module system of es2015 and target es5. In order to make TypeScript play well with the module, I had to change the export in index.d.ts to:

declare var ODataFilterBuilder: ODataFilterBuilderStatic;
export = ODataFilterBuilder;

and import like:

import * as ODataFilterBuilder from 'odata-filter-builder';

If I don't do that, TypeScript complains with:

Cannot invoke an expression whose type lacks a call signature...

... when trying to use the callable for the ODataFilterBuilder interface:

ODataFilterBuilder('and').eq('foo', 'bar');

Other than that, great library!

Convert wrap in parenthesis (grouping) to an operator?

I've created an example at https://stackblitz.com/edit/odata-parser-s38vfw

Notice how each of the operators are being wrapped in parenthesis. What I was expecting is a query that looks like:

foo eq 'bar' and quux eq 'baz' and name ne 'bax'

Parenthesis are a "grouping operator" according to the OData specification. Should they be treated as such in the filter builder?

For example, update the filter builder library to specify where they want to group. Another reason to allow grouping as an operator is some implementations of the OData specification don't support the grouping operator.

Right now the filter builder is producing OData strings implying that all implementations support the grouping operator.

Thoughts?

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.