Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

1.0 KB
Raw Blame
import { Result } from "effect";
import { InvalidArgument } from "../errors/errors.js";

const MIN_PREFIX = 4;
const FULL_ID_LENGTH = 20;

export function matchNotificationId(
  known: ReadonlyArray<string>,
  reference: string,
): Result.Result<string, InvalidArgument> {
  const wanted = reference.trim();
  if (known.includes(wanted)) return Result.succeed(wanted);
  const matches =
    wanted.length >= MIN_PREFIX
      ? known.filter((id) => id.startsWith(wanted))
      : [];
  const [only] = matches;
  if (matches.length === 1 && only) return Result.succeed(only);
  if (matches.length > 1) {
    return Result.fail(
      new InvalidArgument({
        message: `"${wanted}" matches ${matches.length} notifications.`,
        hint: "Type more of the id.",
      }),
    );
  }
  if (wanted.length >= FULL_ID_LENGTH) return Result.succeed(wanted);
  return Result.fail(
    new InvalidArgument({
      message: `No recent notification matches "${wanted}".`,
      hint: "Run kaneo notification list to see the ids.",
    }),
  );
}