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

    Function extractGhs

    • Parses a PUG-View GHS Classification response into a PubChemGhs. Multiple sources report the same fields; pictograms and statements are de-duplicated and the first signal word wins.

      Parameters

      • data: unknown

        The parsed JSON response body

      Returns undefined | PubChemGhs

      The classification, or undefined when it holds no pictograms, signal, or statements

      export function extractGhs(data: unknown): PubChemGhs | undefined {
      const pictograms = new Map<string, { code: string; label: string; url: string }>();
      const statements = new Set<string>();
      let signal: string | undefined;

      for (const entry of collectInformation(data)) {
      const name = Reflect.get(entry, 'Name');
      for (const { text, markup } of markupItems(entry)) {
      if (name === 'Pictogram(s)') {
      for (const mark of markup) {
      const url = Reflect.get(mark, 'URL');
      const label = Reflect.get(mark, 'Extra');
      const code = typeof url === 'string' ? /(GHS\d{2})\.svg$/.exec(url)?.[1] : undefined;
      if (code !== undefined && typeof url === 'string') {
      pictograms.set(code, { code, label: typeof label === 'string' ? label : code, url });
      }
      }
      } else if (name === 'Signal') {
      signal ??= text.trim() || undefined;
      } else if (name === 'GHS Hazard Statements') {
      const statement = text.replace(/\s*\[.*\]\s*$/, '').trim();
      if (statement !== '') statements.add(statement);
      }
      }
      }

      if (pictograms.size === 0 && signal === undefined && statements.size === 0) return undefined;
      return {
      pictograms: [...pictograms.values()].sort((a, b) => a.code.localeCompare(b.code)),
      signal,
      hazardStatements: [...statements],
      };
      }