sql-formatter
Version:
Format whitespace in a SQL query to make it more readable
99 lines (74 loc) • 3.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = formatAliasPositions;
var _utils = require("../utils");
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
/**
* Handles select alias placement - tabulates if enabled
*/
function formatAliasPositions(query) {
var lines = query.split('\n');
var newQuery = [];
for (var i = 0; i < lines.length; i++) {
// find SELECT rows with trailing comma, if no comma (only one row) - no-op
if (lines[i].match(/^\s*SELECT/i)) {
var _ret = function () {
var aliasLines = [];
if (lines[i].match(/.*,$/)) {
aliasLines = [lines[i]]; // add select to aliasLines in case of tabular formats
} else {
newQuery.push(lines[i]); // add select to new query
if (lines[i].match(/^\s*SELECT\s+.+(?!,$)/i)) {
return "continue";
}
aliasLines.push(lines[++i]);
} // get all lines in SELECT clause
while (lines[i++].match(/.*,$/)) {
aliasLines.push(lines[i]);
} // break lines into alias with optional AS, and all preceding text
var splitLines = aliasLines.map(function (line) {
return {
line: line,
matches: line.match(/(^.*?\S) (AS )?(\S+,?$)/i)
};
}).map(function (_ref) {
var line = _ref.line,
matches = _ref.matches;
if (!matches) {
return {
precedingText: line
};
}
return {
precedingText: matches[1],
as: matches[2],
alias: matches[3]
};
}); // get longest of precedingText, trim trailing comma for non-alias columns
var aliasMaxLength = (0, _utils.maxLength)(splitLines.map(function (_ref2) {
var precedingText = _ref2.precedingText;
return precedingText.replace(/\s*,\s*$/, '');
})); // re-construct line, aligning by inserting space before AS or alias
aliasLines = splitLines.map(function (_ref3) {
var precedingText = _ref3.precedingText,
as = _ref3.as,
alias = _ref3.alias;
return precedingText + (alias ? ' '.repeat(aliasMaxLength - precedingText.length + 1) + (as !== null && as !== void 0 ? as : '') + alias : '');
});
newQuery = [].concat(_toConsumableArray(newQuery), _toConsumableArray(aliasLines));
}();
if (_ret === "continue") continue;
}
newQuery.push(lines[i]);
}
return newQuery.join('\n');
}
module.exports = exports.default;
//# sourceMappingURL=formatAliasPositions.js.map