UNPKG

smart-column-indenter

Version:

A smart source code indenter that indent the code into columns

34 lines 1.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class Language { pad(str, length, char = " ") { return str + new Array((length - str.length) + 1).join(char); } /** * Inside and object literal, array literal or a function parameter list where there is one item per line, * it's common to have a missing comma at the end of the last line (or the beginning of the first line), so * there is no way of this comma be part of the LCS, or if it is, it is not what we want. Then, it's best * to remove it before execute the LCS */ removeHeadOrTailMissingToken(lines, missingToken) { const allButFirstStartsWithComma = lines.every((line, i) => this.xor(line[0].content === missingToken, i === 0)); const allButLastEndsWithComma = lines.every((line, i) => this.xor(line[line.length - 1].content === missingToken, i === (lines.length - 1))); if (allButFirstStartsWithComma) { return lines.map((line, i) => i === 0 ? line : line.slice(1)); } else if (allButLastEndsWithComma) { return lines.map((line, i) => i === (lines.length - 1) ? line : line.slice(0, -1)); } else { return lines; } } /** * Binary XOR operation with boolean (the ^ operator just works with numbers) */ xor(left, right) { return !!(+left ^ +right); } } exports.default = Language; //# sourceMappingURL=Language.js.map