iterate.com / ui

git clone https://git.iterate.com/repos/ui

ui / src / components / repo-ide / repo-file-tree-paths.tsmain at d68d7e1

/** The next free `untitled.<extension>` (then `untitled-1.…`) inside a directory. */
export function untitledPath(
  directoryPath: string | null,
  taken: ReadonlySet<string>,
  extension = "txt",
): string {
  const prefix = !directoryPath
    ? ""
    : directoryPath.endsWith("/")
      ? directoryPath
      : `${directoryPath}/`;
  for (let n = 0; ; n++) {
    const candidate = `${prefix}untitled${n === 0 ? "" : `-${n}`}.${extension}`;
    if (!taken.has(candidate)) return candidate;
  }
}