Skip to content

Commit

Permalink
New operation: Extract URI to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Matir committed Oct 28, 2024
1 parent 3822c6c commit e86bdd9
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/core/operations/ExtractURI.mjs
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;

0 comments on commit e86bdd9

Please sign in to comment.