| interface URLSearchParams extends Iterable<[name: string, value: string]> { | ||
| readonly size: number | ||
| append(name: string, value: string): void | ||
| delete(name: string, value?: string): void | ||
| get(name: string): string | undefined | ||
| getAll(name: string): string[] | ||
| has(name: string, value?: string): boolean | ||
| set(name: string, value: string): void | ||
| toString(): string | ||
| toJSON(): string | ||
| } | ||
| declare class URLSearchParams { | ||
| constructor( | ||
| init: string | Record<string, string> | Iterable<[string, string]> | ||
| ) | ||
| } | ||
| export = URLSearchParams |
| module.exports = class URLSearchParams { | ||
| static _urls = new WeakMap() | ||
| // https://url.spec.whatwg.org/#dom-urlsearchparams-urlsearchparams | ||
| constructor(init, url = null) { | ||
| this._params = new Map() | ||
| if (url) URLSearchParams._urls.set(this, url) | ||
| if (typeof init === 'string') { | ||
| this._parse(init) | ||
| } else if (init) { | ||
| for (const [name, value] of typeof init[Symbol.iterator] === 'function' | ||
| ? init | ||
| : Object.entries(init)) { | ||
| this.append(name, value) | ||
| } | ||
| } | ||
| } | ||
| // https://url.spec.whatwg.org/#dom-urlsearchparams-size | ||
| get size() { | ||
| return this._params.length | ||
| } | ||
| // https://url.spec.whatwg.org/#dom-urlsearchparams-append | ||
| append(name, value = null) { | ||
| if (value === null) return | ||
| let list = this._params.get(name) | ||
| if (list === undefined) { | ||
| list = [] | ||
| this._params.set(name, list) | ||
| } | ||
| list.push(value) | ||
| this._update() | ||
| } | ||
| // https://url.spec.whatwg.org/#dom-urlsearchparams-delete | ||
| delete(name, value = null) { | ||
| if (value === null) this._params.delete(name) | ||
| else { | ||
| let list = this._params.get(name) | ||
| if (list === undefined) return | ||
| list = list.filter((found) => found !== value) | ||
| if (list.length === 0) this._params.delete(name) | ||
| else this._params.set(name, list) | ||
| } | ||
| this._update() | ||
| } | ||
| // https://url.spec.whatwg.org/#dom-urlsearchparams-get | ||
| get(name) { | ||
| const list = this._params.get(name) | ||
| if (list === undefined) return null | ||
| return list[0] | ||
| } | ||
| // https://url.spec.whatwg.org/#dom-urlsearchparams-getall | ||
| getAll(name) { | ||
| const list = this._params.get(name) | ||
| if (list === undefined) return [] | ||
| return Array.from(list) | ||
| } | ||
| // https://url.spec.whatwg.org/#dom-urlsearchparams-has | ||
| has(name, value = null) { | ||
| const list = this._params.get(name) | ||
| if (list === undefined) return false | ||
| if (value === null) return true | ||
| return list.includes(value) | ||
| } | ||
| // https://url.spec.whatwg.org/#dom-urlsearchparams-set | ||
| set(name, value = null) { | ||
| if (value === null) this._params.delete(name) | ||
| else this._params.set(name, [value]) | ||
| this._update() | ||
| } | ||
| toString() { | ||
| return this._serialize() | ||
| } | ||
| toJSON() { | ||
| return [...this] | ||
| } | ||
| *[Symbol.iterator]() { | ||
| for (const [name, values] of this._params) { | ||
| for (const value of values) yield [name, value] | ||
| } | ||
| } | ||
| [Symbol.for('bare.inspect')]() { | ||
| const object = { | ||
| __proto__: { constructor: URLSearchParams } | ||
| } | ||
| for (const [name, values] of this._params) { | ||
| if (values.length === 1) object[name] = values[0] | ||
| else object[name] = values | ||
| } | ||
| return object | ||
| } | ||
| // https://url.spec.whatwg.org/#concept-urlsearchparams-update | ||
| _update() { | ||
| const url = URLSearchParams._urls.get(this) | ||
| if (url === undefined) return | ||
| url.search = this._serialize() | ||
| } | ||
| // https://url.spec.whatwg.org/#concept-urlencoded-parser | ||
| _parse(input) { | ||
| if (input[0] === '?') input = input.substring(1) | ||
| this._params = new Map() | ||
| for (const sequence of input.split('&')) { | ||
| if (sequence.length === 0) continue | ||
| let i = sequence.indexOf('=') | ||
| if (i === -1) i = sequence.length | ||
| const name = sequence.substring(0, i) | ||
| const value = sequence.substring(i + 1, sequence.length) | ||
| let list = this._params.get(name) | ||
| if (list === undefined) { | ||
| list = [] | ||
| this._params.set(name, list) | ||
| } | ||
| list.push(value) | ||
| } | ||
| } | ||
| // https://url.spec.whatwg.org/#concept-urlencoded-serializer | ||
| _serialize() { | ||
| let output = '' | ||
| for (let [name, values] of this._params) { | ||
| name = encodeURIComponent(name) | ||
| for (const value of values) { | ||
| if (output) output += '&' | ||
| output += name + '=' + encodeURIComponent(value) | ||
| } | ||
| } | ||
| return output | ||
| } | ||
| } |
+3
-0
| import * as url from '.' | ||
| type URLConstructor = typeof url.URL | ||
| type URLSearchParamsConstructor = typeof url.URLSearchParams | ||
| declare global { | ||
| type URL = url.URL | ||
| type URLSearchParams = url.URLSearchParams | ||
| const URL: URLConstructor | ||
| const URLSearchParams: URLSearchParamsConstructor | ||
| } |
+1
-0
| global.URL = require('.') | ||
| global.URLSearchParams = require('./lib/url-search-params') |
+3
-1
| import URLError from './lib/errors' | ||
| import URLSearchParams from './lib/url-search-params' | ||
@@ -13,2 +14,3 @@ interface URL { | ||
| search: string | ||
| searchParams: URLSearchParams | ||
| hash: string | ||
@@ -35,5 +37,5 @@ | ||
| export { URL, type URLError, URLError as errors } | ||
| export { URL, type URLError, URLError as errors, URLSearchParams } | ||
| } | ||
| export = URL |
+25
-8
| const path = require('bare-path') | ||
| const binding = require('./binding') | ||
| const errors = require('./lib/errors') | ||
| const URLSearchParams = require('./lib/url-search-params') | ||
@@ -17,5 +18,5 @@ const kind = Symbol.for('bare.url.kind') | ||
| input = `${input}` | ||
| input = String(input) | ||
| if (base !== undefined) base = `${base}` | ||
| if (base !== undefined) base = String(base) | ||
@@ -25,2 +26,4 @@ this._components = new Uint32Array(8) | ||
| this._parse(input, base, opts.throw !== false) | ||
| if (this._href) this._params = new URLSearchParams(this.search, this) | ||
| } | ||
@@ -40,2 +43,4 @@ | ||
| this._update(value) | ||
| this._params._parse(this.search) | ||
| } | ||
@@ -201,4 +206,12 @@ | ||
| ) | ||
| this._params._parse(this.search) | ||
| } | ||
| // https://url.spec.whatwg.org/#dom-url-searchparams | ||
| get searchParams() { | ||
| return this._params | ||
| } | ||
| // https://url.spec.whatwg.org/#dom-url-hash | ||
@@ -237,2 +250,3 @@ | ||
| search: this.search, | ||
| searchParams: this.searchParams, | ||
| hash: this.hash | ||
@@ -250,6 +264,6 @@ } | ||
| _parse(href, base, shouldThrow) { | ||
| _parse(input, base, shouldThrow) { | ||
| try { | ||
| this._href = binding.parse( | ||
| String(href), | ||
| String(input), | ||
| base ? String(base) : null, | ||
@@ -266,5 +280,5 @@ this._components, | ||
| _update(href) { | ||
| _update(input) { | ||
| try { | ||
| this._parse(href, null, true) | ||
| this._parse(input, null, true) | ||
| } catch (err) { | ||
@@ -288,3 +302,4 @@ if (err instanceof TypeError) throw err | ||
| exports.URL = URL // For Node.js compatibility | ||
| exports.URL = URL | ||
| exports.URLSearchParams = URLSearchParams | ||
@@ -301,7 +316,9 @@ exports.errors = errors | ||
| // https://url.spec.whatwg.org/#dom-url-parse | ||
| exports.parse = function parse(input, base) { | ||
| const url = new URL(input, base, { throw: false }) | ||
| return url.href ? url : null | ||
| return url._href ? url : null | ||
| } | ||
| // https://url.spec.whatwg.org/#dom-url-canparse | ||
| exports.canParse = function canParse(input, base) { | ||
@@ -308,0 +325,0 @@ return binding.canParse(String(input), base ? String(base) : null) |
+1
-1
| { | ||
| "name": "bare-url", | ||
| "version": "2.1.6", | ||
| "version": "2.2.0", | ||
| "description": "WHATWG URL implementation for JavaScript", | ||
@@ -5,0 +5,0 @@ "exports": { |
1210534
0.43%27
8%522
44.6%