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

    Function findMolarity

    • Pulls a molar concentration (molarity) out of free-form product copy — the "1.5M", "0.2M", "1M", or "1.5 mol/L" that suppliers bake into titles and descriptions — and returns it as a normalized string suitable for the product's concentration field. The unit must be a capital "M" or literal "mol/L" (see MOLARITY_REGEX), so a lowercase "m" (milli, e.g. "500ml") is never mistaken for molarity. Returns undefined when no molarity is present.

      Parameters

      • text: string

        Raw text (title or description) that may contain a molarity anywhere within it

      Returns undefined | string

      The molarity as a normalized string (e.g. "1.5 M", "1-2 M"), or undefined

      findMolarity("Potassium Nitrate: EZ-Prep - Makes 150ml of 1.5M Solution") // "1.5 M"
      findMolarity("demo kit with 12% hydrogen peroxide, 0.2M potassium iodate") // "0.2 M"
      findMolarity("Potassium Iodide Solution, 1M, 500mL") // "1 M"
      findMolarity("Sodium chloride, 500 g") // undefined
      export const findMolarity = (text: string): string | undefined => {
      if (!text || typeof text !== 'string') return undefined;
      const match = text.match(MOLARITY_REGEX);
      if (!match) return undefined;
      const [, low, high, unit] = match;
      const value = high ? `${low}-${high}` : low;
      return `${value} ${unit}`;
      };