From Lightning component controller or helper:
var uri = “URI_Value”;
var enc = encodeURI(uri);//to encode
var dec = decodeURI(enc);// to decode
From Apex: