jql-parser
Version:
* Validating queries
74 lines (73 loc) • 2.8 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Sentence = void 0;
var Expression_1 = require("./Expression");
var OrderBy_1 = require("./OrderBy");
var Token_1 = require("./Token");
var Sentence = /** @class */ (function (_super) {
__extends(Sentence, _super);
function Sentence() {
return _super !== null && _super.apply(this, arguments) || this;
}
// TODO: no global normalization! every token should do itself!
Sentence.normalize = function (jql) {
var s = jql.replace(/[ ]{2,}/g, " ");
s = s.replace(/ and /ig, " AND ");
s = s.replace(/ or /ig, " OR ");
s = s.replace(/ in\s?\(/ig, " IN (");
s = s.replace(/ order by /i, " ORDER BY ");
s = s.replace(/ asc$/i, " ASC");
s = s.replace(/ desc$/i, " DESC");
s = s.replace(/ in openSprint/i, " IN openSprint");
return s.trim();
};
Object.defineProperty(Sentence.prototype, "end", {
get: function () {
var end = this.expression.end;
if (this.orderBy) {
end += this.orderBy.end;
}
return end;
},
enumerable: false,
configurable: true
});
// separate ORDER by clause from the EXPRESSION
Sentence.prototype.matcherFn = function (str) {
var ix = str.indexOf('ORDER');
if (ix === -1) {
ix = str.length;
}
return ix;
};
Sentence.prototype.parse = function (input) {
this.content = Sentence.normalize(input);
this.expression = new Expression_1.Expression();
var end = this.matcherFn(this.content);
var expressionPart = this.content.substr(0, end);
if (this.expression.parse(expressionPart)) {
if (this.content.length !== end) {
var orderByPart = this.content.substr(end, this.content.length - expressionPart.length);
this.orderBy = new OrderBy_1.OrderBy();
return this.orderBy.parse(orderByPart);
}
return true;
}
return false;
};
return Sentence;
}(Token_1.Token));
exports.Sentence = Sentence;