Skip to content

Commit b09e39d

Browse files
authored
Require Node.js 14 and move to ESM (#11)
1 parent 7268ef6 commit b09e39d

File tree

5 files changed

+34
-37
lines changed

5 files changed

+34
-37
lines changed

.github/workflows/main.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ jobs:
1313
- '18'
1414
- '16'
1515
- '14'
16-
- '12'
17-
- '10'
18-
- '8'
19-
- '6'
20-
- '4'
2116
steps:
2217
- uses: actions/checkout@v3
2318
- uses: actions/setup-node@v3

index.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
'use strict';
2-
var token = '%[a-f0-9]{2}';
3-
var singleMatcher = new RegExp('(' + token + ')|([^%]+?)', 'gi');
4-
var multiMatcher = new RegExp('(' + token + ')+', 'gi');
1+
const token = '%[a-f0-9]{2}';
2+
const singleMatcher = new RegExp('(' + token + ')|([^%]+?)', 'gi');
3+
const multiMatcher = new RegExp('(' + token + ')+', 'gi');
54

65
function decodeComponents(components, split) {
76
try {
87
// Try to decode the entire string first
98
return [decodeURIComponent(components.join(''))];
10-
} catch (err) {
9+
} catch {
1110
// Do nothing
1211
}
1312

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

2019
// Split the array in 2 parts
21-
var left = components.slice(0, split);
22-
var right = components.slice(split);
20+
const left = components.slice(0, split);
21+
const right = components.slice(split);
2322

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

2726
function decode(input) {
2827
try {
2928
return decodeURIComponent(input);
30-
} catch (err) {
31-
var tokens = input.match(singleMatcher) || [];
29+
} catch {
30+
let tokens = input.match(singleMatcher) || [];
3231

33-
for (var i = 1; i < tokens.length; i++) {
32+
for (let i = 1; i < tokens.length; i++) {
3433
input = decodeComponents(tokens, i).join('');
3534

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

4342
function customDecodeURIComponent(input) {
4443
// Keep track of all the replacements and prefill the map with the `BOM`
45-
var replaceMap = {
44+
const replaceMap = {
4645
'%FE%FF': '\uFFFD\uFFFD',
47-
'%FF%FE': '\uFFFD\uFFFD'
46+
'%FF%FE': '\uFFFD\uFFFD',
4847
};
4948

50-
var match = multiMatcher.exec(input);
49+
let match = multiMatcher.exec(input);
5150
while (match) {
5251
try {
5352
// Decode as big chunks as possible
5453
replaceMap[match[0]] = decodeURIComponent(match[0]);
55-
} catch (err) {
56-
var result = decode(match[0]);
54+
} catch {
55+
const result = decode(match[0]);
5756

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

69-
var entries = Object.keys(replaceMap);
68+
const entries = Object.keys(replaceMap);
7069

71-
for (var i = 0; i < entries.length; i++) {
70+
for (const key of entries) {
7271
// Replace all decoded components
73-
var key = entries[i];
7472
input = input.replace(new RegExp(key, 'g'), replaceMap[key]);
7573
}
7674

7775
return input;
7876
}
7977

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

8583
try {
8684
// Try the built in decoder first
8785
return decodeURIComponent(encodedURI);
88-
} catch (err) {
86+
} catch {
8987
// Fallback to a more advanced decoder
9088
return customDecodeURIComponent(encodedURI);
9189
}
92-
};
90+
}

package.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010
"url": "github.com/SamVerschueren"
1111
},
1212
"engines": {
13-
"node": ">=0.10"
13+
"node": ">=14.16"
1414
},
1515
"scripts": {
1616
"test": "xo && nyc ava",
1717
"coveralls": "nyc report --reporter=text-lcov | coveralls"
1818
},
19+
"type": "module",
20+
"exports": {
21+
"default": "./index.js"
22+
},
1923
"files": [
2024
"index.js"
2125
],
@@ -29,9 +33,9 @@
2933
"url"
3034
],
3135
"devDependencies": {
32-
"ava": "^0.17.0",
33-
"coveralls": "^2.13.1",
34-
"nyc": "^10.3.2",
35-
"xo": "^0.16.0"
36+
"ava": "^5.1.0",
37+
"coveralls": "^3.1.1",
38+
"nyc": "^15.1.0",
39+
"xo": "^0.53.1"
3640
}
3741
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $ npm install --save decode-uri-component
2222
## Usage
2323

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

2727
decodeUriComponent('%25');
2828
//=> '%'

test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import test from 'ava';
2-
import m from './';
2+
import m from './index.js';
33

44
const tests = {
5-
'test': 'test',
5+
test: 'test',
66
'a+b': 'a+b',
77
'a+b+c+d': 'a+b+c+d',
88
'=a': '=a',
@@ -21,7 +21,7 @@ const tests = {
2121
'%ab': '%ab',
2222
'%ab%ab%ab': '%ab%ab%ab',
2323
'%61+%4d%4D': 'a+MM',
24-
'\uFEFFtest': '\uFEFFtest',
24+
'\uFEFFtest2': '\uFEFFtest2',
2525
'\uFEFF': '\uFEFF',
2626
'%EF%BB%BFtest': '\uFEFFtest',
2727
'%EF%BB%BF': '\uFEFF',
@@ -35,7 +35,7 @@ const tests = {
3535
'%%C2%B5%': '%µ%',
3636
'%ea%ba%5a%ba': '%ea%baZ%ba',
3737
'%C3%5A%A5': '%C3Z%A5',
38-
'%C3%5A%A5%AB': '%C3Z%A5%AB'
38+
'%C3%5A%A5%AB': '%C3Z%A5%AB',
3939
};
4040

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

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

5151
for (const input of Object.keys(tests)) {

0 commit comments

Comments
 (0)