import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import {
resolveEffectiveHomeDir,
resolveHomeRelativePath,
resolveRequiredHomeDir,
} from "./infra/home-dir.js"; import { isPlainObject } from "./infra/plain-object.js";
/** * Check if a file or directory exists at the given path.
*/
export async function pathExists(targetPath: string): Promise<boolean> { try {
await fs.promises.access(targetPath); returntrue;
} catch { returnfalse;
}
}
export function clampNumber(value: number, min: number, max: number): number { return Math.max(min, Math.min(max, value));
}
export function clampInt(value: number, min: number, max: number): number { return clampNumber(Math.floor(value), min, max);
}
/** Alias for clampNumber (shorter, more common name) */
export const clamp = clampNumber;
/** * Escapes special regex characters in a string so it can be used in a RegExp constructor.
*/
export function escapeRegExp(value: string): string { return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
/** * Safely parse JSON, returning null on error instead of throwing.
*/ // oxlint-disable-next-line typescript/no-unnecessary-type-parameters -- JSON parsing helper lets callers ascribe the expected payload type.
export function safeParseJson<T>(raw: string): T | null { try { return JSON.parse(raw) as T;
} catch { returnnull;
}
}
export { isPlainObject };
/** * Type guard for Record<string, unknown> (less strict than isPlainObject). * Accepts any non-null object that isn't an array.
*/
export function isRecord(value: unknown): value is Record<string, unknown> { returntypeof value === "object" && value !== null && !Array.isArray(value);
}
export function sliceUtf16Safe(input: string, start: number, end?: number): string { const len = input.length;
let from = start < 0 ? Math.max(len + start, 0) : Math.min(start, len);
let to = end === undefined ? len : end < 0 ? Math.max(len + end, 0) : Math.min(end, len);
if (to < from) { const tmp = from;
from = to;
to = tmp;
}
if (from > 0 && from < len) { const codeUnit = input.charCodeAt(from); if (isLowSurrogate(codeUnit) && isHighSurrogate(input.charCodeAt(from - 1))) {
from += 1;
}
}
if (to > 0 && to < len) { const codeUnit = input.charCodeAt(to - 1); if (isHighSurrogate(codeUnit) && isLowSurrogate(input.charCodeAt(to))) {
to -= 1;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.