scai
Version:
> **AI-powered CLI for local code analysis, commit message suggestions, and natural-language queries.** 100% local, private, GDPR-friendly, made in Denmark/EU with ❤️.
50 lines (49 loc) • 1.7 kB
JavaScript
import os from 'os';
import path from "path";
/**
* Normalizes a path string for loose, fuzzy matching:
* - Lowercases
* - Removes slashes and backslashes
* - Removes whitespace
*/
export function normalizePathForLooseMatch(p) {
return p.toLowerCase().replace(/[\\/]/g, '').replace(/\s+/g, '');
}
// Helper to normalize and resolve paths to a consistent format (forward slashes)
export function normalizePath(p) {
if (p.startsWith('~')) {
p = path.join(os.homedir(), p.slice(1));
}
return path.resolve(p).replace(/\\/g, '/');
}
export function getRepoKeyForPath(pathToMatch, config) {
const norm = normalizePath(pathToMatch);
return Object.entries(config.repos).find(([, val]) => normalizePath(val.indexDir) === norm)?.[0] || null;
}
export function normalizeText(txt) {
return txt.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
}
export function stripMarkdownFences(txt) {
return txt
.replace(/```[\w-]*\s*/g, "") // ``` or ```java
.replace(/```/g, ""); // closing ```
}
// Very naive classifier: decide if a line is "code-like"
export function isCodeLike(line) {
const trimmed = line.trim();
if (!trimmed)
return false;
// obvious markdown / prose markers
if (/^(This|Here is|Note)\b/.test(trimmed))
return false;
if (/^\d+\./.test(trimmed))
return false; // bullet list
if (/^[-*] /.test(trimmed))
return false; // list
// allow imports, class, functions, braces, annotations, etc.
if (/^(import|export|public|private|protected|class|function|@Test|@Before)/.test(trimmed))
return true;
if (/[;{}()=]/.test(trimmed))
return true;
return false;
}