Skip to content

Commit 4dfaad5

Browse files
authored
Fix/numeric conversion decimal issue (#4)
* fix: missing decimals at numeric conversion * add: further unit tests * change: incerase version number to 1.1.2
1 parent 43f6153 commit 4dfaad5

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rsql-query-builder",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "Library for building RSQL query strings.",
55
"main": "dist/index.js",
66
"module": "dist/index.mjs",

src/RSQLBuilder.test.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { describe, it, expect } from 'vitest';
2-
import RSQLBuilder from './RSQLBuilder';
2+
import { RSQLBuilder } from './';
33

44
describe('RSQLBuilder', () => {
55
it("Test operator Equal ('==')", () => {
66
expect(new RSQLBuilder().equal('name', 'Filip').toString()).toBe('name=="Filip"');
77
expect(new RSQLBuilder().equal('age', 30).toString()).toBe('age==30');
8+
expect(new RSQLBuilder().equal('weight', 68.3).toString()).toBe('weight==68.3');
89
expect(new RSQLBuilder().equal('adult', true).toString()).toBe('adult==true');
910
expect(new RSQLBuilder().equal('created', new Date('2022-04-23T05:46:17.497Z')).toString()).toBe(
1011
'created==2022-04-23T05:46:17.497Z'
@@ -15,6 +16,8 @@ describe('RSQLBuilder', () => {
1516
it("Test operator Not Equal ('!=')", () => {
1617
expect(new RSQLBuilder().notEqual('name', 'Filip').toString()).toBe('name!="Filip"');
1718
expect(new RSQLBuilder().notEqual('age', 30).toString()).toBe('age!=30');
19+
expect(new RSQLBuilder().notEqual('weight', 68.3).toString()).toBe('weight!=68.3');
20+
expect(new RSQLBuilder().notEqual('adult', false).toString()).toBe('adult!=false');
1821
expect(new RSQLBuilder().notEqual('adult', true).toString()).toBe('adult!=true');
1922
expect(new RSQLBuilder().notEqual('created', new Date('2022-04-23T05:46:17.497Z')).toString()).toBe(
2023
'created!=2022-04-23T05:46:17.497Z'
@@ -24,12 +27,12 @@ describe('RSQLBuilder', () => {
2427

2528
it("Test operator In ('=in=')", () => {
2629
expect(new RSQLBuilder().in('name', ['Filip', 'John']).toString()).toBe('name=in=("Filip","John")');
27-
expect(new RSQLBuilder().in('name', [30, 45]).toString()).toBe('name=in=(30,45)');
30+
expect(new RSQLBuilder().in('name', [30, 45, 55.5]).toString()).toBe('name=in=(30,45,55.5)');
2831
});
2932

3033
it("Test operator Not In ('=out=')", () => {
3134
expect(new RSQLBuilder().notIn('name', ['Filip', 'John']).toString()).toBe('name=out=("Filip","John")');
32-
expect(new RSQLBuilder().notIn('name', [30, 45]).toString()).toBe('name=out=(30,45)');
35+
expect(new RSQLBuilder().notIn('name', [30, 45, 55.5]).toString()).toBe('name=out=(30,45,55.5)');
3336
});
3437

3538
it("Test operator Less Than ('=lt=')", () => {
@@ -54,7 +57,38 @@ describe('RSQLBuilder', () => {
5457
});
5558

5659
it("Test logic operator OR (',')", () => {
57-
expect(new RSQLBuilder().and().toString()).toBe(';');
60+
expect(new RSQLBuilder().or().toString()).toBe(',');
5861
expect(new RSQLBuilder().equal('name', 'John').or().equal('age', 30).toString()).toBe('name=="John",age==30');
5962
});
63+
64+
it("Test default logic operator AND (';')", () => {
65+
expect(new RSQLBuilder({ defaultLogicOperator: 'and' }).and().toString()).toBe(';');
66+
expect(new RSQLBuilder({ defaultLogicOperator: 'and' }).equal('name', 'John').equal('age', 30).toString()).toBe(
67+
'name=="John";age==30'
68+
);
69+
});
70+
71+
it("Test default logic operator OR (',')", () => {
72+
expect(new RSQLBuilder({ defaultLogicOperator: 'or' }).or().toString()).toBe(',');
73+
expect(new RSQLBuilder({ defaultLogicOperator: 'or' }).equal('name', 'John').equal('age', 30).toString()).toBe(
74+
'name=="John",age==30'
75+
);
76+
});
77+
78+
it('Test prevent consecutive occurence of logic operators', () => {
79+
expect(new RSQLBuilder().and().or().and().and().or().or().and().toString()).toBe(';');
80+
expect(
81+
new RSQLBuilder().equal('name', 'John').and().or().and().and().or().or().and().equal('age', 30).toString()
82+
).toBe('name=="John";age==30');
83+
});
84+
85+
it('Test isEmpty() and reset()', () => {
86+
expect(new RSQLBuilder().isEmpty()).toBe(true);
87+
expect(new RSQLBuilder().equal('name', 'John').and().equal('age', 30).reset().isEmpty()).toBe(true);
88+
expect(new RSQLBuilder().equal('name', 'John').and().equal('age', 30).isEmpty()).toBe(false);
89+
});
90+
91+
it('Test group()', () => {
92+
expect(new RSQLBuilder().equal('name', 'John').and().group(new RSQLBuilder().equal('age', 30).or().equal('age', null)).toString()).toBe('name==\"John\";(age==30,age==null)');
93+
});
6094
});

src/RSQLBuilderBase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class RSQLBuilderBase<TSelector extends string, TCustomComparisonOperator extend
9595
): string {
9696
if (value === null) return 'null';
9797
if (typeof value === 'string') return '"' + this.escapeString(value) + '"';
98-
if (typeof value === 'number') return value.toFixed();
98+
if (typeof value === 'number') return value.toString();
9999
if (typeof value === 'boolean') return value === true ? 'true' : 'false';
100100
if (value instanceof Date) return value.toISOString();
101101
throw new Error('Unhandled value type.');

0 commit comments

Comments
 (0)