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

    Function superscript

    • Converts regular numbers in a string to superscript Unicode characters.

      Parameters

      • str: string

        The string containing numbers to convert to superscripts

      Returns string

      The string with numbers converted to superscript characters

      superscript("10^2") // Returns "10²"
      superscript("2^3") // Returns "2³"
      export const superscript = (str: string) => {
      // Superscript digits aren't contiguous: ¹²³ live in the Latin-1 block (U+00B9/B2/B3), while
      // ⁰ and ⁴–⁹ are contiguous from U+2070. Special-case the three, compute the rest.
      return str.replace(/[0-9]/g, (d) => {
      const n = Number(d);
      if (n === 1) return '¹';
      if (n === 2) return '²';
      if (n === 3) return '³';
      return String.fromCodePoint(0x2070 + n);
      });
      };