Skip to content

Commit

Permalink
Require Node.js 14 and move to ESM (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
void-spark authored Dec 15, 2022
1 parent 7268ef6 commit b09e39d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 37 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ jobs:
- '18'
- '16'
- '14'
- '12'
- '10'
- '8'
- '6'
- '4'
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
Expand Down
40 changes: 19 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
'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');

function decodeComponents(components, split) {
try {
// Try to decode the entire string first
return [decodeURIComponent(components.join(''))];
} catch (err) {
} catch {
// Do nothing
}

Expand All @@ -18,19 +17,19 @@ function decodeComponents(components, split) {
split = split || 1;

// 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);

return Array.prototype.concat.call([], decodeComponents(left), decodeComponents(right));
}

function decode(input) {
try {
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('');

tokens = input.match(singleMatcher) || [];
Expand All @@ -42,18 +41,18 @@ function decode(input) {

function customDecodeURIComponent(input) {
// 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) {
try {
// Decode as big chunks as possible
replaceMap[match[0]] = decodeURIComponent(match[0]);
} catch (err) {
var result = decode(match[0]);
} catch {
const result = decode(match[0]);

if (result !== match[0]) {
replaceMap[match[0]] = result;
Expand All @@ -66,27 +65,26 @@ function customDecodeURIComponent(input) {
// Add `%C2` at the end of the map to make sure it does not replace the combinator before everything else
replaceMap['%C2'] = '\uFFFD';

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]);
}

return input;
}

module.exports = function (encodedURI) {
export default function decodeUriComponent(encodedURI) {
if (typeof encodedURI !== 'string') {
throw new TypeError('Expected `encodedURI` to be of type `string`, got `' + typeof encodedURI + '`');
}

try {
// Try the built in decoder first
return decodeURIComponent(encodedURI);
} catch (err) {
} catch {
// Fallback to a more advanced decoder
return customDecodeURIComponent(encodedURI);
}
};
}
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
"url": "github.com/SamVerschueren"
},
"engines": {
"node": ">=0.10"
"node": ">=14.16"
},
"scripts": {
"test": "xo && nyc ava",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"type": "module",
"exports": {
"default": "./index.js"
},
"files": [
"index.js"
],
Expand All @@ -29,9 +33,9 @@
"url"
],
"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"
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $ npm install --save decode-uri-component
## Usage

```js
const decodeUriComponent = require('decode-uri-component');
import decodeUriComponent from 'decode-uri-component';

decodeUriComponent('%25');
//=> '%'
Expand Down
10 changes: 5 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import test from 'ava';
import m from './';
import m from './index.js';

const tests = {
'test': 'test',
test: 'test',
'a+b': 'a+b',
'a+b+c+d': 'a+b+c+d',
'=a': '=a',
Expand All @@ -21,7 +21,7 @@ const tests = {
'%ab': '%ab',
'%ab%ab%ab': '%ab%ab%ab',
'%61+%4d%4D': 'a+MM',
'\uFEFFtest': '\uFEFFtest',
'\uFEFFtest2': '\uFEFFtest2',
'\uFEFF': '\uFEFF',
'%EF%BB%BFtest': '\uFEFFtest',
'%EF%BB%BF': '\uFEFF',
Expand All @@ -35,7 +35,7 @@ const tests = {
'%%C2%B5%': '%µ%',
'%ea%ba%5a%ba': '%ea%baZ%ba',
'%C3%5A%A5': '%C3Z%A5',
'%C3%5A%A5%AB': '%C3Z%A5%AB'
'%C3%5A%A5%AB': '%C3Z%A5%AB',
};

function macro(t, input, expected) {
Expand All @@ -45,7 +45,7 @@ function macro(t, input, expected) {
macro.title = (providedTitle, input, expected) => `${input}${expected}`;

test('type error', t => {
t.throws(() => m(5), 'Expected `encodedURI` to be of type `string`, got `number`');
t.throws(() => m(5), {message: 'Expected `encodedURI` to be of type `string`, got `number`'});
});

for (const input of Object.keys(tests)) {
Expand Down

1 comment on commit b09e39d

@sandritis2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.github/workflows/main.yaml

Please sign in to comment.