2 var token = '%[a-f0-9]{2}';
3 var singleMatcher = new RegExp(token, 'gi');
4 var multiMatcher = new RegExp('(' + token + ')+', 'gi');
6 function decodeComponents(components, split) {
8 // Try to decode the entire string first
9 return decodeURIComponent(components.join(''));
14 if (components.length === 1) {
20 // Split the array in 2 parts
21 var left = components.slice(0, split);
22 var right = components.slice(split);
24 return Array.prototype.concat.call([], decodeComponents(left), decodeComponents(right));
27 function decode(input) {
29 return decodeURIComponent(input);
31 var tokens = input.match(singleMatcher);
33 for (var i = 1; i < tokens.length; i++) {
34 input = decodeComponents(tokens, i).join('');
36 tokens = input.match(singleMatcher);
43 function customDecodeURIComponent(input) {
44 // Keep track of all the replacements and prefill the map with the `BOM`
46 '%FE%FF': '\uFFFD\uFFFD',
47 '%FF%FE': '\uFFFD\uFFFD'
50 var match = multiMatcher.exec(input);
53 // Decode as big chunks as possible
54 replaceMap[match[0]] = decodeURIComponent(match[0]);
56 var result = decode(match[0]);
58 if (result !== match[0]) {
59 replaceMap[match[0]] = result;
63 match = multiMatcher.exec(input);
66 // Add `%C2` at the end of the map to make sure it does not replace the combinator before everything else
67 replaceMap['%C2'] = '\uFFFD';
69 var entries = Object.keys(replaceMap);
71 for (var i = 0; i < entries.length; i++) {
72 // Replace all decoded components
74 input = input.replace(new RegExp(key, 'g'), replaceMap[key]);
80 module.exports = function (encodedURI) {
81 if (typeof encodedURI !== 'string') {
82 throw new TypeError('Expected `encodedURI` to be of type `string`, got `' + typeof encodedURI + '`');
86 encodedURI = encodedURI.replace(/\+/g, ' ');
88 // Try the built in decoder first
89 return decodeURIComponent(encodedURI);
91 // Fallback to a more advanced decoder
92 return customDecodeURIComponent(encodedURI);