ChemPal Documentation - v0.0.13-beta.5
    Preparing search index...

    Function getCASByName

    • Gets the CAS of a chemical using the name from the Cactus API.

      Parameters

      • name: string

        The chemical name to lookup

      Returns Promise<Maybe<`${string}-${string}-${string}`>>

      The CAS number of the chemical

      getCASByName("Acetic Acid")
      // Returns '79-11-8'
      getCASByName("Acide chloracetique")
      // Returns '79-11-8'
      getCASByName("adsfasfd")
      // Returns undefined
      export async function getCASByName(name: string): Promise<Maybe<CAS<string>>> {
      try {
      const response = await fetch(
      `https://cactus.nci.nih.gov/chemical/structure/names/${encodeURIComponent(name)}`,
      );
      const data = await response.text();
      if (typeof data === "undefined") return;
      const casList = data.split("\n").find((cas) => isCAS(cas));
      return casList;
      } catch (error) {
      console.error(error);
      return;
      }
      }