@cheprasov/url-query-params
Advanced tools
Comparing version 1.0.1 to 2.0.0
@@ -1,1 +0,1 @@ | ||
(()=>{"use strict";var r={313:function(r,t,e){var a=this&&this.__importDefault||function(r){return r&&r.__esModule?r:{default:r}};Object.defineProperty(t,"__esModule",{value:!0}),t.isURLQueryParamsInstance=void 0;var n=a(e(45));t.isURLQueryParamsInstance=function(r){return r instanceof o};var o=function(){function r(r){this._urlSearchParams=new URLSearchParams((0,t.isURLQueryParamsInstance)(r)?r._urlSearchParams:r),this[Symbol.iterator]=this._urlSearchParams[Symbol.iterator].bind(this._urlSearchParams)}return r.prototype.append=function(r,t){return this._urlSearchParams.append(r,t)},r.prototype.delete=function(r){return this._urlSearchParams.delete(r)},r.prototype.entries=function(){return this._urlSearchParams.entries()},r.prototype.forEach=function(r,t){var e=this,a=t?r.bind(t):r;return this._urlSearchParams.forEach((function(r,t){return a(r,t,e)}))},r.prototype.get=function(r){return this._urlSearchParams.get(r)},r.prototype.getAll=function(r){return this._urlSearchParams.getAll(r)},r.prototype.has=function(r){return this._urlSearchParams.has(r)},r.prototype.keys=function(){return this._urlSearchParams.keys()},r.prototype.set=function(r,t){return this._urlSearchParams.set(r,t)},r.prototype.sort=function(){return this._urlSearchParams.sort()},r.prototype.toObject=function(){var r={};return this.forEach((function(t,e){r[e]=t})),r},r.prototype.toString=function(){var r=[];return this.forEach((function(t,e){r.push((0,n.default)(e)+"="+(0,n.default)(t))})),r.join("&")},r.prototype.values=function(){return this._urlSearchParams.values()},r}();t.default=o,Symbol.iterator},45:(r,t)=>{Object.defineProperty(t,"__esModule",{value:!0});var e=function(r){return"%"+r.charCodeAt(0).toString(16).toUpperCase()},a=/[!'()*]/g;t.default=function(r){return encodeURIComponent(r).replace(a,e)}},607:function(r,t,e){var a=this&&this.__importDefault||function(r){return r&&r.__esModule?r:{default:r}};Object.defineProperty(t,"__esModule",{value:!0}),t.URLQueryParams=void 0;var n=e(313);Object.defineProperty(t,"URLQueryParams",{enumerable:!0,get:function(){return a(n).default}})}},t={},e=function e(a){var n=t[a];if(void 0!==n)return n.exports;var o=t[a]={exports:{}};return r[a].call(o.exports,o,o.exports,e),o.exports}(607);module.exports=e})(); | ||
(()=>{"use strict";var r={313:function(r,e,t){var a=this&&this.__importDefault||function(r){return r&&r.__esModule?r:{default:r}};Object.defineProperty(e,"__esModule",{value:!0}),e.isURLQueryParamsInstance=void 0;const s=a(t(45));e.isURLQueryParamsInstance=r=>r instanceof u;class u{constructor(r){this._urlSearchParams=new URLSearchParams((0,e.isURLQueryParamsInstance)(r)?r._urlSearchParams:r),this[Symbol.iterator]=this._urlSearchParams[Symbol.iterator].bind(this._urlSearchParams)}append(r,e){return this._urlSearchParams.append(r,e)}delete(r){return this._urlSearchParams.delete(r)}entries(){return this._urlSearchParams.entries()}forEach(r,e){const t=this,a=e?r.bind(e):r;return this._urlSearchParams.forEach((function(r,e){return a(r,e,t)}))}get(r){return this._urlSearchParams.get(r)}getAll(r){return this._urlSearchParams.getAll(r)}has(r){return this._urlSearchParams.has(r)}keys(){return this._urlSearchParams.keys()}set(r,e){return this._urlSearchParams.set(r,e)}sort(){return this._urlSearchParams.sort()}toObject(){const r={};return this.forEach(((e,t)=>{t in r?(Array.isArray(r[t])||(r[t]=[r[t]]),r[t].push(e)):r[t]=e})),r}toString(){const r=[];return this.forEach(((e,t)=>{r.push(`${(0,s.default)(t)}=${(0,s.default)(e)}`)})),r.join("&")}values(){return this._urlSearchParams.values()}}Symbol.iterator,e.default=u},45:(r,e)=>{Object.defineProperty(e,"__esModule",{value:!0});const t=r=>"%"+r.charCodeAt(0).toString(16).toUpperCase(),a=/[!'()*]/g;e.default=function(r){return encodeURIComponent(r).replace(a,t)}},607:function(r,e,t){var a=this&&this.__importDefault||function(r){return r&&r.__esModule?r:{default:r}};Object.defineProperty(e,"__esModule",{value:!0}),e.URLQueryParams=void 0;var s=t(313);Object.defineProperty(e,"URLQueryParams",{enumerable:!0,get:function(){return a(s).default}})}},e={},t=function t(a){var s=e[a];if(void 0!==s)return s.exports;var u=e[a]={exports:{}};return r[a].call(u.exports,u,u.exports,t),u.exports}(607);module.exports=t})(); |
@@ -1,4 +0,4 @@ | ||
export declare type InitType = string[][] | Record<string, string> | string | URLSearchParams | URLQueryParams; | ||
export type InitType = string[][] | Record<string, string> | string | URLSearchParams | URLQueryParams; | ||
export declare const isURLQueryParamsInstance: (some: any) => some is URLQueryParams; | ||
export default class URLQueryParams implements URLSearchParams { | ||
export default class URLQueryParams { | ||
private readonly _urlSearchParams; | ||
@@ -17,5 +17,5 @@ [Symbol.iterator]: () => IterableIterator<[string, string]>; | ||
sort(): void; | ||
toObject(): Record<string, string>; | ||
toObject(): Record<string, string | string[]>; | ||
toString(): string; | ||
values(): IterableIterator<string>; | ||
} |
{ | ||
"name": "@cheprasov/url-query-params", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"author": "Alexander Cheprasov", | ||
@@ -25,28 +25,29 @@ "description": "The URLQueryParams class defines utility methods to work with the query string of a URL. It is a good replacement for standard URLSearchParams class", | ||
"devDependencies": { | ||
"@babel/cli": "^7.16.0", | ||
"@babel/core": "^7.16.0", | ||
"@babel/plugin-proposal-class-properties": "^7.16.0", | ||
"@babel/plugin-transform-arrow-functions": "^7.16.0", | ||
"@babel/preset-env": "^7.16.0", | ||
"@babel/preset-flow": "^7.16.0", | ||
"@babel/register": "^7.16.0", | ||
"@types/jest": "^27.0.2", | ||
"@types/webpack": "^5.28.0", | ||
"@typescript-eslint/eslint-plugin": "^5.3.1", | ||
"@typescript-eslint/parser": "^5.3.1", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"@babel/cli": "^7.23.4", | ||
"@babel/core": "^7.23.7", | ||
"@babel/plugin-proposal-class-properties": "^7.18.6", | ||
"@babel/plugin-transform-arrow-functions": "^7.23.3", | ||
"@babel/preset-env": "^7.23.8", | ||
"@babel/preset-flow": "^7.23.3", | ||
"@babel/preset-typescript": "^7.23.3", | ||
"@babel/register": "^7.23.7", | ||
"@types/jest": "^29.5.11", | ||
"@types/webpack": "^5.28.5", | ||
"@typescript-eslint/eslint-plugin": "^6.18.1", | ||
"@typescript-eslint/parser": "^6.18.1", | ||
"babel-core": "^6.26.3", | ||
"babel-eslint": "^10.1.0", | ||
"babel-jest": "^27.3.1", | ||
"babel-loader": "^8.2.3", | ||
"babel-preset-minify": "^0.5.1", | ||
"eslint": "^8.2.0", | ||
"eslint-config-airbnb": "^19.0.0", | ||
"babel-jest": "^29.7.0", | ||
"babel-loader": "^9.1.3", | ||
"babel-preset-minify": "^0.5.2", | ||
"eslint": "^8.56.0", | ||
"eslint-config-airbnb": "^19.0.4", | ||
"eslint-config-airbnb-base": "^15.0.0", | ||
"eslint-plugin-import": "^2.25.3", | ||
"jest": "^27.3.1", | ||
"ts-jest": "^27.0.7", | ||
"ts-loader": "^9.2.6", | ||
"typescript": "^4.4.4", | ||
"webpack": "^5.64.0", | ||
"webpack-cli": "^4.9.1" | ||
"eslint-plugin-import": "^2.29.1", | ||
"jest": "^29.7.0", | ||
"ts-jest": "^29.1.1", | ||
"ts-loader": "^9.5.1", | ||
"typescript": "^5.3.3", | ||
"webpack": "^5.89.0", | ||
"webpack-cli": "^5.1.4" | ||
}, | ||
@@ -53,0 +54,0 @@ "scripts": { |
@@ -5,5 +5,6 @@ import encodeURIComponentRFC3986 from './encodeURIComponentRFC3986'; | ||
it('should decode characters', () => { | ||
expect(encodeURIComponentRFC3986('for bar 42 ;,/?:@&=+$ -_.!~*\'() #')) | ||
.toEqual('for%20bar%2042%20%3B%2C%2F%3F%3A%40%26%3D%2B%24%20-_.%21~%2A%27%28%29%20%23'); | ||
expect(encodeURIComponentRFC3986("for bar 42 ;,/?:@&=+$ -_.!~*'() #")).toEqual( | ||
'for%20bar%2042%20%3B%2C%2F%3F%3A%40%26%3D%2B%24%20-_.%21~%2A%27%28%29%20%23' | ||
); | ||
}); | ||
}); | ||
}); |
const replacer = (char: String) => { | ||
const replacer = (char: string) => { | ||
return '%' + char.charCodeAt(0).toString(16).toUpperCase(); | ||
@@ -4,0 +4,0 @@ }; |
import URLQueryParams, { isURLQueryParamsInstance } from './URLQueryParams'; | ||
const encodeByChar = { | ||
"0": "0", | ||
"1": "1", | ||
"2": "2", | ||
"3": "3", | ||
"4": "4", | ||
"5": "5", | ||
"6": "6", | ||
"7": "7", | ||
"8": "8", | ||
"9": "9", | ||
"\u0000": "%00", | ||
"\u0001": "%01", | ||
"\u0002": "%02", | ||
"\u0003": "%03", | ||
"\u0004": "%04", | ||
"\u0005": "%05", | ||
"\u0006": "%06", | ||
"\u0007": "%07", | ||
"\b": "%08", | ||
"\t": "%09", | ||
"\n": "%0A", | ||
"\u000b": "%0B", | ||
"\f": "%0C", | ||
"\r": "%0D", | ||
"\u000e": "%0E", | ||
"\u000f": "%0F", | ||
"\u0010": "%10", | ||
"\u0011": "%11", | ||
"\u0012": "%12", | ||
"\u0013": "%13", | ||
"\u0014": "%14", | ||
"\u0015": "%15", | ||
"\u0016": "%16", | ||
"\u0017": "%17", | ||
"\u0018": "%18", | ||
"\u0019": "%19", | ||
"\u001a": "%1A", | ||
"\u001b": "%1B", | ||
"\u001c": "%1C", | ||
"\u001d": "%1D", | ||
"\u001e": "%1E", | ||
"\u001f": "%1F", | ||
" ": "%20", | ||
"!": "%21", | ||
"\"": "%22", | ||
"#": "%23", | ||
"$": "%24", | ||
"%": "%25", | ||
"&": "%26", | ||
"'": "%27", | ||
"(": "%28", | ||
")": "%29", | ||
"*": "%2A", | ||
"+": "%2B", | ||
",": "%2C", | ||
"-": "-", | ||
".": ".", | ||
"/": "%2F", | ||
":": "%3A", | ||
";": "%3B", | ||
"<": "%3C", | ||
"=": "%3D", | ||
">": "%3E", | ||
"?": "%3F", | ||
"@": "%40", | ||
"A": "A", | ||
"B": "B", | ||
"C": "C", | ||
"D": "D", | ||
"E": "E", | ||
"F": "F", | ||
"G": "G", | ||
"H": "H", | ||
"I": "I", | ||
"J": "J", | ||
"K": "K", | ||
"L": "L", | ||
"M": "M", | ||
"N": "N", | ||
"O": "O", | ||
"P": "P", | ||
"Q": "Q", | ||
"R": "R", | ||
"S": "S", | ||
"T": "T", | ||
"U": "U", | ||
"V": "V", | ||
"W": "W", | ||
"X": "X", | ||
"Y": "Y", | ||
"Z": "Z", | ||
"[": "%5B", | ||
"\\": "%5C", | ||
"]": "%5D", | ||
"^": "%5E", | ||
"_": "_", | ||
"`": "%60", | ||
"a": "a", | ||
"b": "b", | ||
"c": "c", | ||
"d": "d", | ||
"e": "e", | ||
"f": "f", | ||
"g": "g", | ||
"h": "h", | ||
"i": "i", | ||
"j": "j", | ||
"k": "k", | ||
"l": "l", | ||
"m": "m", | ||
"n": "n", | ||
"o": "o", | ||
"p": "p", | ||
"q": "q", | ||
"r": "r", | ||
"s": "s", | ||
"t": "t", | ||
"u": "u", | ||
"v": "v", | ||
"w": "w", | ||
"x": "x", | ||
"y": "y", | ||
"z": "z", | ||
"{": "%7B", | ||
"|": "%7C", | ||
"}": "%7D", | ||
"~": "~", | ||
"": "%7F", | ||
"": "%C2%80", | ||
"": "%C2%81", | ||
"": "%C2%82", | ||
"": "%C2%83", | ||
"": "%C2%84", | ||
" ": "%C2%85", | ||
"": "%C2%86", | ||
"": "%C2%87", | ||
"": "%C2%88", | ||
"": "%C2%89", | ||
"": "%C2%8A", | ||
"": "%C2%8B", | ||
"": "%C2%8C", | ||
"": "%C2%8D", | ||
"": "%C2%8E", | ||
"": "%C2%8F", | ||
"": "%C2%90", | ||
"": "%C2%91", | ||
"": "%C2%92", | ||
"": "%C2%93", | ||
"": "%C2%94", | ||
"": "%C2%95", | ||
"": "%C2%96", | ||
"": "%C2%97", | ||
"": "%C2%98", | ||
"": "%C2%99", | ||
"": "%C2%9A", | ||
"": "%C2%9B", | ||
"": "%C2%9C", | ||
"": "%C2%9D", | ||
"": "%C2%9E", | ||
"": "%C2%9F", | ||
" ": "%C2%A0", | ||
"¡": "%C2%A1", | ||
"¢": "%C2%A2", | ||
"£": "%C2%A3", | ||
"¤": "%C2%A4", | ||
"¥": "%C2%A5", | ||
"¦": "%C2%A6", | ||
"§": "%C2%A7", | ||
"¨": "%C2%A8", | ||
"©": "%C2%A9", | ||
"ª": "%C2%AA", | ||
"«": "%C2%AB", | ||
"¬": "%C2%AC", | ||
"": "%C2%AD", | ||
"®": "%C2%AE", | ||
"¯": "%C2%AF", | ||
"°": "%C2%B0", | ||
"±": "%C2%B1", | ||
"²": "%C2%B2", | ||
"³": "%C2%B3", | ||
"´": "%C2%B4", | ||
"µ": "%C2%B5", | ||
"¶": "%C2%B6", | ||
"·": "%C2%B7", | ||
"¸": "%C2%B8", | ||
"¹": "%C2%B9", | ||
"º": "%C2%BA", | ||
"»": "%C2%BB", | ||
"¼": "%C2%BC", | ||
"½": "%C2%BD", | ||
"¾": "%C2%BE", | ||
"¿": "%C2%BF", | ||
"À": "%C3%80", | ||
"Á": "%C3%81", | ||
"Â": "%C3%82", | ||
"Ã": "%C3%83", | ||
"Ä": "%C3%84", | ||
"Å": "%C3%85", | ||
"Æ": "%C3%86", | ||
"Ç": "%C3%87", | ||
"È": "%C3%88", | ||
"É": "%C3%89", | ||
"Ê": "%C3%8A", | ||
"Ë": "%C3%8B", | ||
"Ì": "%C3%8C", | ||
"Í": "%C3%8D", | ||
"Î": "%C3%8E", | ||
"Ï": "%C3%8F", | ||
"Ð": "%C3%90", | ||
"Ñ": "%C3%91", | ||
"Ò": "%C3%92", | ||
"Ó": "%C3%93", | ||
"Ô": "%C3%94", | ||
"Õ": "%C3%95", | ||
"Ö": "%C3%96", | ||
"×": "%C3%97", | ||
"Ø": "%C3%98", | ||
"Ù": "%C3%99", | ||
"Ú": "%C3%9A", | ||
"Û": "%C3%9B", | ||
"Ü": "%C3%9C", | ||
"Ý": "%C3%9D", | ||
"Þ": "%C3%9E", | ||
"ß": "%C3%9F", | ||
"à": "%C3%A0", | ||
"á": "%C3%A1", | ||
"â": "%C3%A2", | ||
"ã": "%C3%A3", | ||
"ä": "%C3%A4", | ||
"å": "%C3%A5", | ||
"æ": "%C3%A6", | ||
"ç": "%C3%A7", | ||
"è": "%C3%A8", | ||
"é": "%C3%A9", | ||
"ê": "%C3%AA", | ||
"ë": "%C3%AB", | ||
"ì": "%C3%AC", | ||
"í": "%C3%AD", | ||
"î": "%C3%AE", | ||
"ï": "%C3%AF", | ||
"ð": "%C3%B0", | ||
"ñ": "%C3%B1", | ||
"ò": "%C3%B2", | ||
"ó": "%C3%B3", | ||
"ô": "%C3%B4", | ||
"õ": "%C3%B5", | ||
"ö": "%C3%B6", | ||
"÷": "%C3%B7", | ||
"ø": "%C3%B8", | ||
"ù": "%C3%B9", | ||
"ú": "%C3%BA", | ||
"û": "%C3%BB", | ||
"ü": "%C3%BC", | ||
"ý": "%C3%BD", | ||
"þ": "%C3%BE", | ||
"ÿ": "%C3%BF", | ||
'0': '0', | ||
'1': '1', | ||
'2': '2', | ||
'3': '3', | ||
'4': '4', | ||
'5': '5', | ||
'6': '6', | ||
'7': '7', | ||
'8': '8', | ||
'9': '9', | ||
'\u0000': '%00', | ||
'\u0001': '%01', | ||
'\u0002': '%02', | ||
'\u0003': '%03', | ||
'\u0004': '%04', | ||
'\u0005': '%05', | ||
'\u0006': '%06', | ||
'\u0007': '%07', | ||
'\b': '%08', | ||
'\t': '%09', | ||
'\n': '%0A', | ||
'\u000b': '%0B', | ||
'\f': '%0C', | ||
'\r': '%0D', | ||
'\u000e': '%0E', | ||
'\u000f': '%0F', | ||
'\u0010': '%10', | ||
'\u0011': '%11', | ||
'\u0012': '%12', | ||
'\u0013': '%13', | ||
'\u0014': '%14', | ||
'\u0015': '%15', | ||
'\u0016': '%16', | ||
'\u0017': '%17', | ||
'\u0018': '%18', | ||
'\u0019': '%19', | ||
'\u001a': '%1A', | ||
'\u001b': '%1B', | ||
'\u001c': '%1C', | ||
'\u001d': '%1D', | ||
'\u001e': '%1E', | ||
'\u001f': '%1F', | ||
' ': '%20', | ||
'!': '%21', | ||
'"': '%22', | ||
'#': '%23', | ||
$: '%24', | ||
'%': '%25', | ||
'&': '%26', | ||
"'": '%27', | ||
'(': '%28', | ||
')': '%29', | ||
'*': '%2A', | ||
'+': '%2B', | ||
',': '%2C', | ||
'-': '-', | ||
'.': '.', | ||
'/': '%2F', | ||
':': '%3A', | ||
';': '%3B', | ||
'<': '%3C', | ||
'=': '%3D', | ||
'>': '%3E', | ||
'?': '%3F', | ||
'@': '%40', | ||
A: 'A', | ||
B: 'B', | ||
C: 'C', | ||
D: 'D', | ||
E: 'E', | ||
F: 'F', | ||
G: 'G', | ||
H: 'H', | ||
I: 'I', | ||
J: 'J', | ||
K: 'K', | ||
L: 'L', | ||
M: 'M', | ||
N: 'N', | ||
O: 'O', | ||
P: 'P', | ||
Q: 'Q', | ||
R: 'R', | ||
S: 'S', | ||
T: 'T', | ||
U: 'U', | ||
V: 'V', | ||
W: 'W', | ||
X: 'X', | ||
Y: 'Y', | ||
Z: 'Z', | ||
'[': '%5B', | ||
'\\': '%5C', | ||
']': '%5D', | ||
'^': '%5E', | ||
_: '_', | ||
'`': '%60', | ||
a: 'a', | ||
b: 'b', | ||
c: 'c', | ||
d: 'd', | ||
e: 'e', | ||
f: 'f', | ||
g: 'g', | ||
h: 'h', | ||
i: 'i', | ||
j: 'j', | ||
k: 'k', | ||
l: 'l', | ||
m: 'm', | ||
n: 'n', | ||
o: 'o', | ||
p: 'p', | ||
q: 'q', | ||
r: 'r', | ||
s: 's', | ||
t: 't', | ||
u: 'u', | ||
v: 'v', | ||
w: 'w', | ||
x: 'x', | ||
y: 'y', | ||
z: 'z', | ||
'{': '%7B', | ||
'|': '%7C', | ||
'}': '%7D', | ||
'~': '~', | ||
'': '%7F', | ||
'': '%C2%80', | ||
'': '%C2%81', | ||
'': '%C2%82', | ||
'': '%C2%83', | ||
'': '%C2%84', | ||
' ': '%C2%85', | ||
'': '%C2%86', | ||
'': '%C2%87', | ||
'': '%C2%88', | ||
'': '%C2%89', | ||
'': '%C2%8A', | ||
'': '%C2%8B', | ||
'': '%C2%8C', | ||
'': '%C2%8D', | ||
'': '%C2%8E', | ||
'': '%C2%8F', | ||
'': '%C2%90', | ||
'': '%C2%91', | ||
'': '%C2%92', | ||
'': '%C2%93', | ||
'': '%C2%94', | ||
'': '%C2%95', | ||
'': '%C2%96', | ||
'': '%C2%97', | ||
'': '%C2%98', | ||
'': '%C2%99', | ||
'': '%C2%9A', | ||
'': '%C2%9B', | ||
'': '%C2%9C', | ||
'': '%C2%9D', | ||
'': '%C2%9E', | ||
'': '%C2%9F', | ||
' ': '%C2%A0', | ||
'¡': '%C2%A1', | ||
'¢': '%C2%A2', | ||
'£': '%C2%A3', | ||
'¤': '%C2%A4', | ||
'¥': '%C2%A5', | ||
'¦': '%C2%A6', | ||
'§': '%C2%A7', | ||
'¨': '%C2%A8', | ||
'©': '%C2%A9', | ||
ª: '%C2%AA', | ||
'«': '%C2%AB', | ||
'¬': '%C2%AC', | ||
'': '%C2%AD', | ||
'®': '%C2%AE', | ||
'¯': '%C2%AF', | ||
'°': '%C2%B0', | ||
'±': '%C2%B1', | ||
'²': '%C2%B2', | ||
'³': '%C2%B3', | ||
'´': '%C2%B4', | ||
µ: '%C2%B5', | ||
'¶': '%C2%B6', | ||
'·': '%C2%B7', | ||
'¸': '%C2%B8', | ||
'¹': '%C2%B9', | ||
º: '%C2%BA', | ||
'»': '%C2%BB', | ||
'¼': '%C2%BC', | ||
'½': '%C2%BD', | ||
'¾': '%C2%BE', | ||
'¿': '%C2%BF', | ||
À: '%C3%80', | ||
Á: '%C3%81', | ||
Â: '%C3%82', | ||
Ã: '%C3%83', | ||
Ä: '%C3%84', | ||
Å: '%C3%85', | ||
Æ: '%C3%86', | ||
Ç: '%C3%87', | ||
È: '%C3%88', | ||
É: '%C3%89', | ||
Ê: '%C3%8A', | ||
Ë: '%C3%8B', | ||
Ì: '%C3%8C', | ||
Í: '%C3%8D', | ||
Î: '%C3%8E', | ||
Ï: '%C3%8F', | ||
Ð: '%C3%90', | ||
Ñ: '%C3%91', | ||
Ò: '%C3%92', | ||
Ó: '%C3%93', | ||
Ô: '%C3%94', | ||
Õ: '%C3%95', | ||
Ö: '%C3%96', | ||
'×': '%C3%97', | ||
Ø: '%C3%98', | ||
Ù: '%C3%99', | ||
Ú: '%C3%9A', | ||
Û: '%C3%9B', | ||
Ü: '%C3%9C', | ||
Ý: '%C3%9D', | ||
Þ: '%C3%9E', | ||
ß: '%C3%9F', | ||
à: '%C3%A0', | ||
á: '%C3%A1', | ||
â: '%C3%A2', | ||
ã: '%C3%A3', | ||
ä: '%C3%A4', | ||
å: '%C3%A5', | ||
æ: '%C3%A6', | ||
ç: '%C3%A7', | ||
è: '%C3%A8', | ||
é: '%C3%A9', | ||
ê: '%C3%AA', | ||
ë: '%C3%AB', | ||
ì: '%C3%AC', | ||
í: '%C3%AD', | ||
î: '%C3%AE', | ||
ï: '%C3%AF', | ||
ð: '%C3%B0', | ||
ñ: '%C3%B1', | ||
ò: '%C3%B2', | ||
ó: '%C3%B3', | ||
ô: '%C3%B4', | ||
õ: '%C3%B5', | ||
ö: '%C3%B6', | ||
'÷': '%C3%B7', | ||
ø: '%C3%B8', | ||
ù: '%C3%B9', | ||
ú: '%C3%BA', | ||
û: '%C3%BB', | ||
ü: '%C3%BC', | ||
ý: '%C3%BD', | ||
þ: '%C3%BE', | ||
ÿ: '%C3%BF', | ||
} as Readonly<Record<string, string>>; | ||
describe('isURLQueryParamsInstance', () => { | ||
it('should return true for instances of URLQueryParams', () => { | ||
expect(new URLQueryParams()).toBeInstanceOf(URLQueryParams); | ||
}); | ||
it('should return true for instances of URLQueryParams', () => { | ||
expect(new URLQueryParams()).toBeInstanceOf(URLQueryParams); | ||
}); | ||
it('should return false for not instances of URLQueryParams', () => { | ||
expect(new URLSearchParams()).not.toBeInstanceOf(URLQueryParams); | ||
expect(new Date()).not.toBeInstanceOf(URLQueryParams); | ||
expect(null).not.toBeInstanceOf(URLQueryParams); | ||
}) | ||
it('should return false for not instances of URLQueryParams', () => { | ||
expect(new URLSearchParams()).not.toBeInstanceOf(URLQueryParams); | ||
expect(new Date()).not.toBeInstanceOf(URLQueryParams); | ||
expect(null).not.toBeInstanceOf(URLQueryParams); | ||
}); | ||
}); | ||
describe('URLQueryParams', () => { | ||
let urlQueryParams: URLQueryParams; | ||
let urlQueryParams: URLQueryParams; | ||
beforeEach(() => { | ||
urlQueryParams = new URLQueryParams('foo=bar&q=42&empty=&'); | ||
expect(urlQueryParams.toObject()).toEqual({ foo: 'bar', 'q': '42', empty: '' }); | ||
}); | ||
describe('constructor', () => { | ||
it('should not be instance of URLSearchParams', () => { | ||
expect(urlQueryParams).not.toBeInstanceOf(URLSearchParams); | ||
beforeEach(() => { | ||
urlQueryParams = new URLQueryParams('foo=bar&q=42&empty=&baz=43&baz=44'); | ||
expect(urlQueryParams.toObject()).toEqual({ foo: 'bar', q: '42', empty: '', baz: ['43', '44'] }); | ||
}); | ||
it('should allow to use URLSearchParams as init param', () => { | ||
expect(new URLQueryParams(new URLSearchParams('foo=bar&q=42')).toObject()).toEqual({ foo: 'bar', 'q': '42'}); | ||
}); | ||
describe('constructor', () => { | ||
it('should not be instance of URLSearchParams', () => { | ||
expect(urlQueryParams).not.toBeInstanceOf(URLSearchParams); | ||
}); | ||
it('should allow to use URLQueryParams as init param', () => { | ||
expect(new URLQueryParams(urlQueryParams).toObject()).toEqual({ foo: 'bar', 'q': '42', empty: '' }); | ||
}); | ||
it('should allow to use URLSearchParams as init param', () => { | ||
expect(new URLQueryParams(new URLSearchParams('foo=bar&q=42')).toObject()).toEqual({ foo: 'bar', q: '42' }); | ||
}); | ||
it('should allow to use an object as init param', () => { | ||
expect(new URLQueryParams({ foo: 'bar', 'q': '42' }).toObject()).toEqual({ foo: 'bar', 'q': '42' }); | ||
}); | ||
it('should allow to use URLQueryParams as init param', () => { | ||
expect(new URLQueryParams(urlQueryParams).toObject()).toEqual({ | ||
foo: 'bar', | ||
q: '42', | ||
empty: '', | ||
baz: ['43', '44'], | ||
}); | ||
}); | ||
it('should allow to use array of strings as init param', () => { | ||
expect(new URLQueryParams([['foo', 'bar'], ['q', '42']]).toObject()).toEqual({ foo: 'bar', 'q': '42' }); | ||
}); | ||
it('should allow to use an object as init param', () => { | ||
expect(new URLQueryParams({ foo: 'bar', q: '42' }).toObject()).toEqual({ foo: 'bar', q: '42' }); | ||
}); | ||
it('should allow to use string as init param', () => { | ||
expect(new URLQueryParams('foo=bar&q=42').toObject()).toEqual({ foo: 'bar', 'q': '42' }); | ||
}); | ||
it('should allow to use array of strings as init param', () => { | ||
expect( | ||
new URLQueryParams([ | ||
['foo', 'bar'], | ||
['q', '42'], | ||
]).toObject() | ||
).toEqual({ foo: 'bar', q: '42' }); | ||
}); | ||
it('should allow to use constructor without params', () => { | ||
expect(new URLQueryParams().toObject()).toEqual({}); | ||
}); | ||
it('should allow to use string as init param', () => { | ||
expect(new URLQueryParams('foo=bar&q=42').toObject()).toEqual({ foo: 'bar', q: '42' }); | ||
}); | ||
it('should decode `+` as space character', () => { | ||
expect(new URLQueryParams('q=s+p+a+c+e+s').toObject()).toEqual({ q: 's p a c e s'}); | ||
}); | ||
it('should allow to use constructor without params', () => { | ||
expect(new URLQueryParams().toObject()).toEqual({}); | ||
}); | ||
it('should decode characters', () => { | ||
for (const char in encodeByChar) { | ||
if (!encodeByChar.hasOwnProperty(char)) { | ||
continue; | ||
} | ||
const encodedChar = encodeByChar[char]; | ||
urlQueryParams = new URLQueryParams(`test=${encodedChar}`); | ||
expect(urlQueryParams.get('test')).toEqual(char); | ||
} | ||
}); | ||
}); | ||
it('should decode `+` as space character', () => { | ||
expect(new URLQueryParams('q=s+p+a+c+e+s').toObject()).toEqual({ q: 's p a c e s' }); | ||
}); | ||
describe('<IterableIterator>', () => { | ||
it('should be iterable', () => { | ||
const obj: Record<string, string> = {}; | ||
for (const [ key, value ] of urlQueryParams) { | ||
obj[key] = value; | ||
} | ||
expect(urlQueryParams.toObject()).toEqual(obj); | ||
it('should decode characters', () => { | ||
for (const char in encodeByChar) { | ||
if (!encodeByChar.hasOwnProperty(char)) { | ||
continue; | ||
} | ||
const encodedChar = encodeByChar[char]; | ||
urlQueryParams = new URLQueryParams(`test=${encodedChar}`); | ||
expect(urlQueryParams.get('test')).toEqual(char); | ||
} | ||
}); | ||
}); | ||
}); | ||
describe('append', () => { | ||
it('should append new params', () => { | ||
urlQueryParams.append('test', 'passed'); | ||
expect(urlQueryParams.getAll('test')).toEqual(['passed']); | ||
describe('<IterableIterator>', () => { | ||
it('should be iterable', () => { | ||
const obj: Record<string, string | string[]> = {}; | ||
// @ts-ignore | ||
for (const [key, value] of urlQueryParams) { | ||
if (key in obj) { | ||
if (!Array.isArray(obj[key])) { | ||
obj[key] = [obj[key] as string]; | ||
} | ||
(obj[key] as string[]).push(value); | ||
} else { | ||
obj[key] = value; | ||
} | ||
} | ||
expect(urlQueryParams.toObject()).toEqual(obj); | ||
}); | ||
}); | ||
it('should append param with same key', () => { | ||
urlQueryParams.append('test', 'passed'); | ||
urlQueryParams.append('test', 'ok'); | ||
expect(urlQueryParams.getAll('test')).toEqual(['passed', 'ok']); | ||
}); | ||
}); | ||
describe('append', () => { | ||
it('should append new params', () => { | ||
urlQueryParams.append('test', 'passed'); | ||
expect(urlQueryParams.getAll('test')).toEqual(['passed']); | ||
}); | ||
describe('delete', () => { | ||
it('should delete param by key', () => { | ||
urlQueryParams.delete('foo'); | ||
expect(urlQueryParams.has('foo')).toBeFalsy(); | ||
it('should append param with same key', () => { | ||
urlQueryParams.append('test', 'passed'); | ||
urlQueryParams.append('test', 'ok'); | ||
expect(urlQueryParams.getAll('test')).toEqual(['passed', 'ok']); | ||
}); | ||
}); | ||
}); | ||
describe('entries', () => { | ||
it('should return params as entries', () => { | ||
const entries = urlQueryParams.entries(); | ||
expect(typeof entries[Symbol.iterator]).toEqual('function'); | ||
expect(entries.next()).toEqual({ done: false, value: ['foo', 'bar'] }); | ||
expect(entries.next()).toEqual({ done: false, value: ['q', '42'] }); | ||
expect(entries.next()).toEqual({ done: false, value: ['empty', ''] }); | ||
expect(entries.next()).toEqual({ done: true, value: undefined }); | ||
describe('delete', () => { | ||
it('should delete param by key', () => { | ||
urlQueryParams.delete('foo'); | ||
expect(urlQueryParams.has('foo')).toBeFalsy(); | ||
}); | ||
}); | ||
}); | ||
describe('forEach', () => { | ||
it('should iterate over items', () => { | ||
const obj: Record<string, string> = {}; | ||
urlQueryParams.forEach((value, key, parent) => { | ||
expect(parent).toBe(urlQueryParams); | ||
obj[key] = value; | ||
}); | ||
expect(obj).toEqual(urlQueryParams.toObject()); | ||
describe('entries', () => { | ||
it('should return params as entries', () => { | ||
const entries = urlQueryParams.entries(); | ||
expect(typeof entries[Symbol.iterator]).toEqual('function'); | ||
expect(entries.next()).toEqual({ done: false, value: ['foo', 'bar'] }); | ||
expect(entries.next()).toEqual({ done: false, value: ['q', '42'] }); | ||
expect(entries.next()).toEqual({ done: false, value: ['empty', ''] }); | ||
expect(entries.next()).toEqual({ done: false, value: ['baz', '43'] }); | ||
expect(entries.next()).toEqual({ done: false, value: ['baz', '44'] }); | ||
expect(entries.next()).toEqual({ done: true, value: undefined }); | ||
}); | ||
}); | ||
it('should user thisArg right', () => { | ||
const obj: Record<string, string> = {}; | ||
urlQueryParams.forEach(function (value, key, parent) { | ||
// @ts-ignore | ||
this[key] = value; | ||
}, obj); | ||
expect(obj).toEqual(urlQueryParams.toObject()); | ||
}); | ||
}); | ||
describe('forEach', () => { | ||
it('should iterate over items', () => { | ||
const obj: Record<string, string | string[]> = {}; | ||
urlQueryParams.forEach((value, key, parent) => { | ||
expect(parent).toBe(urlQueryParams); | ||
if (key in obj) { | ||
if (!Array.isArray(obj[key])) { | ||
obj[key] = [obj[key] as string]; | ||
} | ||
(obj[key] as string[]).push(value); | ||
} else { | ||
obj[key] = value; | ||
} | ||
}); | ||
expect(obj).toEqual(urlQueryParams.toObject()); | ||
}); | ||
describe('get', () => { | ||
it('should return value by key', () => { | ||
expect(urlQueryParams.get('foo')).toEqual('bar'); | ||
expect(urlQueryParams.get('q')).toEqual('42'); | ||
expect(urlQueryParams.get('empty')).toEqual(''); | ||
it('should user thisArg right', () => { | ||
const obj: Record<string, string | string[]> = {}; | ||
urlQueryParams.forEach(function (value, key, parent) { | ||
// @ts-ignore | ||
if (key in this) { | ||
// @ts-ignore | ||
if (!Array.isArray(this[key])) { | ||
// @ts-ignore | ||
this[key] = [this[key]]; | ||
} | ||
// @ts-ignore | ||
this[key].push(value); | ||
} else { | ||
// @ts-ignore | ||
this[key] = value; | ||
} | ||
}, obj); | ||
expect(obj).toEqual(urlQueryParams.toObject()); | ||
}); | ||
}); | ||
it('should return first value for key', () => { | ||
urlQueryParams.append('foo', 'baz'); | ||
expect(urlQueryParams.get('foo')).toEqual('bar'); | ||
}); | ||
describe('get', () => { | ||
it('should return value by key', () => { | ||
expect(urlQueryParams.get('foo')).toEqual('bar'); | ||
expect(urlQueryParams.get('q')).toEqual('42'); | ||
expect(urlQueryParams.get('empty')).toEqual(''); | ||
}); | ||
it('should return null if key is not exist', () => { | ||
expect(urlQueryParams.get('bad')).toBeNull(); | ||
}); | ||
}); | ||
it('should return first value for key', () => { | ||
urlQueryParams.append('foo', 'baz'); | ||
expect(urlQueryParams.get('foo')).toEqual('bar'); | ||
}); | ||
describe('getAll', () => { | ||
it('should return values by key', () => { | ||
expect(urlQueryParams.getAll('foo')).toEqual(['bar']); | ||
expect(urlQueryParams.getAll('q')).toEqual(['42']); | ||
expect(urlQueryParams.getAll('empty')).toEqual(['']); | ||
it('should return null if key is not exist', () => { | ||
expect(urlQueryParams.get('bad')).toBeNull(); | ||
}); | ||
}); | ||
it('should return all values for key', () => { | ||
urlQueryParams.append('foo', 'baz'); | ||
expect(urlQueryParams.getAll('foo')).toEqual(['bar', 'baz']); | ||
}); | ||
describe('getAll', () => { | ||
it('should return values by key', () => { | ||
expect(urlQueryParams.getAll('foo')).toEqual(['bar']); | ||
expect(urlQueryParams.getAll('q')).toEqual(['42']); | ||
expect(urlQueryParams.getAll('empty')).toEqual(['']); | ||
}); | ||
it('should return empty list if key is not exist', () => { | ||
expect(urlQueryParams.getAll('bad')).toEqual([]); | ||
}); | ||
}); | ||
it('should return all values for key', () => { | ||
urlQueryParams.append('foo', 'baz'); | ||
expect(urlQueryParams.getAll('foo')).toEqual(['bar', 'baz']); | ||
}); | ||
describe('has', () => { | ||
it('should return true if key is exist', () => { | ||
expect(urlQueryParams.has('foo')).toBeTruthy(); | ||
expect(urlQueryParams.has('q')).toBeTruthy(); | ||
expect(urlQueryParams.has('empty')).toBeTruthy(); | ||
it('should return empty list if key is not exist', () => { | ||
expect(urlQueryParams.getAll('bad')).toEqual([]); | ||
}); | ||
}); | ||
it('should return false if key is not exist', () => { | ||
expect(urlQueryParams.has('bad')).toBeFalsy(); | ||
}); | ||
}); | ||
describe('has', () => { | ||
it('should return true if key is exist', () => { | ||
expect(urlQueryParams.has('foo')).toBeTruthy(); | ||
expect(urlQueryParams.has('q')).toBeTruthy(); | ||
expect(urlQueryParams.has('empty')).toBeTruthy(); | ||
}); | ||
describe('keys', () => { | ||
it('should return iterator for keys', () => { | ||
const keys = urlQueryParams.keys(); | ||
expect(typeof keys[Symbol.iterator]).toEqual('function'); | ||
expect(keys.next()).toEqual({ done: false, value: 'foo' }); | ||
expect(keys.next()).toEqual({ done: false, value: 'q' }); | ||
expect(keys.next()).toEqual({ done: false, value: 'empty' }); | ||
expect(keys.next()).toEqual({ done: true, value: undefined }); | ||
it('should return false if key is not exist', () => { | ||
expect(urlQueryParams.has('bad')).toBeFalsy(); | ||
}); | ||
}); | ||
}); | ||
describe('set', () => { | ||
it('should set new params', () => { | ||
urlQueryParams.set('test', 'passed'); | ||
expect(urlQueryParams.getAll('test')).toEqual(['passed']); | ||
describe('keys', () => { | ||
it('should return iterator for keys', () => { | ||
const keys = urlQueryParams.keys(); | ||
expect(typeof keys[Symbol.iterator]).toEqual('function'); | ||
expect(keys.next()).toEqual({ done: false, value: 'foo' }); | ||
expect(keys.next()).toEqual({ done: false, value: 'q' }); | ||
expect(keys.next()).toEqual({ done: false, value: 'empty' }); | ||
expect(keys.next()).toEqual({ done: false, value: 'baz' }); | ||
expect(keys.next()).toEqual({ done: false, value: 'baz' }); | ||
expect(keys.next()).toEqual({ done: true, value: undefined }); | ||
}); | ||
}); | ||
it('should replace param with same key', () => { | ||
urlQueryParams.set('test', 'passed'); | ||
urlQueryParams.set('test', 'ok'); | ||
expect(urlQueryParams.getAll('test')).toEqual(['ok']); | ||
}); | ||
}); | ||
describe('set', () => { | ||
it('should set new params', () => { | ||
urlQueryParams.set('test', 'passed'); | ||
expect(urlQueryParams.getAll('test')).toEqual(['passed']); | ||
}); | ||
describe('sort', () => { | ||
it('should sort key/value params', () => { | ||
urlQueryParams.sort(); | ||
expect(urlQueryParams.toString()).toEqual('empty=&foo=bar&q=42'); | ||
const keys = urlQueryParams.keys(); | ||
expect(keys.next()).toEqual({ done: false, value: 'empty' }); | ||
expect(keys.next()).toEqual({ done: false, value: 'foo' }); | ||
expect(keys.next()).toEqual({ done: false, value: 'q' }); | ||
expect(keys.next()).toEqual({ done: true, value: undefined }); | ||
it('should replace param with same key', () => { | ||
urlQueryParams.set('test', 'passed'); | ||
urlQueryParams.set('test', 'ok'); | ||
expect(urlQueryParams.getAll('test')).toEqual(['ok']); | ||
}); | ||
}); | ||
}); | ||
describe('toObject', () => { | ||
it('should key/value object of the params', () => { | ||
expect(urlQueryParams.toObject()).toEqual({ foo: 'bar', q: '42', empty: '' }); | ||
describe('sort', () => { | ||
it('should sort key/value params', () => { | ||
urlQueryParams.sort(); | ||
expect(urlQueryParams.toString()).toEqual('baz=43&baz=44&empty=&foo=bar&q=42'); | ||
const keys = urlQueryParams.keys(); | ||
expect(keys.next()).toEqual({ done: false, value: 'baz' }); | ||
expect(keys.next()).toEqual({ done: false, value: 'baz' }); | ||
expect(keys.next()).toEqual({ done: false, value: 'empty' }); | ||
expect(keys.next()).toEqual({ done: false, value: 'foo' }); | ||
expect(keys.next()).toEqual({ done: false, value: 'q' }); | ||
expect(keys.next()).toEqual({ done: true, value: undefined }); | ||
}); | ||
}); | ||
it('should return a new object of params', () => { | ||
expect(urlQueryParams.toObject()).not.toBe(urlQueryParams.toObject()); | ||
}); | ||
}); | ||
describe('toObject', () => { | ||
it('should key/value object of the params', () => { | ||
expect(urlQueryParams.toObject()).toEqual({ foo: 'bar', q: '42', empty: '', baz: ['43', '44'] }); | ||
}); | ||
describe('toString', () => { | ||
it('encode params to string', () => { | ||
expect(urlQueryParams.toString()).toEqual('foo=bar&q=42&empty='); | ||
it('should return a new object of params', () => { | ||
expect(urlQueryParams.toObject()).not.toBe(urlQueryParams.toObject()); | ||
}); | ||
}); | ||
it('should encode reserved characters for keys & values', () => { | ||
expect(new URLQueryParams({ 'key;,/?:@&=+$': 'foo', bar: 'value;,/?:@&=+$' }).toString()) | ||
.toEqual('key%3B%2C%2F%3F%3A%40%26%3D%2B%24=foo&bar=value%3B%2C%2F%3F%3A%40%26%3D%2B%24'); | ||
}); | ||
describe('toString', () => { | ||
it('encode params to string', () => { | ||
expect(urlQueryParams.toString()).toEqual('foo=bar&q=42&empty=&baz=43&baz=44'); | ||
}); | ||
it('should encode number sign for keys & values', () => { | ||
expect(new URLQueryParams({ 'key#': 'foo', bar: 'value#' }).toString()) | ||
.toEqual('key%23=foo&bar=value%23'); | ||
}); | ||
it('should encode reserved characters for keys & values', () => { | ||
expect(new URLQueryParams({ 'key;,/?:@&=+$': 'foo', bar: 'value;,/?:@&=+$' }).toString()).toEqual( | ||
'key%3B%2C%2F%3F%3A%40%26%3D%2B%24=foo&bar=value%3B%2C%2F%3F%3A%40%26%3D%2B%24' | ||
); | ||
}); | ||
it('should encode additionals characters of RFC 3986 for keys & values', () => { | ||
expect(new URLQueryParams({ 'key with space': 'foo', bar: 'value with space' }).toString()) | ||
.toEqual('key%20with%20space=foo&bar=value%20with%20space'); | ||
}); | ||
it('should encode number sign for keys & values', () => { | ||
expect(new URLQueryParams({ 'key#': 'foo', bar: 'value#' }).toString()).toEqual('key%23=foo&bar=value%23'); | ||
}); | ||
it('should encode characters', () => { | ||
urlQueryParams = new URLQueryParams(); | ||
for (const char in encodeByChar) { | ||
if (!encodeByChar.hasOwnProperty(char)) { | ||
continue; | ||
} | ||
const encodedChar = encodeByChar[char]; | ||
urlQueryParams.set('test', char); | ||
expect(urlQueryParams.toString()).toEqual(`test=${encodedChar}`); | ||
} | ||
it('should encode additionals characters of RFC 3986 for keys & values', () => { | ||
expect(new URLQueryParams({ 'key with space': 'foo', bar: 'value with space' }).toString()).toEqual( | ||
'key%20with%20space=foo&bar=value%20with%20space' | ||
); | ||
}); | ||
it('should encode characters', () => { | ||
urlQueryParams = new URLQueryParams(); | ||
for (const char in encodeByChar) { | ||
if (!encodeByChar.hasOwnProperty(char)) { | ||
continue; | ||
} | ||
const encodedChar = encodeByChar[char]; | ||
urlQueryParams.set('test', char); | ||
expect(urlQueryParams.toString()).toEqual(`test=${encodedChar}`); | ||
} | ||
}); | ||
}); | ||
}); | ||
describe('values', () => { | ||
it('should return iterator for values', () => { | ||
const values = urlQueryParams.values(); | ||
expect(typeof values[Symbol.iterator]).toEqual('function'); | ||
expect(values.next()).toEqual({ done: false, value: 'bar' }); | ||
expect(values.next()).toEqual({ done: false, value: '42' }); | ||
expect(values.next()).toEqual({ done: false, value: '' }); | ||
expect(values.next()).toEqual({ done: true, value: undefined }); | ||
describe('values', () => { | ||
it('should return iterator for values', () => { | ||
const values = urlQueryParams.values(); | ||
expect(typeof values[Symbol.iterator]).toEqual('function'); | ||
expect(values.next()).toEqual({ done: false, value: 'bar' }); | ||
expect(values.next()).toEqual({ done: false, value: '42' }); | ||
expect(values.next()).toEqual({ done: false, value: '' }); | ||
expect(values.next()).toEqual({ done: false, value: '43' }); | ||
expect(values.next()).toEqual({ done: false, value: '44' }); | ||
expect(values.next()).toEqual({ done: true, value: undefined }); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -5,88 +5,94 @@ import encodeURIComponentRFC3986 from './encodeURIComponentRFC3986'; | ||
export const isURLQueryParamsInstance = (some: any): some is URLQueryParams => { | ||
return some instanceof URLQueryParams; | ||
} | ||
export const isURLQueryParamsInstance = (some: any): some is URLQueryParams => { | ||
return some instanceof URLQueryParams; | ||
}; | ||
export default class URLQueryParams implements URLSearchParams { | ||
private readonly _urlSearchParams: URLSearchParams; | ||
[Symbol.iterator]: () => IterableIterator<[string, string]> | ||
constructor(init?: InitType) { | ||
this._urlSearchParams = new URLSearchParams( | ||
isURLQueryParamsInstance(init) ? init._urlSearchParams : init, | ||
); | ||
this[Symbol.iterator] = this._urlSearchParams[Symbol.iterator].bind(this._urlSearchParams); | ||
} | ||
export default class URLQueryParams { | ||
append(name: string, value: string): void { | ||
return this._urlSearchParams.append(name, value); | ||
} | ||
private readonly _urlSearchParams: URLSearchParams; | ||
// @ts-ignore | ||
[Symbol.iterator]: () => IterableIterator<[string, string]>; | ||
// todo: appendMulti(init: InitType) {} | ||
constructor(init?: InitType) { | ||
this._urlSearchParams = new URLSearchParams(isURLQueryParamsInstance(init) ? init._urlSearchParams : init); | ||
this[Symbol.iterator] = this._urlSearchParams[Symbol.iterator].bind(this._urlSearchParams); | ||
} | ||
delete(name: string): void { | ||
return this._urlSearchParams.delete(name); | ||
} | ||
append(name: string, value: string): void { | ||
return this._urlSearchParams.append(name, value); | ||
} | ||
// todo: deleteMulti(names: string[]) {} | ||
// todo: appendMulti(init: InitType) {} | ||
entries(): IterableIterator<[string, string]> { | ||
return this._urlSearchParams.entries(); | ||
} | ||
delete(name: string): void { | ||
return this._urlSearchParams.delete(name); | ||
} | ||
forEach(callbackfn: (value: string, key: string, parent: URLQueryParams) => void, thisArg?: any): void { | ||
const self = this; | ||
const callback = thisArg ? callbackfn.bind(thisArg) : callbackfn; | ||
return this._urlSearchParams.forEach(function (value, key) { | ||
return callback(value, key, self); | ||
}); | ||
} | ||
// todo: deleteMulti(names: string[]) {} | ||
get(name: string): string | null { | ||
return this._urlSearchParams.get(name); | ||
} | ||
entries(): IterableIterator<[string, string]> { | ||
return this._urlSearchParams.entries(); | ||
} | ||
getAll(name: string): string[] { | ||
return this._urlSearchParams.getAll(name); | ||
} | ||
forEach(callbackfn: (value: string, key: string, parent: URLQueryParams) => void, thisArg?: any): void { | ||
const self = this; | ||
const callback = thisArg ? callbackfn.bind(thisArg) : callbackfn; | ||
return this._urlSearchParams.forEach(function (value, key) { | ||
return callback(value, key, self); | ||
}); | ||
} | ||
has(name: string): boolean { | ||
return this._urlSearchParams.has(name); | ||
} | ||
get(name: string): string | null { | ||
return this._urlSearchParams.get(name); | ||
} | ||
keys(): IterableIterator<string> { | ||
return this._urlSearchParams.keys(); | ||
} | ||
getAll(name: string): string[] { | ||
return this._urlSearchParams.getAll(name); | ||
} | ||
set(name: string, value: string): void { | ||
return this._urlSearchParams.set(name, value); | ||
} | ||
has(name: string): boolean { | ||
return this._urlSearchParams.has(name); | ||
} | ||
// todo: setMulti(init: InitType) {} | ||
keys(): IterableIterator<string> { | ||
return this._urlSearchParams.keys(); | ||
} | ||
sort(): void { | ||
return this._urlSearchParams.sort(); | ||
} | ||
set(name: string, value: string): void { | ||
return this._urlSearchParams.set(name, value); | ||
} | ||
toObject(): Record<string, string> { | ||
const obj: Record<string, string> = {}; | ||
this.forEach((value, key) => { | ||
obj[key] = value; | ||
}); | ||
return obj; | ||
} | ||
// todo: setMulti(init: InitType) {} | ||
toString(): string { | ||
const params: string[] = []; | ||
this.forEach((value, key) => { | ||
params.push(`${encodeURIComponentRFC3986(key)}=${encodeURIComponentRFC3986(value)}`); | ||
}); | ||
return params.join('&'); | ||
} | ||
sort(): void { | ||
return this._urlSearchParams.sort(); | ||
} | ||
values(): IterableIterator<string> { | ||
return this._urlSearchParams.values(); | ||
} | ||
toObject(): Record<string, string | string[]> { | ||
const obj: Record<string, string | string[]> = {}; | ||
this.forEach((value, key) => { | ||
if (key in obj) { | ||
if (!Array.isArray(obj[key])) { | ||
obj[key] = [obj[key] as string]; | ||
} | ||
(obj[key] as string[]).push(value); | ||
} else { | ||
obj[key] = value; | ||
} | ||
}); | ||
return obj; | ||
} | ||
toString(): string { | ||
const params: string[] = []; | ||
this.forEach((value, key) => { | ||
params.push(`${encodeURIComponentRFC3986(key)}=${encodeURIComponentRFC3986(value)}`); | ||
}); | ||
return params.join('&'); | ||
} | ||
values(): IterableIterator<string> { | ||
return this._urlSearchParams.values(); | ||
} | ||
} |
@@ -7,3 +7,3 @@ { | ||
"declaration": true, | ||
"target": "es5", | ||
"target": "ES2015", | ||
"downlevelIteration": false, | ||
@@ -10,0 +10,0 @@ "allowJs": true, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32117
748
27