1

I'm invoking Telegram Bot API from a Node.js application. I need to send some Unicode characters to the API, that only accepts Ruby format (\u{1F680} outputs ). I store all Unicode characters in this format in a file that is required by the one actually sending the message to the API.

Node.js is ok with this, it's working fine. However, when I run Mocha tests on the same code, it results in an Unexpected token ILLEGAL error, it doesn't like the Ruby Unicode format. The test itself isn't even using this Unicode character, the compilation is what fails.

emojis.rocket = '\u{1F680}';
                ^^^
SyntaxError: Unexpected token ILLEGAL
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/path/to/file/values_es.js:1:76)
  // more error lines... 
npm ERR! Test failed.  See above for more details.

If I change it to the JavaScript Unicode format (\u1F680), Mocha and Node.js are OK with it, but the API doesn't get it as the appropriate character.

I've tried the following:

  • Store in JavaScript format \u1F680 and then adding the {} in runtime, but it outputs a different character (Ὠ0), corresponding to the first four HEX values (\u1F68) and the extra 0.
  • Store in JavaScript format \u1F680 and adding encode Ruby function code.encode('UTF-8') just in case Telegram Bot API executes Ruby code. Not working either, it prints \u1F680.encode('UTF-8').

Is there any way in which I can make Mocha accept Ruby-ish (\u{1F680}) format?

Vega
  • 27,856
  • 27
  • 95
  • 103
iria
  • 11
  • 1

1 Answers1

0

Use the RegExp constructor to pass it through the mocha test:

RegExp('\u{1F680}')

then use exec and toString to convert it to a string:

RegExp('\u{1F680}').exec('\u{1F680}').toString()

References

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265