diff --git a/package-lock.json b/package-lock.json index 7ffcf1a..1f6f58a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "spring-filter-query-builder", - "version": "0.1.0", + "version": "0.2.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "spring-filter-query-builder", - "version": "0.1.0", + "version": "0.2.0", "license": "MIT" } } diff --git a/package.json b/package.json index 3abc409..9fef5ea 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/comparators.js b/src/comparators.js index a43a576..53d6edc 100644 --- a/src/comparators.js +++ b/src/comparators.js @@ -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; @@ -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}`; } @@ -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 { @@ -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 }; \ No newline at end of file diff --git a/src/functions.js b/src/functions.js index 302db76..5e0ab91 100644 --- a/src/functions.js +++ b/src/functions.js @@ -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"); } @@ -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(", ") + "')"); } }