-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* @author David Tomaschik [[email protected]] | ||
* @copyright Google LLC 2024 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation.mjs"; | ||
|
||
/** | ||
* Extract a URI to JSON Operation | ||
*/ | ||
class ExtractURI extends Operation { | ||
/** | ||
* ExtractURI Constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "Extract URI"; | ||
this.module = "URL"; | ||
this.description = "Extract components of URI to JSON for further processing."; | ||
this.infoURL = "https://wikipedia.org/wiki/Uniform_Resource_Identifier"; | ||
this.inputType = "string"; | ||
this.outputType = "JSON"; | ||
this.args = []; | ||
} | ||
|
||
/** | ||
* @param {string} input | ||
* @param {Object[]} args | ||
* @returns {JSON} | ||
*/ | ||
run(input, args) { | ||
const uri = new URL(input); | ||
const pieces = {}; | ||
// Straight copy some attributes | ||
['protocol', 'hostname', 'port', 'username', 'password', 'pathname', 'hash' | ||
].forEach((name) => { | ||
if (uri[name]) pieces[name] = uri[name]; | ||
}); | ||
// Now handle query params | ||
const params = uri.searchParams; | ||
if (params.size) { | ||
pieces['query'] = {}; | ||
for (const name of params.keys()) { | ||
const values = params.getAll(name); | ||
if (values.length > 1) { | ||
pieces['query'][name] = values; | ||
} else { | ||
pieces['query'][name] = values[0]; | ||
} | ||
} | ||
} | ||
return pieces; | ||
} | ||
}; | ||
|
||
export default ExtractURI; |