mirror of
https://github.com/Kong/insomnia
synced 2024-11-07 22:30:15 +00:00
Now convert escaped unicode
This commit is contained in:
parent
6afe3ace55
commit
f6f148185d
@ -8,6 +8,16 @@
|
||||
* @returns {string}
|
||||
*/
|
||||
export function prettifyJson (json, indentChars) {
|
||||
|
||||
// Convert the unicode. To correctly mimic JSON.stringify(JSON.parse(json), null, indentChars)
|
||||
// we need to convert all escaped unicode characters to proper unicode characters.
|
||||
try {
|
||||
json = _convertUnicode(json);
|
||||
} catch (err) {
|
||||
// Just in case (should never happen)
|
||||
console.warn('Prettify failed to handle unicode', err);
|
||||
}
|
||||
|
||||
let i = 0;
|
||||
let il = json.length;
|
||||
let tab = (typeof indentChars !== 'undefined') ? indentChars : ' ';
|
||||
@ -18,7 +28,7 @@ export function prettifyJson (json, indentChars) {
|
||||
let previousChar = null;
|
||||
let nextChar = null;
|
||||
|
||||
for (;i < il; i += 1) {
|
||||
for (; i < il; i += 1) {
|
||||
currentChar = json.charAt(i);
|
||||
previousChar = json.charAt(i - 1);
|
||||
nextChar = json.charAt(i + 1);
|
||||
@ -92,6 +102,40 @@ export function prettifyJson (json, indentChars) {
|
||||
return newJson;
|
||||
}
|
||||
|
||||
function _repeatString(s, count) {
|
||||
function _repeatString (s, count) {
|
||||
return new Array(count + 1).join(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert escaped unicode characters to real characters. Any JSON parser will do this by
|
||||
* default. This is really fast too. Around 25ms for ~2MB of data with LOTS of unicode.
|
||||
*
|
||||
* @param originalStr
|
||||
* @returns {string}
|
||||
* @private
|
||||
*/
|
||||
function _convertUnicode (originalStr) {
|
||||
let m;
|
||||
let c;
|
||||
let lastI = 0;
|
||||
|
||||
// Matches \u####
|
||||
const unicodeRegex = /\\u([0-9a-fA-F]{4})/g;
|
||||
|
||||
let convertedStr = '';
|
||||
while (m = unicodeRegex.exec(originalStr)) {
|
||||
try {
|
||||
c = String.fromCharCode(parseInt(m[1], 16));
|
||||
convertedStr += originalStr.slice(lastI, m.index) + c;
|
||||
lastI = m.index + 6;
|
||||
} catch (err) {
|
||||
// Some reason we couldn't convert a char. Should never actually happen
|
||||
console.warn('Failed to convert unicode char', m[1], err);
|
||||
}
|
||||
}
|
||||
|
||||
// Finally, add the rest of the string to the end.
|
||||
convertedStr += originalStr.slice(lastI, originalStr.length);
|
||||
|
||||
return convertedStr;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user