Skip to content

Commit

Permalink
feat: support filter with type
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Jun 20, 2021
1 parent b3fb06c commit 81d6cc7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,19 @@ param().top(1).filter(filter({ A: 1 }))
// => $top=1&$filter=A eq 1
```

### filter with type

```ts
import { filter, Edm } from "@odata/parser";

expect(filter({ A: 1 }).build())
.toBe("A eq 1")
expect(filter({ A: literalValues.String(1) }).build())
.toBe("A eq '1'")
expect(filter({ A: literalValues.Guid("253f842d-d739-41b8-ac8c-139ac7a9dd14") }).build())
.toBe("A eq 253f842d-d739-41b8-ac8c-139ac7a9dd14")

```


## [CHANGELOG](./CHANGELOG.md)
41 changes: 40 additions & 1 deletion src/builder/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Edm } from '@odata/metadata';
import { ODataFilter } from './filter';
import { ODataParam } from './param';

export { Edm } from '@odata/metadata';
export * from './batch';
export * from './filter';
export * from './param';
Expand All @@ -14,3 +14,42 @@ export function param() {
export function filter(obj?: Record<string, any>) {
return ODataFilter.New(obj);
}

/**
* edm primitive literal value creators
*/
export const literalValues = {
Binary: (value: any) => Edm.Binary.createValue(value),
Boolean: (value: any) => Edm.Boolean.createValue(value),
Byte: (value: any) => Edm.Byte.createValue(value),
Date: (value: any) => Edm.Date.createValue(value),
DateTimeOffset: (value: any) => Edm.DateTimeOffset.createValue(value),
Decimal: (value: any) => Edm.Decimal.createValue(value),
Double: (value: any) => Edm.Double.createValue(value),
Duration: (value: any) => Edm.Duration.createValue(value),
Guid: (value: any) => Edm.Guid.createValue(value),
Int16: (value: any) => Edm.Int16.createValue(value),
Int32: (value: any) => Edm.Int32.createValue(value),
Int64: (value: any) => Edm.Int64.createValue(value),
SByte: (value: any) => Edm.SByte.createValue(value),
Single: (value: any) => Edm.Single.createValue(value),
Stream: (value: any) => Edm.Stream.createValue(value),
String: (value: any) => Edm.String.createValue(value),
TimeOfDay: (value: any) => Edm.TimeOfDay.createValue(value),
Geography: (value: any) => Edm.Geography.createValue(value),
GeographyPoint: (value: any) => Edm.GeographyPoint.createValue(value),
GeographyLineString: (value: any) => Edm.GeographyLineString.createValue(value),
GeographyPolygon: (value: any) => Edm.GeographyPolygon.createValue(value),
GeographyMultiPoint: (value: any) => Edm.GeographyMultiPoint.createValue(value),
GeographyMultiLineString: (value: any) => Edm.GeographyMultiLineString.createValue(value),
GeographyMultiPolygon: (value: any) => Edm.GeographyMultiPolygon.createValue(value),
GeographyCollection: (value: any) => Edm.GeographyCollection.createValue(value),
Geometry: (value: any) => Edm.Geometry.createValue(value),
GeometryPoint: (value: any) => Edm.GeometryPoint.createValue(value),
GeometryLineString: (value: any) => Edm.GeometryLineString.createValue(value),
GeometryPolygon: (value: any) => Edm.GeometryPolygon.createValue(value),
GeometryMultiPoint: (value: any) => Edm.GeometryMultiPoint.createValue(value),
GeometryMultiLineString: (value: any) => Edm.GeometryMultiLineString.createValue(value),
GeometryMultiPolygon: (value: any) => Edm.GeometryMultiPolygon.createValue(value),
GeometryCollection: (value: any) => Edm.GeometryCollection.createValue(value),
}
18 changes: 12 additions & 6 deletions test/builder/filter.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Edm, filter, ODataFilter } from '../../src';
import { filter, literalValues, ODataFilter } from '../../src';

describe('OData Query Builder - Filter Test Suite', () => {

it('should support filter by value/name', () => {

expect(ODataFilter.New().field('A').eq('a').toString()).toBe("A eq 'a'");
expect(ODataFilter.New().field('A').eq(Edm.String.createValue("a")).toString()).toBe("A eq 'a'");
expect(ODataFilter.New().field('A').eq(literalValues.String("a")).toString()).toBe("A eq 'a'");
expect(ODataFilter.New().field('A').eq(1).toString()).toBe('A eq 1');

});
Expand All @@ -18,8 +18,8 @@ describe('OData Query Builder - Filter Test Suite', () => {
expect(ODataFilter.New<Type>({ A: 1, B: '2' }).toString()).toBe("A eq 1 and B eq '2'");

expect(ODataFilter.New<any>({
A: Edm.Int16.createValue(12),
B: Edm.String.createValue('12')
A: literalValues.Int16(12),
B: literalValues.String('12')
}).toString()).toBe("A eq 12 and B eq '12'");
});

Expand All @@ -31,14 +31,20 @@ describe('OData Query Builder - Filter Test Suite', () => {
expect(
filter()
.field("A")
.eq(Edm.Guid.createValue("253f842d-d739-41b8-ac8c-139ac7a9dd14"))
.eq(literalValues.Guid("253f842d-d739-41b8-ac8c-139ac7a9dd14"))
.build()
).toBe("A eq 253f842d-d739-41b8-ac8c-139ac7a9dd14")

expect(
filter({ A: Edm.Guid.createValue("253f842d-d739-41b8-ac8c-139ac7a9dd14") })
filter({ A: literalValues.Guid("253f842d-d739-41b8-ac8c-139ac7a9dd14") })
.build()
).toBe("A eq 253f842d-d739-41b8-ac8c-139ac7a9dd14")
});

it('should support filter with type', () => {
expect(filter({ A: 1 }).build()).toBe("A eq 1")
expect(filter({ A: literalValues.String(1) }).build()).toBe("A eq '1'")
expect(filter({ A: literalValues.Guid("253f842d-d739-41b8-ac8c-139ac7a9dd14") }).build()).toBe("A eq 253f842d-d739-41b8-ac8c-139ac7a9dd14")
});

});

0 comments on commit 81d6cc7

Please sign in to comment.