@ai2070/l0
Version:
L0: The Missing Reliability Substrate for AI
179 lines • 5.24 kB
JavaScript
export function normalizeNewlines(text) {
if (!text)
return text;
return text.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
}
export function normalizeWhitespace(text, options = {}) {
if (!text)
return text;
const { collapseSpaces = false, trimLines = false, removeEmptyLines = false, } = options;
let result = text;
result = normalizeNewlines(result);
if (collapseSpaces) {
result = result.replace(/ {2,}/g, " ");
}
if (trimLines) {
result = result
.split("\n")
.map((line) => line.trim())
.join("\n");
}
if (removeEmptyLines) {
result = result
.split("\n")
.filter((line) => line.trim().length > 0)
.join("\n");
}
return result;
}
export function normalizeIndentation(text, mode = "spaces", spacesPerTab = 2) {
if (!text)
return text;
const lines = normalizeNewlines(text).split("\n");
if (mode === "spaces") {
return lines
.map((line) => line.replace(/\t/g, " ".repeat(spacesPerTab)))
.join("\n");
}
else {
return lines
.map((line) => {
let converted = line;
const leadingSpaces = line.match(/^ +/);
if (leadingSpaces) {
const spaces = leadingSpaces[0].length;
const tabs = Math.floor(spaces / spacesPerTab);
const remainingSpaces = spaces % spacesPerTab;
converted =
"\t".repeat(tabs) +
" ".repeat(remainingSpaces) +
line.slice(spaces);
}
return converted;
})
.join("\n");
}
}
export function dedent(text) {
if (!text)
return text;
const lines = normalizeNewlines(text).split("\n");
let minIndent = Infinity;
for (const line of lines) {
if (line.trim().length === 0)
continue;
const indent = line.match(/^[ \t]*/)?.[0].length ?? 0;
minIndent = Math.min(minIndent, indent);
}
if (minIndent === Infinity || minIndent === 0) {
return text;
}
return lines
.map((line) => {
if (line.trim().length === 0)
return line;
return line.slice(minIndent);
})
.join("\n");
}
export function indent(text, indent = 2) {
if (!text)
return text;
const indentStr = typeof indent === "number" ? " ".repeat(indent) : indent;
const lines = normalizeNewlines(text).split("\n");
return lines
.map((line) => (line.trim().length > 0 ? indentStr + line : line))
.join("\n");
}
export function trimText(text) {
if (!text)
return text;
const lines = normalizeNewlines(text).split("\n");
while (lines.length > 0 && lines[0].trim().length === 0) {
lines.shift();
}
while (lines.length > 0 && lines[lines.length - 1].trim().length === 0) {
lines.pop();
}
return lines.join("\n").trim();
}
export function normalizeText(text, options = {}) {
if (!text)
return text;
const { newlines = true, whitespace = false, indentation = false, spacesPerTab = 2, dedent: shouldDedent = false, trim = false, } = options;
let result = text;
if (newlines) {
result = normalizeNewlines(result);
}
if (whitespace) {
result = normalizeWhitespace(result, {
collapseSpaces: true,
trimLines: false,
removeEmptyLines: false,
});
}
if (indentation) {
result = normalizeIndentation(result, indentation, spacesPerTab);
}
if (shouldDedent) {
result = dedent(result);
}
if (trim) {
result = trimText(result);
}
return result;
}
export function ensureTrailingNewline(text) {
if (!text)
return text;
const normalized = normalizeNewlines(text);
const trimmed = normalized.replace(/\n+$/, "");
return trimmed + "\n";
}
export function removeTrailingWhitespace(text) {
if (!text)
return text;
return normalizeNewlines(text)
.split("\n")
.map((line) => line.replace(/[ \t]+$/, ""))
.join("\n");
}
export function normalizeForModel(text) {
if (!text)
return text;
return normalizeText(text, {
newlines: true,
whitespace: true,
trim: true,
});
}
export function isWhitespaceOnly(text) {
if (!text)
return true;
return /^[\s\r\n\t]*$/.test(text);
}
export function countLines(text) {
if (!text)
return 0;
return normalizeNewlines(text).split("\n").length;
}
export function getLine(text, lineIndex) {
if (!text)
return null;
const lines = normalizeNewlines(text).split("\n");
if (lineIndex < 0 || lineIndex >= lines.length) {
return null;
}
return lines[lineIndex] ?? null;
}
export function replaceLine(text, lineIndex, newLine) {
if (!text)
return text;
const lines = normalizeNewlines(text).split("\n");
if (lineIndex < 0 || lineIndex >= lines.length) {
return text;
}
lines[lineIndex] = newLine;
return lines.join("\n");
}
//# sourceMappingURL=normalize.js.map