@clickup/pg-mig
Version:
PostgreSQL schema migration tool with microsharding and clustering support
34 lines • 1.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.collapse = collapse;
const multi_integer_range_1 = require("multi-integer-range");
const DefaultMap_1 = require("./DefaultMap");
function collapse(list) {
const res = [];
const numberSuffixes = new DefaultMap_1.DefaultMap();
for (const s of list.sort()) {
const match = s.match(/^(.*?)(\d+)$/);
if (match) {
const prefix = match[1];
const numStr = match[2];
const n = parseInt(numStr);
const slot = numberSuffixes.getOrAdd(prefix, {
numbers: [],
widths: new Map(),
});
slot.numbers.push(n);
slot.widths.set(n, numStr.length);
}
else {
res.push(s);
}
}
for (const [prefix, { numbers, widths }] of numberSuffixes.entries()) {
res.push(prefix +
(0, multi_integer_range_1.multirange)(numbers)
.toString()
.replace(/(\d+)/g, (_, n) => { var _a; return n.padStart((_a = widths.get(parseInt(n))) !== null && _a !== void 0 ? _a : 0, "0"); }));
}
return res;
}
//# sourceMappingURL=collapse.js.map