0

MDN is suggesting to use encodeURI and encodeURIComponent instead of depreciated escape method.But both of these methods are not giving same output what escape used to give.

We have a scenario where JavaScript is receiving byte array from WCF web service which was encoded using UTF8. The data has special chars as well which produces 3 bytes / each character when we convert tp UTF-8 inside service. The below function works perfect for us but would like to avoid the use of depreciated escape().

function decode_utf8(bytes) {
  return decodeURIComponent(escape(bytes));
}

Another reason behind avoiding escape is its not supported by TypeScript by native and we would like to go with TS.

Another way is to code the conversion routine to avoid calling escape.

Expecting suggestions on best practice. Without any changes to WCF service as its legacy and used by other systems :)

Community
  • 1
  • 1
Joy George Kunjikkuru
  • 1,495
  • 13
  • 27
  • 1
    So what is the question? What are the equivalents of `escape`? You have already listed them. – hindmost Dec 10 '15 at 16:26
  • Best practice is to not have a server that does non-standard encoding. If you want to avoid using `escape()`, you could implement your own custom decoder that is expecting the funky encoding coming from the server. – jfriend00 Dec 10 '15 at 16:26
  • @hindmost, I am investigating is there any better way instead of writing custom routine for UTF-8 conversion. @ jfriend00, yes it seems custom code is the way as its difficult to change service and TS not supporting escape. Looking for an intermediate proxy service too. So that proxy can hide the bytearray and unicode complexity from browser. – Joy George Kunjikkuru Dec 10 '15 at 16:36

1 Answers1

0

If you want to use escape. Just declare it in your as an extension to lib.d.ts in your globals.d.ts (more docs):

declare var escape:any;

And then TypeScript will not complain.

Definitely consider upgrading your code though.

basarat
  • 261,912
  • 58
  • 460
  • 511