{"version":3,"file":"getWholeChar.mjs","names":[],"sources":["../../../src/utils/getWholeChar.ts"],"sourcesContent":["/**\n * Returns the whole character at index `i`, so a surrogate pair comes back as one string.\n *\n * @param str - String to read from.\n * @param i - Character index.\n * @returns The character, or `''` when `i` is out of range or points at the low half of a pair.\n * @throws A string (not an `Error`) when a surrogate half has no matching partner.\n * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt#getting_whole_characters\n */\nexport const getWholeChar = (str: string, i: number) => {\n  const code = str.charCodeAt(i);\n\n  if (Number.isNaN(code)) return '';\n\n  if (code < 0xd800 || code > 0xdfff) return str.charAt(i);\n\n  if (0xd800 <= code && code <= 0xdbff) {\n    if (str.length <= i + 1) {\n      throw 'High surrogate without following low surrogate';\n    }\n\n    const next = str.charCodeAt(i + 1);\n\n    if (0xdc00 > next || next > 0xdfff) {\n      throw 'High surrogate without following low surrogate';\n    }\n\n    return str.charAt(i) + str.charAt(i + 1);\n  }\n\n  if (i === 0) {\n    throw 'Low surrogate without preceding high surrogate';\n  }\n\n  const prev = str.charCodeAt(i - 1);\n\n  if (0xd800 > prev || prev > 0xdbff) {\n    throw 'Low surrogate without preceding high surrogate';\n  }\n\n  return '';\n};\n"],"mappings":";;;;;;;;;;AASA,IAAa,gBAAgB,KAAa,MAAc;CACtD,MAAM,OAAO,IAAI,WAAW,CAAC;CAE7B,IAAI,OAAO,MAAM,IAAI,GAAG,OAAO;CAE/B,IAAI,OAAO,SAAU,OAAO,OAAQ,OAAO,IAAI,OAAO,CAAC;CAEvD,IAAI,SAAU,QAAQ,QAAQ,OAAQ;EACpC,IAAI,IAAI,UAAU,IAAI,GACpB,MAAM;EAGR,MAAM,OAAO,IAAI,WAAW,IAAI,CAAC;EAEjC,IAAI,QAAS,QAAQ,OAAO,OAC1B,MAAM;EAGR,OAAO,IAAI,OAAO,CAAC,IAAI,IAAI,OAAO,IAAI,CAAC;CACzC;CAEA,IAAI,MAAM,GACR,MAAM;CAGR,MAAM,OAAO,IAAI,WAAW,IAAI,CAAC;CAEjC,IAAI,QAAS,QAAQ,OAAO,OAC1B,MAAM;CAGR,OAAO;AACT"}