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

    Function resolveIdentifierNames

    • Resolves a chemical identifier (CAS number or molecular formula) to a ranked list of human-readable chemical-name candidates via PubChem — the compound's Title plus its cleaned popularity-ranked synonyms. Used to let suppliers that can only search by name answer identifier queries: the identifier is converted to names, and a product is matched against whichever candidate scores best (so e.g. "Sodium hexametaphosphate" is reached even when the Title is "Hexasodium hexametaphosphate"). SMILES is handled separately by resolveSmiles in @/helpers/smiles.

      Parameters

      • term: string

        The raw identifier (a CAS number or a molecular formula)

      • termType: "cas" | "formula"

        Which kind of identifier term is

      Returns Promise<undefined | { names: string[]; cas?: string[] }>

      The candidate names (and the CAS, for a CAS query), or undefined when unresolved

      await resolveIdentifierNames("10124-56-8", "cas");
      // { names: ["Hexasodium hexametaphosphate", "Sodium hexametaphosphate", …], cas: ["10124-56-8"] }
      export async function resolveIdentifierNames(
      term: string,
      termType: 'cas' | 'formula',
      ): Promise<{ names: string[]; cas?: string[] } | undefined> {
      const cid = termType === 'formula' ? await getCidByFormula(term) : await getCidByName(term);
      if (cid === undefined) {
      return undefined;
      }
      const [properties, synonyms] = await Promise.all([
      getCompoundProperties(cid),
      getSynonymsByCid(cid),
      ]);
      const names = buildCandidateNames(properties?.title, synonyms);
      if (names.length === 0) {
      return undefined;
      }
      return termType === 'cas' ? { names, cas: [term] } : { names };
      }