From b09e39d44b87fa9cf7fb0c32942005451c13bcce Mon Sep 17 00:00:00 2001 From: void-spark <81029971+void-spark@users.noreply.github.com> Date: Thu, 15 Dec 2022 15:22:39 +0100 Subject: [PATCH 1/2] Require Node.js 14 and move to ESM (#11) --- .github/workflows/main.yaml | 5 ----- index.js | 40 ++++++++++++++++++------------------- package.json | 14 ++++++++----- readme.md | 2 +- test.js | 10 +++++----- 5 files changed, 34 insertions(+), 37 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 0063223..4d9f325 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -13,11 +13,6 @@ jobs: - '18' - '16' - '14' - - '12' - - '10' - - '8' - - '6' - - '4' steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 diff --git a/index.js b/index.js index 773ae6a..a327d76 100644 --- a/index.js +++ b/index.js @@ -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 } @@ -18,8 +17,8 @@ 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)); } @@ -27,10 +26,10 @@ function decodeComponents(components, split) { 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) || []; @@ -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; @@ -66,18 +65,17 @@ 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 + '`'); } @@ -85,8 +83,8 @@ module.exports = function (encodedURI) { try { // Try the built in decoder first return decodeURIComponent(encodedURI); - } catch (err) { + } catch { // Fallback to a more advanced decoder return customDecodeURIComponent(encodedURI); } -}; +} diff --git a/package.json b/package.json index 0b1b750..caa4be0 100644 --- a/package.json +++ b/package.json @@ -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" ], @@ -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" } } diff --git a/readme.md b/readme.md index bd71b4b..192dd9d 100644 --- a/readme.md +++ b/readme.md @@ -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'); //=> '%' diff --git a/test.js b/test.js index 0424493..7a03897 100644 --- a/test.js +++ b/test.js @@ -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', @@ -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', @@ -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) { @@ -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)) { From b3710c42d27fb2ff93a1349760bb07c3846662fe Mon Sep 17 00:00:00 2001 From: Sam Verschueren Date: Thu, 15 Dec 2022 15:26:32 +0100 Subject: [PATCH 2/2] 0.4.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index caa4be0..2de867f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "decode-uri-component", - "version": "0.3.0", + "version": "0.4.0", "description": "A better decodeURIComponent", "license": "MIT", "repository": "SamVerschueren/decode-uri-component",