JS coding style example. codepoint to utf16 encoding

By Xah Lee. Date: .

supreme js fp functional programing

supreme js fp 2026-01-29 2cf2d
supreme js fp 2026-01-29 2cf2d
/* xah_codepoint_to_utf16_hexStr(zcodepoint) return a string of hexadecimal that's the UTF-16 encoding of the char of codepoint zcodepoint (integer). */

// before

const xah_codepoint_to_utf16_hexStr = (zcodepoint) => {
 const xCharStr = String.fromCodePoint(zcodepoint);
 if (zcodepoint < 2 ** 16) {
  return xCharStr.charCodeAt(0).toString(16).toUpperCase();
 } else {
  const xout = [];
  for (let i = 0; i < xCharStr.length; i++) {
   xout.push(xCharStr.charCodeAt(i).toString(16).toUpperCase());
  }
  return xout.join(" ");
 }
};

// after

const xah_codepoint_to_utf16_hexStr = (zcodepoint) =>
 ((xstr) => (Array.from(Array(xstr.length).keys(), (x) => (xstr.charCodeAt(x).toString(16).toUpperCase())).join(" ")))(String.fromCodePoint(zcodepoint));

actually the 2 versions are not semantically the same. but the latter is a improvement anyhow.

it is shown here to as code study example.