ChemPal Documentation - v1.6.0
    Preparing search index...

    Function resolveQueryForSearch

    • Top-level entry that turns any search query into a term the existing supplier search can consume. If the query is a structure (via smiles:/inchikey: prefix or the looksLikeSmiles heuristic) it is resolved to the best searchable identifier (name preferred, CAS fallback) and returned alongside the resolved structure metadata. Non-structure queries pass through unchanged. Built for the future search wiring; currently invoked manually for testing.

      Parameters

      • query: string

        The raw search query

      Returns Promise<{ searchTerm: string; structure?: ResolvedStructure }>

      searchTerm (always safe to pass to the supplier search) and optional structure

      await resolveQueryForSearch("CCO")
      // { searchTerm: "ethanol", structure: { name: "ethanol", cas: ["64-17-5"], ... } }
      await resolveQueryForSearch("sulfuric acid") // { searchTerm: "sulfuric acid" }
      export async function resolveQueryForSearch(
      query: string,
      ): Promise<{ searchTerm: string; structure?: ResolvedStructure }> {
      const { mode, value } = parseStructurePrefix(query);

      if (mode === 'inchikey') {
      const name = await nameFromInchikey(value);
      return name
      ? { searchTerm: name, structure: { name, inchikey: value, source: 'pubchem-inchikey' } }
      : { searchTerm: value };
      }

      const treatAsSmiles = mode === 'smiles' || (mode === 'auto' && looksLikeSmiles(value));
      if (!treatAsSmiles) {
      return { searchTerm: value };
      }

      const structure = await resolveSmiles(value);
      if (!structure) {
      return { searchTerm: value };
      }
      return { searchTerm: structure.name ?? structure.cas?.[0] ?? value, structure };
      }