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 63684ad
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
@@ -1,6 +1,9 @@
language: node_js
node_js:
- '8'
- '6'
- '4'
- '0.12'
- '0.10'
after_script:
- npm run coveralls
24 changes: 12 additions & 12 deletions index.js
@@ -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,18 +42,18 @@ function decode(input) {

function customDecodeURIComponent(input) {
// Keep track of all the replacements and prefill the map with the `BOM`
const replaceMap = new Map([
var replaceMap = new Map([
['%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]));
} catch (err) {
const result = decode(match[0]);
var result = decode(match[0]);

if (result !== match[0]) {
replaceMap.set(match[0], result);
Expand All @@ -66,15 +66,15 @@ function customDecodeURIComponent(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');

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

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}\``);
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
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
@@ -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 63684ad

Please sign in to comment.