UNPKG

jqlgen

Version:
167 lines (165 loc) 5.25 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; // src/index.ts var src_exports = {}; __export(src_exports, { JqlGen: () => JqlGen, jql: () => jql }); module.exports = __toCommonJS(src_exports); var JqlGen = class _JqlGen { constructor(statement) { __publicField(this, "statement"); __publicField(this, "externalStatement"); __publicField(this, "operators"); __publicField(this, "orderByOperators"); this.statement = statement; this.operators = []; this.orderByOperators = []; return this; } and(statement) { const jql2 = statement instanceof _JqlGen ? statement : new _JqlGen(statement); this.forwardOrderBy(jql2); this.operators.push({ jql: jql2, type: "and" }); return this; } or(statement) { const jql2 = statement instanceof _JqlGen ? statement : new _JqlGen(statement); this.forwardOrderBy(jql2); this.operators.push({ jql: jql2, type: "or" }); return this; } orderBy(orderByItem) { this.orderByOperators.push(orderByItem); return this; } injectExternal(str) { const orderByStr = "order by"; const orderByIndex = str.toLowerCase().lastIndexOf(orderByStr); const base = (orderByIndex !== -1 ? str.slice(0, orderByIndex) : str).trim(); const orderBy = (orderByIndex !== -1 ? str.slice(orderByIndex + orderByStr.length) : void 0)?.trim(); this.externalStatement = base; orderBy?.split(",").map((x) => x.trim()).reverse().forEach((by) => { const bySpace = by.split(" ").filter((x) => !!x); this.orderBy({ field: bySpace[0], type: bySpace.at(-1).toLowerCase() }); }); return this; } toString() { const base = this.toStringOperators(); const orderBy = this.toStringOrderBy(); return [base, orderBy].join(" ").trim(); } escapeStatement(value) { if (typeof value === "number") { return value.toString(); } if (value.toLowerCase() === "empty" || value.toLowerCase() === "null") { return value; } value = value.replaceAll("\\", "\\\\"); value = value.replaceAll("'", "\\'"); return `'${value}'`; } forwardOrderBy(from) { this.orderByOperators.unshift(...from.orderByOperators); } toStringStatement() { if (!this.statement && !this.externalStatement) return; if (!this.statement) { return `(${this.externalStatement})`; } let right = ""; if (Array.isArray(this.statement.right)) { right = "("; right += this.statement.right.map((x) => this.escapeStatement(x)).join(","); right += ")"; } else { right = this.escapeStatement(this.statement.right); } let str = "("; str += this.statement.left + " "; str += this.statement.sign + " "; str += right; if (this.externalStatement) { str += ` and (${this.externalStatement})`; } str += ")"; return str; } toStringOperators() { const baseStatement = this.toStringStatement(); let str = "("; str += baseStatement || ""; str += baseStatement ? " " : ""; const nested = this.operators.map((op) => ({ operator: op.jql.toStringOperators(), type: op.type })).filter((x) => !!x.operator).map(({ operator, type }, i) => { if (i === 0 && !baseStatement) { return operator; } return `${type} ${operator}`; }).filter((x) => !!x); if (nested.length === 0) { return baseStatement || ""; } str += nested.join(" "); str += ")"; return str; } toStringOrderBy() { const withoutDupes = this.orderByOperators.filter( (a, i) => this.orderByOperators.findLastIndex((b) => a.field === b.field) === i ); withoutDupes.reverse(); if (withoutDupes.length === 0) return ""; const toString = (item) => [item.field, item.type].join(" "); let str = ["order by", toString(withoutDupes[0])].join(" "); for (let i = 1; i < withoutDupes.length; i++) { str += `, ${toString(withoutDupes[i])}`; } return str; } }; function jql(statement) { return new JqlGen(statement); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { JqlGen, jql });