From 26fa5abcaa1d3141da280de43768dc7d80bc36eb Mon Sep 17 00:00:00 2001
From: Dheeraj <dheerajkawatra@gmail.com>
Date: Sun, 23 Oct 2022 02:33:01 +0530
Subject: [PATCH] feat: Loom video support

---
 __tests__/loom.test.js | 28 ++++++++++++++++++++++++++++
 readme.md              | 14 ++++++++++++++
 src/index.js           |  6 ++++++
 src/loom.js            | 16 ++++++++++++++++
 4 files changed, 64 insertions(+)
 create mode 100644 __tests__/loom.test.js
 create mode 100644 src/loom.js

diff --git a/__tests__/loom.test.js b/__tests__/loom.test.js
new file mode 100644
index 0000000..ad66960
--- /dev/null
+++ b/__tests__/loom.test.js
@@ -0,0 +1,28 @@
+/* eslint max-len: 0 */
+import fn from '../src/index.js';
+
+/**
+ *  Loom should be able to find these patterns:
+ *
+ *  Urls:
+ *  https://www.loom.com/share/7c7ced4911904070a5627374ccd84e8c
+ *  https://www.loom.com/share/5bbdeb480ba84e65b1b3de8c190e2003?source=embed&t=20
+ * 	https://www.loom.com/embed/7c7ced4911904070a5627374ccd84e8c
+ */
+describe('Loom', () => {
+	test('Loom basic link', () => {
+		expect(fn('https://www.loom.com/share/7c7ced4911904070a5627374ccd84e8c').id).toBe('7c7ced4911904070a5627374ccd84e8c');
+		expect(fn('https://www.loom.com/share/5bbdeb480ba84e65b1b3de8c190e2003?source=embed&t=20').id).toBe('5bbdeb480ba84e65b1b3de8c190e2003');
+	});
+
+	test('Loom embed', () => {
+		expect(fn('<div style="position: relative; padding-bottom: 62.5%; height: 0;"><iframe src="https://www.loom.com/embed/7c7ced4911904070a5627374ccd84e8c" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe></div>').id).toBe('7c7ced4911904070a5627374ccd84e8c');
+	});
+
+	test('returns undefined for unknown video ids', () => {
+		const actual = fn('https://www.loom.com');
+		expect(actual.id).toBe(undefined);
+		expect(actual.service).toBe('loom');
+	});
+});
+
diff --git a/readme.md b/readme.md
index 084340f..9392684 100644
--- a/readme.md
+++ b/readme.md
@@ -274,6 +274,20 @@ http://dai.ly/*
 **:warning: Unsupported Dailymotion urls**
  *  Channel id urls: `http://www.dailymotion.com/hub/*_title`
 
+ ### Loom
+
+**Loom urls**
+```
+https://www.loom.com/share/7c7ced4911904070a5627374ccd84e8c
+https://www.loom.com/share/5bbdeb480ba84e65b1b3de8c190e2003?source=embed&t=20
+```
+
+**Loom iframe**
+```
+<div style="position: relative; padding-bottom: 62.5%; height: 0;"><iframe src="https://www.loom.com/embed/7c7ced4911904070a5627374ccd84e8c" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe></div>
+```
+
+
 ## Contributing
 
 If you discover a url pattern that is not covered by this module, please [open an issue](https://github.com/radiovisual/get-video-id/issues) to report it, or [submit a Pull Request](https://github.com/radiovisual/get-video-id/pull/new/master). For any submitted pull requests, please ensure that you include unit test(s) to fully cover your code contribution(s).
diff --git a/src/index.js b/src/index.js
index d521096..fc8d0c2 100644
--- a/src/index.js
+++ b/src/index.js
@@ -5,6 +5,7 @@ import videopress from './videopress.js';
 import microsoftStream from './microsoftstream.js';
 import tiktok from './tiktok.js';
 import dailymotion from './dailymotion.js';
+import loom from './loom.js';
 import getSrc from './utils/get-src.js';
 
 /**
@@ -84,6 +85,11 @@ function getVideoId(urlString) {
 			id: dailymotion(string_),
 			service: 'dailymotion',
 		};
+	} else if (/loom\.com/.test(string_)) {
+		metadata = {
+			id: loom(string_),
+			service: 'loom',
+		};
 	}
 
 	return metadata;
diff --git a/src/loom.js b/src/loom.js
new file mode 100644
index 0000000..4e4d5f1
--- /dev/null
+++ b/src/loom.js
@@ -0,0 +1,16 @@
+/**
+ * Get the loom id.
+ * @param {string} urlString - the url from which you want to extract the id
+ * @returns {string|undefined}
+ */
+export default function loom(urlString) {
+	// Parse basic url and embeds
+	const basicReg
+		= /(https?:\/\/)?(www\.)?loom\.com\/?(.*\/)?([\d)([a-z]{32})\??.*/;
+	const basicParsed = urlString.match(basicReg);
+	if (basicParsed && basicParsed.length === 5) {
+		return basicParsed[4];
+	}
+
+	return undefined;
+}