From 92407c83ea69bf123cb084de55ac78c923e44698 Mon Sep 17 00:00:00 2001 From: Michael Wuergler Date: Sun, 18 Feb 2024 22:16:23 +0100 Subject: [PATCH] feat!: favor undefined over null when ids or services are unknown --- __tests__/dailymotion.test.js | 2 +- __tests__/google-redirect.test.js | 6 +++--- __tests__/index.test.js | 6 +++--- __tests__/microsoft-stream.test.js | 2 +- __tests__/tiktok.test.js | 2 +- __tests__/utils/get-src.test.js | 6 +++--- __tests__/utils/sanitize-url.test.js | 2 +- __tests__/videopress.test.js | 2 +- __tests__/vimeo.test.js | 2 +- __tests__/vine.test.js | 2 +- __tests__/youtube.test.js | 7 +++---- index.d.ts | 4 ++-- src/index.js | 4 ++-- 13 files changed, 23 insertions(+), 24 deletions(-) diff --git a/__tests__/dailymotion.test.js b/__tests__/dailymotion.test.js index a642e04..9e9ae40 100644 --- a/__tests__/dailymotion.test.js +++ b/__tests__/dailymotion.test.js @@ -28,11 +28,11 @@ describe('Dailymotion', () => { test('Dailymotion returns the service', () => { expect(fn('http://www.dailymotion.com/video/1234').service).toBe('dailymotion'); - expect(fn('http://www.dailymotion.com/video/1234').id).toBe('1234'); }); test('Dailymotion short link', () => { expect(fn('http://dai.ly/x2no31b').id).toBe('x2no31b'); + expect(fn('http://dai.ly/x2no31b').service).toBe('dailymotion'); }); test('Dailymotion dynamic id', () => { diff --git a/__tests__/google-redirect.test.js b/__tests__/google-redirect.test.js index c587a68..6dac25d 100644 --- a/__tests__/google-redirect.test.js +++ b/__tests__/google-redirect.test.js @@ -43,9 +43,9 @@ describe('Google Redirects', () => { expect(fn(url).service).toBe('microsoftstream'); }); - test('google link returns null as id and service if missing url parameter', () => { + test('google link returns undefined as id and service if missing url parameter', () => { const url = 'https://www.google.cz/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0ahUKEwiz9P3Aw5DVAhUDZVAKHcegCi8QuAIINDAB&usg=AFQjCNG0kTPdL8nC6zCi2QoZ1KVeTXH-pw'; - expect(fn(url).id).toBe(null); - expect(fn(url).service).toBe(null); + expect(fn(url).id).toBeUndefined(); + expect(fn(url).service).toBeUndefined(); }); }); diff --git a/__tests__/index.test.js b/__tests__/index.test.js index e6e5f65..d0695f0 100644 --- a/__tests__/index.test.js +++ b/__tests__/index.test.js @@ -7,10 +7,10 @@ test('expects a string', () => { }).toThrow(/get-video-id expects a string/); }); -test('returns null as id and service', () => { +test('returns undefined as id and service', () => { const expected = { - id: null, - service: null, + id: undefined, + service: undefined, }; expect(fn('foo')).toMatchObject(expected); expect(fn(' { test('microsoft stream links returns undefined id if id missing', () => { const object = fn('https://web.microsoftstream.com/video'); - expect(object.id).toBe(undefined); + expect(object.id).toBeUndefined(); expect(object.service).toBe('microsoftstream'); }); }); diff --git a/__tests__/tiktok.test.js b/__tests__/tiktok.test.js index 6f92f10..5d05ded 100644 --- a/__tests__/tiktok.test.js +++ b/__tests__/tiktok.test.js @@ -20,7 +20,7 @@ describe('TikTok', () => { test('returns undefined for unknown video ids', () => { const actual = fn('http://www.tiktok.com'); - expect(actual.id).toBe(undefined); + expect(actual.id).toBeUndefined(); expect(actual.service).toBe('tiktok'); }); }); diff --git a/__tests__/utils/get-src.test.js b/__tests__/utils/get-src.test.js index 07ca491..9e1cc8c 100644 --- a/__tests__/utils/get-src.test.js +++ b/__tests__/utils/get-src.test.js @@ -28,11 +28,11 @@ describe('get-src', () => { }); test('returns undefined when no src= is found', () => { - expect(fn('hello')).toBe(undefined); - expect(fn(' { - expect(fn(' src=\'noop\' ')).toBe(undefined); + expect(fn(' src=\'noop\' ')).toBeUndefined(); }); }); diff --git a/__tests__/utils/sanitize-url.test.js b/__tests__/utils/sanitize-url.test.js index c5e84c9..2d3eeb6 100644 --- a/__tests__/utils/sanitize-url.test.js +++ b/__tests__/utils/sanitize-url.test.js @@ -26,7 +26,7 @@ describe('sanitize-url', () => { expect(sanitizeUrl(iframeHtml)).toBe('https://example.com'); }); - test('returns an empty string when getSrc returns null for an iframe', () => { + test('returns an empty string when getSrc returns undefined for an iframe', () => { const iframeHtml = ''; expect(sanitizeUrl(iframeHtml)).toBe(''); }); diff --git a/__tests__/videopress.test.js b/__tests__/videopress.test.js index 38967f2..5e94bdd 100644 --- a/__tests__/videopress.test.js +++ b/__tests__/videopress.test.js @@ -30,7 +30,7 @@ describe('Videopress', () => { test('videopress links returns undefined id if id missing', () => { const object = fn('https://videopress.com'); - expect(object.id).toBe(undefined); + expect(object.id).toBeUndefined(); expect(object.service).toBe('videopress'); }); }); diff --git a/__tests__/vimeo.test.js b/__tests__/vimeo.test.js index dcbbea6..0b7ec02 100644 --- a/__tests__/vimeo.test.js +++ b/__tests__/vimeo.test.js @@ -81,7 +81,7 @@ describe('Vimeo', () => { test('vimeo links returns undefined id if id missing', () => { const object = fn('https://www.vimeo.co'); - expect(object.id).toBe(undefined); + expect(object.id).toBeUndefined(); expect(object.service).toBe('vimeo'); }); diff --git a/__tests__/vine.test.js b/__tests__/vine.test.js index 9670101..8f8b20b 100644 --- a/__tests__/vine.test.js +++ b/__tests__/vine.test.js @@ -42,7 +42,7 @@ describe('Vine', () => { test('vine links returns undefined id if id missing', () => { const object = fn('https://vine.co'); - expect(object.id).toBe(undefined); + expect(object.id).toBeUndefined(); expect(object.service).toBe('vine'); }); }); diff --git a/__tests__/youtube.test.js b/__tests__/youtube.test.js index 23fcd6b..5f6c84a 100644 --- a/__tests__/youtube.test.js +++ b/__tests__/youtube.test.js @@ -186,23 +186,22 @@ describe('Youtube', () => { expect(fn('https://www.youtube.com/live/ABC1230').id).toBe('ABC1230'); expect(fn('www.youtube-nocookie.com/live/ABC12301?feature=share').id).toBe('ABC12301'); expect(fn('http://www.youtube.com/live/ABC12302?feature=share').id).toBe('ABC12302'); - expect(fn('http://www.youtube.com/live/ABC12302?feature=share').service).toBe('youtube'); }); test('youtube links returns undefined id if id missing', () => { const object = fn('https://www.youtube.com'); - expect(object.id).toBe(undefined); + expect(object.id).toBeUndefined(); expect(object.service).toBe('youtube'); }); - test('removes time hash at end of string ', () => { + test('extracts ids from urls with time hashes', () => { expect(fn('https://www.youtube.com/watch?v=G-3YxlZIhus#t=0m10s').id).toBe('G-3YxlZIhus'); expect(fn('http://www.youtube.com/watch?v=G-3YxlZIhus#t=0m10s').id).toBe('G-3YxlZIhus'); expect(fn('http://www.youtube.com/watch?v=G-3YxlZIhus#t=0m10s').service).toBe('youtube'); }); - test('removes trailing parameters from youtube urls', () => { + test('extracts ids from urls with trailing parameters', () => { expect(fn('http://youtu.be/G-3YxlZIhus&feature=channel').id).toBe('G-3YxlZIhus'); expect(fn('http://youtube.com/vi/G-3YxlZIhus&feature=channel').id).toBe('G-3YxlZIhus'); expect(fn('http://youtube.com/vi/G-3YxlZIhus&feature=channel').service).toBe('youtube'); diff --git a/index.d.ts b/index.d.ts index aeb6531..565c185 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,6 +1,6 @@ export default function getVideoId( url: string ): { - id: string | null; - service: 'youtube' | 'vimeo' | 'vine' | 'videopress' | 'microsoftstream' | 'tiktok' | 'dailymotion' | null; + id: string | undefined; + service: 'youtube' | 'vimeo' | 'vine' | 'videopress' | 'microsoftstream' | 'tiktok' | 'dailymotion' | undefined; }; diff --git a/src/index.js b/src/index.js index e4efdb1..88753db 100644 --- a/src/index.js +++ b/src/index.js @@ -22,8 +22,8 @@ function getVideoId(urlString) { const url = extractGoogleRedirectionUrl(string_); let metadata = { - id: null, - service: null, + id: undefined, + service: undefined, }; if (/youtube|youtu\.be|y2u\.be|i.ytimg\./.test(url)) {