You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
713 B
35 lines
713 B
'use strict'
|
|
|
|
const { URL } = require('url')
|
|
|
|
const BASE_URL = 'http://localhost'
|
|
|
|
/**
|
|
* Parse URL
|
|
*
|
|
* @param {(Object|String)} url
|
|
* @param {Object} [query]
|
|
* @return {URL}
|
|
*/
|
|
module.exports = function parseURL (url, query) {
|
|
const result = typeof url === 'object'
|
|
? Object.assign(new URL(BASE_URL), url)
|
|
: new URL(url, BASE_URL)
|
|
|
|
const merged = Object.assign({}, url.query, query)
|
|
Object.keys(merged).forEach(key => {
|
|
const value = merged[key]
|
|
|
|
if (Array.isArray(value)) {
|
|
result.searchParams.delete(key)
|
|
value.forEach(param => {
|
|
result.searchParams.append(key, param)
|
|
})
|
|
} else {
|
|
result.searchParams.set(key, value)
|
|
}
|
|
})
|
|
|
|
return result
|
|
}
|