Last active
May 7, 2018 15:09
-
-
Save shgysk8zer0/52cd64b16f137bf6654c to your computer and use it in GitHub Desktop.
Testing polyfill for URLSearchParams
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
.eslintrc | |
.eslintignore | |
.travis.yml | |
zq.js | |
README.md | |
polyfills.js |
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
{ | |
"ecmaFeatures": { | |
"arrowFunctions": true, | |
"blockBindings": true, | |
"classes": true, | |
"destructuring": true, | |
"forOf": true, | |
"generators": true, | |
"templateStrings": true, | |
"unicodeCodePointEscapes": true, | |
"jsx": false, | |
"modules": true | |
}, | |
"rules": { | |
"semi": 2, | |
"quotes": [1, "single"], | |
"strict": 0, | |
"comma-spacing": 1, | |
"new-cap": 0, | |
"no-new": 0, | |
"camelcase": 0, | |
"no-underscore-dangle": 0, | |
"no-negated-in-lhs": 0, | |
"no-extra-boolean-cast": 0, | |
"no-redeclare": 0, | |
"no-shadow": 0, | |
"consistent-return": 0, | |
"eqeqeq": 0, | |
"no-extend-native": 0, | |
"no-undef": 0, | |
"no-unused-expressions": 0, | |
"no-console": 0, | |
"no-alert": 0, | |
"no-unused-vars": 0, | |
"no-loop-func": 0, | |
"no-eval": 0 | |
}, | |
"env": { | |
"browser": true, | |
"node": false, | |
"worker": true, | |
"amd": false, | |
"mocha": false, | |
"jasmine": false, | |
"jquery": false, | |
"prototypejs": false, | |
"shelljs": false, | |
"meteor": false, | |
"mongo": false, | |
"applescript": false, | |
"es6": true | |
}, | |
"globals": { | |
"document": false, | |
"window": false, | |
"ga": true | |
} | |
} |
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
# Auto detect text files and perform LF normalization | |
* text=auto | |
# Custom for Visual Studio | |
*.cs diff=csharp | |
# Standard to msysgit | |
*.doc diff=astextplain | |
*.DOC diff=astextplain | |
*.docx diff=astextplain | |
*.DOCX diff=astextplain | |
*.dot diff=astextplain | |
*.DOT diff=astextplain | |
*.pdf diff=astextplain | |
*.PDF diff=astextplain | |
*.rtf diff=astextplain | |
*.RTF diff=astextplain |
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
*.bak | |
*.orig | |
*.remote | |
*.patch | |
*~ | |
/node_modules/ | |
################# | |
## Eclipse | |
################# | |
*.pydevproject | |
.project | |
.metadata | |
bin/ | |
tmp/ | |
*.tmp | |
*.bak | |
*.swp | |
*~.nib | |
local.properties | |
.classpath | |
.settings/ | |
.loadpath | |
# External tool builders | |
.externalToolBuilders/ | |
# Locally stored "Eclipse launch configurations" | |
*.launch | |
# CDT-specific | |
.cproject | |
# PDT-specific | |
.buildpath | |
################# | |
## Visual Studio | |
################# | |
## Ignore Visual Studio temporary files, build results, and | |
## files generated by popular Visual Studio add-ons. | |
# User-specific files | |
*.suo | |
*.user | |
*.sln.docstates | |
# Build results | |
[Dd]ebug/ | |
[Rr]elease/ | |
x64/ | |
build/ | |
[Bb]in/ | |
[Oo]bj/ | |
# MSTest test Results | |
[Tt]est[Rr]esult*/ | |
[Bb]uild[Ll]og.* | |
*_i.c | |
*_p.c | |
*.ilk | |
*.meta | |
*.obj | |
*.pch | |
*.pdb | |
*.pgc | |
*.pgd | |
*.rsp | |
*.sbr | |
*.tlb | |
*.tli | |
*.tlh | |
*.tmp | |
*.tmp_proj | |
*.log | |
*.vspscc | |
*.vssscc | |
.builds | |
*.pidb | |
*.log | |
*.scc | |
# Visual C++ cache files | |
ipch/ | |
*.aps | |
*.ncb | |
*.opensdf | |
*.sdf | |
*.cachefile | |
# Visual Studio profiler | |
*.psess | |
*.vsp | |
*.vspx | |
# Guidance Automation Toolkit | |
*.gpState | |
# ReSharper is a .NET coding add-in | |
_ReSharper*/ | |
*.[Rr]e[Ss]harper | |
# TeamCity is a build add-in | |
_TeamCity* | |
# DotCover is a Code Coverage Tool | |
*.dotCover | |
# NCrunch | |
*.ncrunch* | |
.*crunch*.local.xml | |
# Installshield output folder | |
[Ee]xpress/ | |
# DocProject is a documentation generator add-in | |
DocProject/buildhelp/ | |
DocProject/Help/*.HxT | |
DocProject/Help/*.HxC | |
DocProject/Help/*.hhc | |
DocProject/Help/*.hhk | |
DocProject/Help/*.hhp | |
DocProject/Help/Html2 | |
DocProject/Help/html | |
# Click-Once directory | |
publish/ | |
# Publish Web Output | |
*.Publish.xml | |
*.pubxml | |
*.publishproj | |
# NuGet Packages Directory | |
## TODO: If you have NuGet Package Restore enabled, uncomment the next line | |
#packages/ | |
# Windows Azure Build Output | |
csx | |
*.build.csdef | |
# Windows Store app package directory | |
AppPackages/ | |
# Others | |
sql/ | |
*.Cache | |
ClientBin/ | |
[Ss]tyle[Cc]op.* | |
~$* | |
*~ | |
*.dbmdl | |
*.[Pp]ublish.xml | |
*.pfx | |
*.publishsettings | |
# RIA/Silverlight projects | |
Generated_Code/ | |
# Backup & report files from converting an old project file to a newer | |
# Visual Studio version. Backup files are not needed, because we have git ;-) | |
_UpgradeReport_Files/ | |
Backup*/ | |
UpgradeLog*.XML | |
UpgradeLog*.htm | |
# SQL Server files | |
App_Data/*.mdf | |
App_Data/*.ldf | |
############# | |
## Windows detritus | |
############# | |
# Windows image file caches | |
Thumbs.db | |
ehthumbs.db | |
# Folder config file | |
Desktop.ini | |
# Recycle Bin used on file shares | |
$RECYCLE.BIN/ | |
# Mac crap | |
.DS_Store | |
############# | |
## Python | |
############# | |
*.py[cod] | |
# Packages | |
*.egg | |
*.egg-info | |
dist/ | |
build/ | |
eggs/ | |
parts/ | |
var/ | |
sdist/ | |
develop-eggs/ | |
.installed.cfg | |
# Installer logs | |
pip-log.txt | |
# Unit test / coverage reports | |
.coverage | |
.tox | |
#Translations | |
*.mo | |
#Mr Developer | |
.mr.developer.cfg |
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
(function(url) { | |
/** | |
* @see https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams | |
*/ | |
class URLSearchParamsX extends Map { | |
constructor(url = '') { | |
super(); | |
url.split('&').forEach(item => { | |
let [key, value] = item.split('='); | |
this.append(decodeURIComponent(key), decodeURIComponent(value)); | |
}); | |
} | |
toString() { | |
console.log(this); | |
let query = []; | |
for (let key of this.keys()) { | |
this.getAll(key).forEach(value => { | |
query.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`); | |
}); | |
} | |
return query.join('&').replace(/%20/g, '+') | |
set(key, value) { | |
return super.set(key, [value]); | |
} | |
append(key, value) { | |
if (this.has(key)) { | |
let values = super.get(key); | |
values.push(value); | |
return super.set(key, values); | |
} else { | |
return this.set(key, value); | |
} | |
} | |
get(key) { | |
return super.get(key)[0]; | |
} | |
getAll(key) { | |
return this.has(key) ? super.get(key) : []; | |
} | |
*values() { | |
for (let key of this.keys()) { | |
yield this.get(key); | |
} | |
} | |
*entries() { | |
for (let key of this.keys()) { | |
yield [key, this.get(key)]; | |
} | |
} | |
} | |
let shim = new URLSearchParamsX(url); | |
let native = new URLSearchParams(url); | |
console.info({ | |
"shim.set(name, Chris)": shim.set('name', 'Chris'), | |
"native.set(name, Chris)": native.set('name', 'Chris'), | |
"shim.append(foo, baz)": shim.append('foo', 'baz'), | |
"native.append(foo, baz)": native.append('foo', 'baz'), | |
"shim.getAll(foo)": shim.getAll('foo'), | |
"native.getAll(foo)": native.getAll('foo'), | |
"shim.has(name)": shim.has('name'), | |
"native.has(name)": native.has('name'), | |
"shim.delete(foo)": shim.delete('foo'), | |
"native.delete(foo)": native.delete('foo'), | |
"shim.has(foo)": shim.has('foo'), | |
"native.has(foo)": native.has('foo'), | |
"native.set(foo, bar)": native.set('foo', 'bar'), | |
"shim.set(foo, bar)": shim.set('foo', 'bar'), | |
"shim.toString()": shim.toString(), | |
"native.toString()": native.toString() | |
}); | |
})('foo=bar&bar=baz+foo&bar=sjkfg'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment