diff --git a/README.md b/README.md index 9f7e516..b5e2011 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/builder/index.ts b/src/builder/index.ts index caa4465..1c63318 100644 --- a/src/builder/index.ts +++ b/src/builder/index.ts @@ -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'; @@ -14,3 +14,42 @@ export function param() { export function filter(obj?: Record) { 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), +} \ No newline at end of file diff --git a/test/builder/filter.test.ts b/test/builder/filter.test.ts index 2ce3940..0f38d71 100644 --- a/test/builder/filter.test.ts +++ b/test/builder/filter.test.ts @@ -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'); }); @@ -18,8 +18,8 @@ describe('OData Query Builder - Filter Test Suite', () => { expect(ODataFilter.New({ A: 1, B: '2' }).toString()).toBe("A eq 1 and B eq '2'"); expect(ODataFilter.New({ - 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'"); }); @@ -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") + }); + });