UNPKG

@ai2070/l0

Version:

L0: The Missing Reliability Substrate for AI

170 lines (169 loc) 4.71 kB
function normalizeNewlines(text) { if (!text) return text; return text.replace(/\r\n/g, "\n").replace(/\r/g, "\n"); } 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; } 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 = " ".repeat(tabs) + " ".repeat(remainingSpaces) + line.slice(spaces); } return converted; }).join("\n"); } } 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 indent2 = line.match(/^[ \t]*/)?.[0].length ?? 0; minIndent = Math.min(minIndent, indent2); } if (minIndent === Infinity || minIndent === 0) { return text; } return lines.map((line) => { if (line.trim().length === 0) return line; return line.slice(minIndent); }).join("\n"); } function indent(text, indent2 = 2) { if (!text) return text; const indentStr = typeof indent2 === "number" ? " ".repeat(indent2) : indent2; const lines = normalizeNewlines(text).split("\n"); return lines.map((line) => line.trim().length > 0 ? indentStr + line : line).join("\n"); } 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(); } 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; } function ensureTrailingNewline(text) { if (!text) return text; const normalized = normalizeNewlines(text); const trimmed = normalized.replace(/\n+$/, ""); return trimmed + "\n"; } function removeTrailingWhitespace(text) { if (!text) return text; return normalizeNewlines(text).split("\n").map((line) => line.replace(/[ \t]+$/, "")).join("\n"); } function normalizeForModel(text) { if (!text) return text; return normalizeText(text, { newlines: true, whitespace: true, trim: true }); } function isWhitespaceOnly(text) { if (!text) return true; return /^[\s\r\n\t]*$/.test(text); } function countLines(text) { if (!text) return 0; return normalizeNewlines(text).split("\n").length; } 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; } 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"); } export { countLines, dedent, ensureTrailingNewline, getLine, indent, isWhitespaceOnly, normalizeForModel, normalizeIndentation, normalizeNewlines, normalizeText, normalizeWhitespace, removeTrailingWhitespace, replaceLine, trimText }; //# sourceMappingURL=normalize.js.map