Skip to content

Commit

Permalink
add support for older node versions
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Jun 28, 2017
1 parent 3f86a3e commit bb60483
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
language: node_js
node_js:
- '8'
- '6'
- '4'
- '0.12'
- '0.10'
after_script:
- npm run coveralls
43 changes: 23 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const token = '%[a-f0-9]{2}';
const singleMatcher = new RegExp(token, 'gi');
const multiMatcher = new RegExp(`(${token})+`, 'gi');
var token = '%[a-f0-9]{2}';
var singleMatcher = new RegExp(token, 'gi');
var multiMatcher = new RegExp('(' + token + ')+', 'gi');

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

// Split the array in 2 parts
const left = components.slice(0, split);
const right = components.slice(split);
var left = components.slice(0, split);
var right = components.slice(split);

return Array.prototype.concat.call([], decodeComponents(left), decodeComponents(right));
}
Expand All @@ -28,9 +28,9 @@ function decode(input) {
try {
return decodeURIComponent(input);
} catch (err) {
let tokens = input.match(singleMatcher);
var tokens = input.match(singleMatcher);

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

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

function customDecodeURIComponent(input) {
// Keep track of all the replacements and prefill the map with the `BOM`
const replaceMap = new Map([
['%FE%FF', '\uFFFD\uFFFD'],
['%FF%FE', '\uFFFD\uFFFD']
]);
var replaceMap = {
'%FE%FF': '\uFFFD\uFFFD',
'%FF%FE': '\uFFFD\uFFFD'
};

let match = multiMatcher.exec(input);
var match = multiMatcher.exec(input);
while (match) {
try {
// Decode as big chunks as possible
replaceMap.set(match[0], decodeURIComponent(match[0]));
replaceMap[match[0]] = decodeURIComponent(match[0]);
} catch (err) {
const result = decode(match[0]);
var result = decode(match[0]);

if (result !== match[0]) {
replaceMap.set(match[0], result);
replaceMap[match[0]] = result;
}
}

match = multiMatcher.exec(input);
}

// Add `%C2` at the end of the map to make sure it does not replace the combinator before everything else
replaceMap.set('%C2', '\uFFFD');
replaceMap['%C2'] = '\uFFFD';

for (const entry of replaceMap.entries()) {
var entries = Object.keys(replaceMap);

for (var i = 0; i < entries.length; i++) {
// Replace all decoded components
input = input.replace(new RegExp(entry[0], 'g'), entry[1]);
var key = entries[i];
input = input.replace(new RegExp(key, 'g'), replaceMap[key]);
}

return input;
}

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

try {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "github.com/SamVerschueren"
},
"engines": {
"node": ">=4"
"node": ">=0.10"
},
"scripts": {
"test": "xo && nyc ava",
Expand All @@ -29,9 +29,9 @@
"url"
],
"devDependencies": {
"ava": "*",
"ava": "^0.17.0",
"coveralls": "^2.13.1",
"nyc": "^10.3.2",
"xo": "*"
"xo": "^0.16.0"
}
}
4 changes: 2 additions & 2 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 './';

const tests = {
test: 'test',
'test': 'test',
'a+b': 'a b',
'a+b+c+d': 'a b c d',
'=a': '=a',
Expand Down

0 comments on commit bb60483

Please sign in to comment.