decode-uri-component
Advanced tools
Comparing version 0.3.0 to 0.4.0
40
index.js
@@ -1,5 +0,4 @@ | ||
'use strict'; | ||
var token = '%[a-f0-9]{2}'; | ||
var singleMatcher = new RegExp('(' + token + ')|([^%]+?)', 'gi'); | ||
var multiMatcher = new RegExp('(' + token + ')+', 'gi'); | ||
const token = '%[a-f0-9]{2}'; | ||
const singleMatcher = new RegExp('(' + token + ')|([^%]+?)', 'gi'); | ||
const multiMatcher = new RegExp('(' + token + ')+', 'gi'); | ||
@@ -10,3 +9,3 @@ function decodeComponents(components, split) { | ||
return [decodeURIComponent(components.join(''))]; | ||
} catch (err) { | ||
} catch { | ||
// Do nothing | ||
@@ -22,4 +21,4 @@ } | ||
// Split the array in 2 parts | ||
var left = components.slice(0, split); | ||
var right = components.slice(split); | ||
const left = components.slice(0, split); | ||
const right = components.slice(split); | ||
@@ -32,6 +31,6 @@ return Array.prototype.concat.call([], decodeComponents(left), decodeComponents(right)); | ||
return decodeURIComponent(input); | ||
} catch (err) { | ||
var tokens = input.match(singleMatcher) || []; | ||
} catch { | ||
let tokens = input.match(singleMatcher) || []; | ||
for (var i = 1; i < tokens.length; i++) { | ||
for (let i = 1; i < tokens.length; i++) { | ||
input = decodeComponents(tokens, i).join(''); | ||
@@ -48,8 +47,8 @@ | ||
// Keep track of all the replacements and prefill the map with the `BOM` | ||
var replaceMap = { | ||
const replaceMap = { | ||
'%FE%FF': '\uFFFD\uFFFD', | ||
'%FF%FE': '\uFFFD\uFFFD' | ||
'%FF%FE': '\uFFFD\uFFFD', | ||
}; | ||
var match = multiMatcher.exec(input); | ||
let match = multiMatcher.exec(input); | ||
while (match) { | ||
@@ -59,4 +58,4 @@ try { | ||
replaceMap[match[0]] = decodeURIComponent(match[0]); | ||
} catch (err) { | ||
var result = decode(match[0]); | ||
} catch { | ||
const result = decode(match[0]); | ||
@@ -74,7 +73,6 @@ if (result !== match[0]) { | ||
var entries = Object.keys(replaceMap); | ||
const entries = Object.keys(replaceMap); | ||
for (var i = 0; i < entries.length; i++) { | ||
for (const key of entries) { | ||
// Replace all decoded components | ||
var key = entries[i]; | ||
input = input.replace(new RegExp(key, 'g'), replaceMap[key]); | ||
@@ -86,3 +84,3 @@ } | ||
module.exports = function (encodedURI) { | ||
export default function decodeUriComponent(encodedURI) { | ||
if (typeof encodedURI !== 'string') { | ||
@@ -95,6 +93,6 @@ throw new TypeError('Expected `encodedURI` to be of type `string`, got `' + typeof encodedURI + '`'); | ||
return decodeURIComponent(encodedURI); | ||
} catch (err) { | ||
} catch { | ||
// Fallback to a more advanced decoder | ||
return customDecodeURIComponent(encodedURI); | ||
} | ||
}; | ||
} |
{ | ||
"name": "decode-uri-component", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "A better decodeURIComponent", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=0.10" | ||
"node": ">=14.16" | ||
}, | ||
@@ -20,2 +20,6 @@ "scripts": { | ||
}, | ||
"type": "module", | ||
"exports": { | ||
"default": "./index.js" | ||
}, | ||
"files": [ | ||
@@ -34,7 +38,7 @@ "index.js" | ||
"devDependencies": { | ||
"ava": "^0.17.0", | ||
"coveralls": "^2.13.1", | ||
"nyc": "^10.3.2", | ||
"xo": "^0.16.0" | ||
"ava": "^5.1.0", | ||
"coveralls": "^3.1.1", | ||
"nyc": "^15.1.0", | ||
"xo": "^0.53.1" | ||
} | ||
} |
@@ -25,3 +25,3 @@ # decode-uri-component | ||
```js | ||
const decodeUriComponent = require('decode-uri-component'); | ||
import decodeUriComponent from 'decode-uri-component'; | ||
@@ -28,0 +28,0 @@ decodeUriComponent('%25'); |
6032
Yes
71