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.
		
		
		
		
		
			
		
			
				
					
					
						
							38 lines
						
					
					
						
							855 B
						
					
					
				
			
		
		
	
	
							38 lines
						
					
					
						
							855 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) {
 | |
|   if ((typeof url === 'string' || Object.prototype.toString.call(url) === '[object String]') && url.startsWith('//')) {
 | |
|     url = BASE_URL + url
 | |
|   }
 | |
|   const result = typeof url === 'object'
 | |
|     ? Object.assign(new URL(BASE_URL), url)
 | |
|     : new URL(url, BASE_URL)
 | |
| 
 | |
|   const merged = Object.assign({}, url.query, query)
 | |
|   for (const key in merged) {
 | |
|     const value = merged[key]
 | |
| 
 | |
|     if (Array.isArray(value)) {
 | |
|       result.searchParams.delete(key)
 | |
|       for (const param of value) {
 | |
|         result.searchParams.append(key, param)
 | |
|       }
 | |
|     } else {
 | |
|       result.searchParams.set(key, value)
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   return result
 | |
| }
 |