-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f54f265
Showing
9 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = tab | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[{package.json,*.yml}] | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* text=auto | ||
*.js text eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
.nyc_output | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
language: node_js | ||
node_js: | ||
- '6' | ||
- '4' | ||
after_script: | ||
- npm run coveralls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
'use strict'; | ||
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) { | ||
// Do nothing | ||
} | ||
|
||
if (components.length === 1) { | ||
return components; | ||
} | ||
|
||
split = split || 1; | ||
|
||
// Split the array in 2 parts | ||
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) { | ||
let tokens = input.match(singleMatcher); | ||
|
||
for (let i = 1; i < tokens.length; i++) { | ||
input = decodeComponents(tokens, i).join(''); | ||
|
||
tokens = input.match(singleMatcher); | ||
} | ||
|
||
return 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'] | ||
]); | ||
|
||
let 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]); | ||
|
||
if (result !== match[0]) { | ||
replaceMap.set(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'); | ||
|
||
for (const entry of replaceMap.entries()) { | ||
// Replace all decoded components | ||
input = input.replace(new RegExp(entry[0], 'g'), entry[1]); | ||
} | ||
|
||
return input; | ||
} | ||
|
||
module.exports = encodedURI => { | ||
if (typeof encodedURI !== 'string') { | ||
throw new TypeError(`Expected \`encodedURI\` to be of type \`string\`, got \`${typeof encodedURI}\``); | ||
} | ||
|
||
try { | ||
encodedURI = encodedURI.replace(/\+/g, ' '); | ||
|
||
// Try the built in decoder first | ||
return decodeURIComponent(encodedURI); | ||
} catch (err) { | ||
// Fallback to a more advanced decoder | ||
return customDecodeURIComponent(encodedURI); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Sam Verschueren <[email protected]> (github.com/SamVerschueren) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "decode-uri-component", | ||
"version": "0.0.0", | ||
"description": "A better decodeURIComponent", | ||
"license": "MIT", | ||
"repository": "SamVerschueren/decode-uri-component", | ||
"author": { | ||
"name": "Sam Verschueren", | ||
"email": "[email protected]", | ||
"url": "github.com/SamVerschueren" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && nyc ava", | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"decode", | ||
"uri", | ||
"component", | ||
"decodeuricomponent", | ||
"components", | ||
"decoder", | ||
"url" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"coveralls": "^2.13.1", | ||
"nyc": "^10.3.2", | ||
"xo": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# decode-uri-component | ||
|
||
[![Build Status](https://travis-ci.org/SamVerschueren/decode-uri-component.svg?branch=master)](https://travis-ci.org/SamVerschueren/decode-uri-component) [![Coverage Status](https://coveralls.io/repos/SamVerschueren/decode-uri-component/badge.svg?branch=master&service=github)](https://coveralls.io/github/SamVerschueren/decode-uri-component?branch=master) | ||
|
||
> A better [decodeURIComponent](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent) | ||
|
||
## Install | ||
|
||
``` | ||
$ npm install --save decode-uri-component | ||
``` | ||
|
||
|
||
## Usage | ||
|
||
```js | ||
const decodeUriComponent = require('decode-uri-component'); | ||
|
||
decodeUriComponent('%25'); | ||
//=> '%' | ||
|
||
decodeUriComponent('%'); | ||
//=> '%' | ||
|
||
decodeUriComponent('st%C3%A5le'); | ||
//=> 'ståle' | ||
|
||
decodeUriComponent('%st%C3%A5le%'); | ||
//=> '%ståle%' | ||
|
||
decodeUriComponent('%%7Bst%C3%A5le%7D%'); | ||
//=> '%{ståle}%' | ||
|
||
decodeUriComponent('%7B%ab%%7C%de%%7D'); | ||
//=> '{%ab%|%de%}' | ||
|
||
decodeUriComponent('%FE%FF'); | ||
//=> '\uFFFD\uFFFD' | ||
|
||
decodeUriComponent('%C2'); | ||
//=> '\uFFFD' | ||
|
||
decodeUriComponent('%C2%B5'); | ||
//=> 'µ' | ||
``` | ||
|
||
|
||
## API | ||
|
||
### decodeUriComponent(encodedURI) | ||
|
||
#### encodedURI | ||
|
||
Type: `string` | ||
|
||
An encoded component of a Uniform Resource Identifier. | ||
|
||
|
||
## License | ||
|
||
MIT © [Sam Verschueren](https://github.com/SamVerschueren) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import test from 'ava'; | ||
import m from '.'; | ||
|
||
const tests = { | ||
test: 'test', | ||
'a+b': 'a b', | ||
'a+b+c+d': 'a b c d', | ||
'=a': '=a', | ||
'%': '%', | ||
'%25': '%', | ||
'%%25%%': '%%%%', | ||
'st%C3%A5le': 'ståle', | ||
'st%C3%A5le%': 'ståle%', | ||
'%st%C3%A5le%': '%ståle%', | ||
'%%7Bst%C3%A5le%7D%': '%{ståle}%', | ||
'%ab%C3%A5le%': '%abåle%', | ||
'%C3%A5%able%': 'å%able%', | ||
'%7B%ab%7C%de%7D': '{%ab|%de}', | ||
'%7B%ab%%7C%de%%7D': '{%ab%|%de%}', | ||
'%7 B%ab%%7C%de%%7 D': '%7 B%ab%|%de%%7 D', | ||
'%ab': '%ab', | ||
'%ab%ab%ab': '%ab%ab%ab', | ||
'%61+%4d%4D': 'a MM', | ||
'\uFEFFtest': '\uFEFFtest', | ||
'\uFEFF': '\uFEFF', | ||
'%EF%BB%BFtest': '\uFEFFtest', | ||
'%EF%BB%BF': '\uFEFF', | ||
'%FE%FF': '\uFFFD\uFFFD', | ||
'%FF%FE': '\uFFFD\uFFFD', | ||
'†': '†', | ||
'%C2': '\uFFFD', | ||
'%C2x': '\uFFFDx', | ||
'%C2%B5': 'µ', | ||
'%C2%B5%': 'µ%', | ||
'%%C2%B5%': '%µ%' | ||
}; | ||
|
||
function macro(t, input, expected) { | ||
t.is(m(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`'); | ||
}); | ||
|
||
for (const input of Object.keys(tests)) { | ||
test(macro, input, tests[input]); | ||
} |