rsql-filter-builder
Version:
Implementation of RSQL filter builder for NodeJs
9 lines (5 loc) • 6.33 kB
JavaScript
;
exports.Operator = void 0;!function(r){r.AND=";",r.OR=",";}(exports.Operator||(exports.Operator={}));var Operation;!function(r){r.EQUALS="==",r.GREATER=">",r.MINOR="<",r.GREATER_THAN=">=",r.MINOR_THAN="<=",r.NOT_EQUALS="!=",r.IN="=in=",r.OUT="=out=";}(Operation||(Operation={}));
class RSQLFilter{constructor(){this.val="";}and(t){return this.operator(exports.Operator.AND,t)}or(t){return this.operator(exports.Operator.OR,t)}optionalAnd(t){return this.optOperator(exports.Operator.AND,t)}optionalOr(t){return this.optOperator(exports.Operator.OR,t)}equals(t,e){return this.notNull(e)?this.operation(t,e,Operation.EQUALS):this.append("")}notEquals(t,e){return this.notNull(e)?this.operation(t,e,Operation.NOT_EQUALS):this.append("")}greaterThan(t,e){return this.notNull(e)?this.operation(t,e,Operation.GREATER):this.append("")}greaterAndEquals(t,e){return this.notNull(e)?this.operation(t,e,Operation.GREATER_THAN):this.append("")}lessThan(t,e){return this.notNull(e)?this.operation(t,e,Operation.MINOR):this.append("")}lessAndEquals(t,e){return this.notNull(e)?this.operation(t,e,Operation.MINOR_THAN):this.append("")}in(t,...e){return e?.length?this.iterator(t,Operation.IN,e):this.append("")}out(t,...e){return e?.length?this.iterator(t,Operation.OUT,e):this.append("")}value(){return this.val}static new(){return new RSQLFilter}likeAll(t,e){return this.notNull(e)?this.equals(t,`*${e}*`):this.append("")}likeRight(t,e){return this.notNull(e)?this.equals(t,`*${e}`):this.append("")}likeLeft(t,e){return this.notNull(e)?this.equals(t,`${e}*`):this.append("")}notLikeAll(t,e){return this.notNull(e)?this.notEquals(t,`*${e}*`):this.append("")}notLikeRight(t,e){return this.notNull(e)?this.notEquals(t,`*${e}`):this.append("")}notLikeLeft(t,e){return this.notNull(e)?this.notEquals(t,`${e}*`):this.append("")}orLikeAll(t,e){return this.notNull(e)?(this.orIfVal(),this.likeAll(t,e)):this.append("")}andLikeAll(t,e){return this.notNull(e)?(this.andIfVal(),this.likeAll(t,e)):this.append("")}orLikeRight(t,e){return this.notNull(e)?(this.orIfVal(),this.likeRight(t,e)):this.append("")}andLikeRight(t,e){return this.notNull(e)?(this.andIfVal(),this.likeRight(t,e)):this.append("")}orLikeLeft(t,e){return this.notNull(e)?(this.orIfVal(),this.likeLeft(t,e)):this.append("")}andLikeLeft(t,e){return this.notNull(e)?(this.andIfVal(),this.likeLeft(t,e)):this.append("")}orNotLikeAll(t,e){return this.notNull(e)?(this.orIfVal(),this.notLikeAll(t,e)):this.append("")}andNotLikeAll(t,e){return this.notNull(e)?(this.andIfVal(),this.notLikeAll(t,e)):this.append("")}between(t,e,n){return this.notNull(e)&&this.greaterThan(t,e),this.notNull(n)&&this.notNull(e)&&this.and(),this.notNull(n)&&this.lessThan(t,n),this.append("")}strongBetween(t,e,n){return this.notNull(e)&&this.greaterAndEquals(t,e),this.notNull(n)&&this.notNull(e)&&this.and(),this.notNull(n)&&this.lessThan(t,n),this.append("")}orBetween(t,e,n){return this.notNull(e)&&this.notNull(n)?(this.orIfVal(),this.between(t,e,n)):this.append("")}andBetween(t,e,n){return this.notNull(e)&&this.notNull(n)?(this.andIfVal(),this.between(t,e,n)):this.append("")}orStrongBetween(t,e,n){return this.notNull(e)&&this.notNull(n)?(this.orIfVal(),this.between(t,e,n)):this.append("")}andStrongBetween(t,e,n){return this.notNull(e)&&this.notNull(n)?(this.andIfVal(),this.between(t,e,n)):this.append("")}toBeNumber(){const t=/([^\s]+)\s*(==|!=|>=|<=|>|<)\s*['"]([^'"]+)['"]/g.exec(this.val);if(t){const e=t[3]?.replace(/'/g,"");if(isNaN(Number(e))){let e,n=null;const s=t?.input?.match(/^(.*)[,;]\s*([^,;]*)$/),i=/([^\s]+)\s*(==|!=|>=|<=|>|<)\s*(?:"((?:\\.|[^"\\])*)"|'((?:\\.|[^'\\])*)'|([^\s]+))/g;for(;null!==(e=i.exec(s[s?.length-1]));)n=e;if(n){const t=n[0];this.val=this.val.replace(t,"")?.slice(0,-1);}}}return this.append("")}iterate(t){const e=t?.valueList;let n=this.append("");if(t){let s=0;for(const i of e)this.notNull(i)&&(n=0==s?this.addParenthesis(t.operation(i).value()):this.iterateByOperator(t,n,i),s++);return n}return n}andEquals(t,e){return this.notNull(e)?(this.andIfVal(),this.equals(t,e)):this.append("")}orEquals(t,e){return this.notNull(e)?(this.orIfVal(),this.equals(t,e)):this.append("")}andNotEquals(t,e){return this.notNull(e)?(this.andIfVal(),this.notEquals(t,e)):this.append("")}orNotEquals(t,e){return this.notNull(e)?(this.orIfVal(),this.notEquals(t,e)):this.append("")}andGreaterThan(t,e){return this.notNull(e)?(this.andIfVal(),this.greaterThan(t,e)):this.append("")}orGreaterThan(t,e){return this.notNull(e)?(this.orIfVal(),this.greaterThan(t,e)):this.append("")}andGreaterAndEquals(t,e){return this.notNull(e)?(this.andIfVal(),this.greaterThan(t,e)):this.append("")}orGreaterAndEquals(t,e){return this.notNull(e)?(this.orIfVal(),this.greaterAndEquals(t,e)):this.append("")}andLessThan(t,e){return this.notNull(e)?(this.andIfVal(),this.lessThan(t,e)):this.append("")}orLessThan(t,e){return this.notNull(e)?(this.orIfVal(),this.lessThan(t,e)):this.append("")}andLessAndEquals(t,e){return this.notNull(e)?(this.andIfVal(),this.lessAndEquals(t,e)):this.append("")}orLessAndEquals(t,e){return this.notNull(e)?(this.orIfVal(),this.lessAndEquals(t,e)):this.append("")}operation(t,e,n,s){return this.append(t).append(n),this.setValue(e,s)}operator(t,e){return this.append(t),e&&this.addParenthesis(e.value()),this}addParenthesis(t){return this.append("(").append(t).append(")")}iterateByOperator(t,e,n){const s=t.operator;return s===exports.Operator.AND?e.and().addParenthesis(t.operation(n).value()):s===exports.Operator.OR?e.or().addParenthesis(t.operation(n).value()):this}iterator(t,e,...n){const s=this.parenthesisIn(n);return this.operation(t,s,e,true)}parenthesisIn(t){let e="";const n=t[0].length;for(let s=0;s<n;s++)e=e.concat(this.setItem(t[0][s])),s<n-1&&(e=e.concat(","));return `(${e})`}setItem(t){return "string"==typeof t?`'${t}'`:t}setValue(t,e){return "string"!=typeof t||e?this.append(t):this.append("'").append(this.fixedValue(t)).append("'")}fixedValue(t){return t.replace(/'/g,"\\'")}append(t){return this.val+=String(t),this}notNull(t){return null!=t&&""!=t}orIfVal(t){this.val&&this.or(t);}andIfVal(t){this.val&&this.and(t);}optOperator(t,e){return this.val&&e?.value()?t===exports.Operator.AND?this.and(e):this.or(e):this.append(e?.value())??this.append("")}}
exports.RSQLFilter = RSQLFilter;
//# sourceMappingURL=index.js.map