Javascript: read from url in unicode-escape format

I am using node-fetch to read data from URL. The return result is perfect except it is different from my preferred result. For some special characters like í or this ř, I want it instead to be in the format as following: u00ed and u0159. Is it possible to achieve something like that without looking up for the character and replacing it manually?

Here is my code:

const fetch = require("node-fetch");
var result = {}
fetch(url)
.then(res => res.json())
.then(json => result = json);

My json result:

{"title":"Adopce zvířat"}

In stead of the result above, I want:

{"title":"Adopce zv\u00ed\u0159at"}

It must be strange but for some reason I want it that way.

Answer

You could use the jsesc package:

const jsesc = require('jsesc');

jsesc('Ich ♥ Bücher'); // → 'Ich \u2665 B\xFCcher'

jsesc('foo 𝌆 bar'); // → 'foo \uD834\uDF06 bar' 

From the Documentation on Github