0

I would like to bold some character in a javascript confirm popup. As you can't use html/css there, and extending a bit the solution to underline with the dedicated unicode character, I thought I might simply replace each digit with its bolded unicode counterpart, and as they are five digit long (ranging from 1D7CE to , 1D7D7), I came with the following code:

function bold(number) {
  return number.
    replace(/0/, String.fromCharCode(0x1D7CE)).
    replace(/1/, String.fromCharCode(0x1D7CF)).
    replace(/2/, String.fromCharCode(0x1D7D0)).
    replace(/3/, String.fromCharCode(0x1D7D1)).
    replace(/4/, String.fromCharCode(0x1D7D2)).
    replace(/5/, String.fromCharCode(0x1D7D3)).
    replace(/6/, String.fromCharCode(0x1D7D4)).
    replace(/7/, String.fromCharCode(0x1D7D5)).
    replace(/8/, String.fromCharCode(0x1D7D6)).
    replace(/9/, String.fromCharCode(0x1D7D7));
}

But I must misunderstand something, as for example 1 is replaced with (D7CE).

What might be a working solution?

psychoslave
  • 2,783
  • 3
  • 27
  • 44

1 Answers1

7

Try this simplified implementation:

function bold(number) {
  return number.replace(/\d/g, (c) => String.fromCodePoint(0x1D79E + c.charCodeAt(0)));
}

console.log(bold('abc 0123456789 def'));

This uses String.fromCodePoint() which supports the full range of unicode, unlike String.fromCharCode().

This simplification takes advantage of the fact that numbers and bolded numbers both take up a consecutive run of 10 unicode points, separated by a distance of 0x1D79E.

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
  • How do I use this to make any character bold? – kittu Jun 20 '18 at 20:50
  • @kittu you would need to locate other ranges of bold characters in the unicode pages. Basically, every type of character that has a normal and bold equivalent in unicode, you'd need to implement separately. – Patrick Roberts Jun 21 '18 at 00:22