Skip to content

Commit

Permalink
Adding support for number type as a value to a comparator.
Browse files Browse the repository at this point in the history
Fixing Concat function.
  • Loading branch information
sisimomo committed Nov 6, 2021
1 parent 8ecbd39 commit a35ecd9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 29 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spring-filter-query-builder",
"version": "0.1.0",
"version": "0.2.0",
"description": "A simple Query Builder for Spring Filter",
"main": "scr/index.js",
"scripts": {
Expand Down
27 changes: 18 additions & 9 deletions src/comparators.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class Comparator extends Item { //Abstract
throw `${new.target.name} parameter 'selector' must be a Function or a String!`;
}
if (value) {
if (!(typeof value === 'string' || value instanceof String)) {
throw `${new.target.name} parameter 'value' must be a String!`;
if (!(typeof value === 'string' || value instanceof String || typeof value === 'number' || value instanceof Number)) {
throw `${new.target.name} parameter 'value' must be a String or a Number!`;
}
}
this.selector = selector;
Expand All @@ -21,7 +21,11 @@ class Comparator extends Item { //Abstract

toString() {
if (this.value) {
return `${this.selector} ${this.comparatorKeyWord} ${this.value}`;
if (typeof this.value === 'number' || this.value instanceof Number) {
return `${this.selector} ${this.comparatorKeyWord} ${this.value}`;
} else {
return `${this.selector} ${this.comparatorKeyWord} '${this.value}'`;
}
} else {
return `${this.selector} ${this.comparatorKeyWord}`;
}
Expand All @@ -30,37 +34,37 @@ class Comparator extends Item { //Abstract

class Like extends Comparator {
constructor(selector, value) {
super(selector, "~",value);
super(selector, "~", value);
}
}

class Equal extends Comparator {
constructor(selector, value) {
super(selector, ":",value);
super(selector, ":", value);
}
}

class NotEqual extends Comparator {
constructor(selector, value) {
super(selector, "!",value);
super(selector, "!", value);
}
}

class Gt extends Comparator {
constructor(selector, value) {
super(selector, ">",value);
super(selector, ">", value);
}
}

class Ge extends Comparator {
constructor(selector, value) {
super(selector, ">:",value);
super(selector, ">:", value);
}
}

class Lt extends Comparator {
constructor(selector, value) {
super(selector, "<",value);
super(selector, "<", value);
}
}
class Le extends Comparator {
Expand Down Expand Up @@ -97,6 +101,11 @@ class In extends Comparator {
constructor(selector, ...values) {
super(selector, "IN", "('" + values.join("', '") + "')");
}

toString() {
return `${this.selector} ${this.comparatorKeyWord} ${this.value}`;
}

}

export { Comparator, Like, Equal, NotEqual, Gt, Ge, Lt, Le, IsNull, IsNotNull, IsEmpty, IsNotEmpty, In };
39 changes: 22 additions & 17 deletions src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Item } from './item.js';
import { Comparator } from './comparators.js';

class Function extends Item { //Abstract
constructor(selector, functionKeyWord) {
constructor(functionKeyWord, selector) {
if (new.target === Comparator) {
throw new TypeError("Cannot construct " + new.target.name + " instances directly");
}
Expand All @@ -15,91 +15,96 @@ class Function extends Item { //Abstract
}

toString() {
return this.functionKeyWord + " (" + this.selector + ")";
if (this.selector) {
return this.functionKeyWord + "(" + this.selector + ")";
} else {
return this.functionKeyWord + "()";

}
}
}

class Absolute extends Function {
constructor(selector) {
super(selector, "ABSOLUTE");
super("ABSOLUTE", selector);
}
}

class Average extends Function {
constructor(selector) {
super(selector, "AVERAGE");
super("AVERAGE", selector);
}
}

class Min extends Function {
constructor(selector) {
super(selector, "MIN");
super("MIN", selector);
}
}

class Max extends Function {
constructor(selector) {
super(selector, "MAX");
super("MAX", selector);
}
}

class Sum extends Function {
constructor(selector) {
super(selector, "SUM");
super("SUM", selector);
}
}

class CurrentDate extends Function {
constructor() {
super("", "CURRENTDATE");
super("CURRENTDATE");
}
}

class CurrentTime extends Function {
constructor() {
super("", "CURRENTTIME");
super("CURRENTTIME");
}
}

class CurrentTimestamp extends Function {
constructor() {
super("", "CURRENTTIMESSTAMP");
super("CURRENTTIMESSTAMP");
}
}

class Size extends Function {
constructor(selector) {
super(selector, "SIZE");
super("SIZE", selector);
}
}

class Length extends Function {
constructor(selector) {
super(selector, "LENGTH");
super("LENGTH", selector);
}
}

class Trim extends Function {
constructor(selector) {
super(selector, "TRIM");
super("TRIM", selector);
}
}

class Upper extends Function {
constructor(selector) {
super(selector, "UPPER");
super("UPPER", selector);
}
}

class Lower extends Function {
constructor(selector) {
super(selector, "LOWER");
super("LOWER", selector);
}
}

class Concat extends Function {
constructor(selector) {
super(selector, "CONCAT");
constructor(...selectors) {
super("CONCAT", "('" + selectors.join(", ") + "')");
}
}

Expand Down

0 comments on commit a35ecd9

Please sign in to comment.