assertthat
Version:
assertthat provides fluent TDD.
60 lines (59 loc) • 3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatNestedArray = void 0;
const errors_1 = require("../../errors");
const common_tags_1 = require("common-tags");
const twoDimensionalArrayTransformer = function () {
return {
onSubstitution(substitution, resultSoFar) {
var _a, _b;
if (!Array.isArray(substitution)) {
throw new errors_1.InvalidOperation('Values formatted using the nested inline array transformer must be nested arrays.');
}
const lastSeparatorIndex = substitution.length - 2;
const indentationMatch = /\n(?<indentation>[^\S\n]+)$/u.exec(resultSoFar);
let endResult = '';
for (const [index, part] of substitution.entries()) {
if (!Array.isArray(part)) {
throw new errors_1.InvalidOperation('Values formatted using the nested inline array transformer must be nested arrays.');
}
const isFirstPart = index === 0;
// If there is no indentation, we want to display the array parts
// inline. Thus, no newline, just a space to separate the parts.
endResult += isFirstPart ? '' : `${indentationMatch ? '\n' : ' '}`;
for (const [subIndex, subPart] of part.entries()) {
const isLastSubPart = subIndex === part.length - 1;
const firstStringOverall = isFirstPart && subIndex === 0;
// The first part of the entire substitution does not need an
// indentation, since it is already indented in the enclosing template
// string. That is where the indentation match comes from.
if (indentationMatch && !firstStringOverall) {
endResult += (_b = (_a = indentationMatch.groups) === null || _a === void 0 ? void 0 : _a.indentation) !== null && _b !== void 0 ? _b : '';
}
endResult += subPart;
if (!isLastSubPart) {
if (indentationMatch) {
endResult += '\n';
}
else {
// If there is no indentation, we want to display the array parts
// inline. Thus, no newline, just a space to separate the
// sub-parts.
endResult += ' ';
}
}
}
if (index <= lastSeparatorIndex) {
endResult += ',';
}
}
return endResult;
}
};
};
const formatNestedArray = new common_tags_1.TemplateTag([
twoDimensionalArrayTransformer(),
(0, common_tags_1.stripIndentTransformer)(),
(0, common_tags_1.trimResultTransformer)()
]);
exports.formatNestedArray = formatNestedArray;