Skip to content

Commit

Permalink
feat!: favor undefined over null when ids or services are unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
radiovisual committed Feb 18, 2024
1 parent fdad334 commit 92407c8
Show file tree
Hide file tree
Showing 13 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion __tests__/dailymotion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 3 additions & 3 deletions __tests__/google-redirect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
6 changes: 3 additions & 3 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<iframe')).toMatchObject(expected);
Expand Down
2 changes: 1 addition & 1 deletion __tests__/microsoft-stream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Microsoft Stream', () => {

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');
});
});
2 changes: 1 addition & 1 deletion __tests__/tiktok.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
Expand Down
6 changes: 3 additions & 3 deletions __tests__/utils/get-src.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ describe('get-src', () => {
});

test('returns undefined when no src= is found', () => {
expect(fn('hello')).toBe(undefined);
expect(fn('<iframe')).toBe(undefined);
expect(fn('hello')).toBeUndefined();
expect(fn('<iframe')).toBeUndefined();
});

test('single quotes return undefined', () => {
expect(fn(' src=\'noop\' ')).toBe(undefined);
expect(fn(' src=\'noop\' ')).toBeUndefined();
});
});
2 changes: 1 addition & 1 deletion __tests__/utils/sanitize-url.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<iframe></iframe>';
expect(sanitizeUrl(iframeHtml)).toBe('');
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/videopress.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
2 changes: 1 addition & 1 deletion __tests__/vimeo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

Expand Down
2 changes: 1 addition & 1 deletion __tests__/vine.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
7 changes: 3 additions & 4 deletions __tests__/youtube.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
};
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 92407c8

Please sign in to comment.