aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils/urls.js
diff options
context:
space:
mode:
authorSam Potts <sam@potts.es>2018-06-13 00:02:55 +1000
committerSam Potts <sam@potts.es>2018-06-13 00:02:55 +1000
commit392dfd024c505f5ae1bbb2f0d3e0793c251a1f35 (patch)
treeaedb56d3945eaa10bf74e61902e16c08fd24914a /src/js/utils/urls.js
parent840e31a693462e7ed9f7644a13a0187d9e9d93a9 (diff)
downloadplyr-392dfd024c505f5ae1bbb2f0d3e0793c251a1f35.tar.lz
plyr-392dfd024c505f5ae1bbb2f0d3e0793c251a1f35.tar.xz
plyr-392dfd024c505f5ae1bbb2f0d3e0793c251a1f35.zip
Utils broken down into seperate files and exports
Diffstat (limited to 'src/js/utils/urls.js')
-rw-r--r--src/js/utils/urls.js44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/js/utils/urls.js b/src/js/utils/urls.js
new file mode 100644
index 00000000..28323a1c
--- /dev/null
+++ b/src/js/utils/urls.js
@@ -0,0 +1,44 @@
+// ==========================================================================
+// URL utils
+// ==========================================================================
+
+import is from './is';
+
+/**
+ * Parse a string to a URL object
+ * @param {string} input - the URL to be parsed
+ * @param {boolean} safe - failsafe parsing
+ */
+export function parseUrl(input, safe = true) {
+ let url = input;
+
+ if (safe) {
+ const parser = document.createElement('a');
+ parser.href = url;
+ url = parser.href;
+ }
+
+ try {
+ return new URL(url);
+ } catch (e) {
+ return null;
+ }
+}
+
+// Convert object to URLSearchParams
+export function buildUrlParams(input) {
+ if (!is.object(input)) {
+ return '';
+ }
+
+ const params = new URLSearchParams();
+
+ Object.entries(input).forEach(([
+ key,
+ value,
+ ]) => {
+ params.set(key, value);
+ });
+
+ return params;
+}