Skip to content

bodia-uz/odata-filter-builder

Repository files navigation

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