Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

1.3 KB
Raw Blame
export function filterDatabaseOptions(options: string[], query: string, displayName: (option: string) => string = (option) => option): string[] {
  const normalizedQuery = query.trim().toLowerCase();
  if (!normalizedQuery) return options;

  interface RankedOption {
    option: string;
    tier: number;
    index: number;
  }

  const isWordBoundaryMatch = (text: string): boolean => {
    const words = text.split(/[\s_\-(),/]+/);
    return words.some((word) => word.startsWith(normalizedQuery));
  };

  const matches: RankedOption[] = [];

  for (let index = 0; index < options.length; index++) {
    const option = options[index];
    const raw = option.toLowerCase();
    const label = displayName(option).toLowerCase();

    let tier = -1;
    if (raw === normalizedQuery || label === normalizedQuery) {
      tier = 0;
    } else if (raw.startsWith(normalizedQuery) || label.startsWith(normalizedQuery)) {
      tier = 1;
    } else if (isWordBoundaryMatch(raw) || isWordBoundaryMatch(label)) {
      tier = 2;
    } else if (raw.includes(normalizedQuery) || label.includes(normalizedQuery)) {
      tier = 3;
    }

    if (tier >= 0) {
      matches.push({ option, tier, index });
    }
  }

  matches.sort((a, b) => {
    if (a.tier !== b.tier) return a.tier - b.tier;
    return a.index - b.index;
  });

  return matches.map((m) => m.option);
}