Skip to content

Commit cc00acd

Browse files
authored
Merge pull request #77 from odobosh-lohika-tix/support-for-decimal-data-type
Added support of decimal numbers
2 parents 31ff7f8 + 3d10fde commit cc00acd

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,14 @@ const filter = { "someProp": { eq: { type: 'binary', value: 'YmluYXJ5RGF0YQ==' }
351351
buildQuery({ filter })
352352
=> "?$filter=someProp eq binary'YmluYXJ5RGF0YQ=='"
353353
```
354+
355+
Decimal:
356+
```js
357+
const filter = { "someProp": { eq: { type: 'decimal', value: '12.3456789' } } };
358+
buildQuery({ filter })
359+
=> "?$filter=someProp eq '12.3456789M'"
360+
```
361+
354362
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/).
355363

356364
Other types coming soon

src/index.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,16 @@ export type Duration = { type: 'duration'; value: any; }
5757
export type Binary = { type: 'binary'; value: any; }
5858
export type Json = { type: 'json'; value: any; }
5959
export type Alias = { type: 'alias'; name: string; value: any; }
60-
export type Value = string | Date | number | boolean | Raw | Guid | Duration | Binary | Json | Alias;
60+
export type Decimal = { type: 'decimal'; value: any; }
61+
export type Value = string | Date | number | boolean | Raw | Guid | Duration | Binary | Json | Alias | Decimal;
6162

6263
export const raw = (value: string): Raw => ({ type: 'raw', value });
6364
export const guid = (value: string): Guid => ({ type: 'guid', value });
6465
export const duration = (value: string): Duration => ({ type: 'duration', value });
6566
export const binary = (value: string): Binary => ({ type: 'binary', value });
6667
export const json = (value: PlainObject): Json => ({ type: 'json', value });
6768
export const alias = (name: string, value: PlainObject): Alias => ({ type: 'alias', name, value });
69+
export const decimal = (value: string): Decimal => ({ type: 'decimal', value });
6870

6971
export type QueryOptions<T> = ExpandOptions<T> & {
7072
search: string;
@@ -368,29 +370,31 @@ function handleValue(value: Value, aliases?: Alias[]): any {
368370
} else if (value === null) {
369371
return value;
370372
} else if (typeof value === 'object') {
371-
if (value.type === 'raw') {
372-
return value.value;
373-
} else if (value.type === 'guid') {
373+
switch (value.type) {
374+
case 'raw':
375+
case 'guid':
374376
return value.value;
375-
} else if (value.type === 'duration') {
377+
case 'duration':
376378
return `duration'${value.value}'`;
377-
} else if (value.type === 'binary') {
379+
case 'binary':
378380
return `binary'${value.value}'`;
379-
} else if (value.type === 'alias') {
380-
// Store
381-
if (Array.isArray(aliases))
382-
aliases.push(value as Alias);
383-
return `@${(value as Alias).name}`;
384-
} else if (value.type === 'json') {
381+
case 'alias':
382+
// Store
383+
if (Array.isArray(aliases))
384+
aliases.push(value as Alias);
385+
return `@${(value as Alias).name}`;
386+
case 'json':
385387
return escape(JSON.stringify(value.value));
386-
} else {
387-
return Object.entries(value)
388-
.filter(([, v]) => v !== undefined)
389-
.map(([k, v]) => `${k}=${handleValue(v as Value, aliases)}`).join(',');
390-
}
388+
case 'decimal':
389+
return `${value.value}M`;
390+
default:
391+
return Object.entries(value)
392+
.filter(([, v]) => v !== undefined)
393+
.map(([k, v]) => `${k}=${handleValue(v as Value, aliases)}`).join(',');
391394
}
392-
return value;
393395
}
396+
return value;
397+
}
394398

395399
function buildExpand<T>(expands: Expand<T>): string {
396400
if (typeof expands === 'number') {

test/index.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import buildQuery, {Expand, OrderBy, alias, json, ITEM_ROOT} from '../src/index';
1+
import buildQuery, {Expand, OrderBy, alias, json, ITEM_ROOT, decimal} from '../src/index';
22

33
it('should return an empty string by default', () => {
44
expect(buildQuery()).toEqual('');
@@ -751,6 +751,13 @@ describe('filter', () => {
751751
expect(actual).toEqual(expected);
752752
});
753753

754+
it('should handle decimal number', () => {
755+
const filter = { NumberProp: decimal('1.23456789') };
756+
const expected = '?$filter=NumberProp eq 1.23456789M';
757+
const actual = buildQuery({ filter });
758+
expect(actual).toEqual(expected);
759+
});
760+
754761
it('should handle a string', () => {
755762
const filter = { StringProp: '2' };
756763
const expected = "?$filter=StringProp eq '2'";

0 commit comments

Comments
 (0)