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

    Function stripHydrogens

    • Returns a copy of a molecule with every hydrogen removed, along with the bonds that touched them, reindexing the surviving bonds. Hiding hydrogens makes the carbon skeleton legible at small render sizes.

      A molecule that is entirely hydrogen (H2) is returned unchanged, since stripping it would leave nothing to draw.

      Parameters

      • molecule: Molecule

        The molecule to strip

      Returns Molecule

      A new molecule without hydrogens, or the original if it is all hydrogen

      stripHydrogens({ atoms: [{ symbol: 'C', … }, { symbol: 'H', … }], bonds: [{ from: 0, to: 1, order: 1 }], isPlanar: false });
      // { atoms: [{ symbol: 'C', … }], bonds: [], isPlanar: false }
      export function stripHydrogens(molecule: Molecule): Molecule {
      const keptIndices = new Map<number, number>();
      const atoms: MoleculeAtom[] = [];
      for (const [index, atom] of molecule.atoms.entries()) {
      if (atom.symbol === 'H') continue;
      keptIndices.set(index, atoms.length);
      atoms.push(atom);
      }
      if (atoms.length === 0) return molecule;

      const bonds: MoleculeBond[] = [];
      for (const bond of molecule.bonds) {
      const from = keptIndices.get(bond.from);
      const to = keptIndices.get(bond.to);
      if (from === undefined || to === undefined) continue;
      bonds.push({ from, to, order: bond.order });
      }

      return { atoms, bonds, isPlanar: molecule.isPlanar };
      }