Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support of decimal numbers #77

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@ const filter = { "someProp": { eq: { type: 'binary', value: 'YmluYXJ5RGF0YQ==' }
buildQuery({ filter })
=> "?$filter=someProp eq binary'YmluYXJ5RGF0YQ=='"
```

Decimal:
```js
const filter = { "someProp": { eq: { type: 'decimal', value: '12.3456789' } } };
buildQuery({ filter })
=> "?$filter=someProp eq '12.3456789M'"
```

Note that as per OData specification, binary data is transmitted as a base64 encoded string. Refer to [Primitive Types in JSON Format](https://www.odata.org/documentation/odata-version-2-0/json-format/), and [binary representation](https://www.odata.org/documentation/odata-version-2-0/overview/).

Other types coming soon
Expand Down
40 changes: 22 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ export type Duration = { type: 'duration'; value: any; }
export type Binary = { type: 'binary'; value: any; }
export type Json = { type: 'json'; value: any; }
export type Alias = { type: 'alias'; name: string; value: any; }
export type Value = string | Date | number | boolean | Raw | Guid | Duration | Binary | Json | Alias;
export type Decimal = { type: 'decimal'; value: any; }
export type Value = string | Date | number | boolean | Raw | Guid | Duration | Binary | Json | Alias | Decimal;

export const raw = (value: string): Raw => ({ type: 'raw', value });
export const guid = (value: string): Guid => ({ type: 'guid', value });
export const duration = (value: string): Duration => ({ type: 'duration', value });
export const binary = (value: string): Binary => ({ type: 'binary', value });
export const json = (value: PlainObject): Json => ({ type: 'json', value });
export const alias = (name: string, value: PlainObject): Alias => ({ type: 'alias', name, value });
export const decimal = (value: string): Decimal => ({ type: 'decimal', value });

export type QueryOptions<T> = ExpandOptions<T> & {
search: string;
Expand Down Expand Up @@ -368,29 +370,31 @@ function handleValue(value: Value, aliases?: Alias[]): any {
} else if (value === null) {
return value;
} else if (typeof value === 'object') {
if (value.type === 'raw') {
return value.value;
} else if (value.type === 'guid') {
switch (value.type) {
case 'raw':
case 'guid':
return value.value;
} else if (value.type === 'duration') {
case 'duration':
return `duration'${value.value}'`;
} else if (value.type === 'binary') {
case 'binary':
return `binary'${value.value}'`;
} else if (value.type === 'alias') {
// Store
if (Array.isArray(aliases))
aliases.push(value as Alias);
return `@${(value as Alias).name}`;
} else if (value.type === 'json') {
case 'alias':
// Store
if (Array.isArray(aliases))
aliases.push(value as Alias);
return `@${(value as Alias).name}`;
case 'json':
return escape(JSON.stringify(value.value));
} else {
return Object.entries(value)
.filter(([, v]) => v !== undefined)
.map(([k, v]) => `${k}=${handleValue(v as Value, aliases)}`).join(',');
}
case 'decimal':
return `${value.value}M`;
default:
return Object.entries(value)
.filter(([, v]) => v !== undefined)
.map(([k, v]) => `${k}=${handleValue(v as Value, aliases)}`).join(',');
}
return value;
}
return value;
}

function buildExpand<T>(expands: Expand<T>): string {
if (typeof expands === 'number') {
Expand Down
9 changes: 8 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import buildQuery, {Expand, OrderBy, alias, json, ITEM_ROOT} from '../src/index';
import buildQuery, {Expand, OrderBy, alias, json, ITEM_ROOT, decimal} from '../src/index';

it('should return an empty string by default', () => {
expect(buildQuery()).toEqual('');
Expand Down Expand Up @@ -751,6 +751,13 @@ describe('filter', () => {
expect(actual).toEqual(expected);
});

it('should handle decimal number', () => {
const filter = { NumberProp: decimal('1.23456789') };
const expected = '?$filter=NumberProp eq 1.23456789M';
const actual = buildQuery({ filter });
expect(actual).toEqual(expected);
});

it('should handle a string', () => {
const filter = { StringProp: '2' };
const expected = "?$filter=StringProp eq '2'";
Expand Down