Skip to content

Commit

Permalink
Merge pull request #52 from 4lejandrito/improve-typings
Browse files Browse the repository at this point in the history
Returning null for unrecognized urls
  • Loading branch information
radiovisual authored Jan 30, 2021
2 parents 83723c8 + 2161f23 commit 3da56df
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
10 changes: 4 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export default function getVideoId(
url: string
):
| {}
| {
id: string;
service: "youtube" | "vimeo" | "vine" | "videopress";
};
): {
id: string | null;
service: "youtube" | "vimeo" | "vine" | "videopress" | null;
};
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ function getVideoId(videoStr) {
// remove any leading `www.`
str = str.replace('/www.', '/');

let metadata = {};
let metadata = {
id: null,
service: null,
};

// Try to handle google redirection uri
if (/\/\/google/.test(str)) {
Expand Down
12 changes: 7 additions & 5 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ test('expects a string', (t) => {
}, 'get-video-id expects a string');
});

test('returns empty object', (t) => {
test('returns null as id and service', (t) => {
const notFound = fn('foo');
t.is(typeof notFound, 'object');
t.is(Object.keys(notFound).length, 0);
t.is(notFound.id, null);
t.is(notFound.service, null);
});

/**
Expand Down Expand Up @@ -339,6 +339,8 @@ test('handles google redirection to vimeo', (t) => {
t.is(fn(url).service, 'vimeo');
});

test('google link returns empty object if missing url parameter', (t) => {
t.is(Object.keys(fn('https://www.google.cz/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0ahUKEwiz9P3Aw5DVAhUDZVAKHcegCi8QuAIINDAB&usg=AFQjCNG0kTPdL8nC6zCi2QoZ1KVeTXH-pw')).length, 0);
test('google link returns null as id and service if missing url parameter', (t) => {
const url = 'https://www.google.cz/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0ahUKEwiz9P3Aw5DVAhUDZVAKHcegCi8QuAIINDAB&usg=AFQjCNG0kTPdL8nC6zCi2QoZ1KVeTXH-pw';
t.is(fn(url).id, null);
t.is(fn(url).service, null);
});

0 comments on commit 3da56df

Please sign in to comment.