Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CJS export #109

Merged
merged 10 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ jobs:
matrix:
node-version:
- 16
- 14
- 18
- latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v1
if: matrix.node-version == 14
- uses: codecov/codecov-action@v3
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
10 changes: 10 additions & 0 deletions __tests__/builds/cjs-module.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* istanbul ignore file */
/* eslint-disable unicorn/prefer-module */
const getVideoId = require('../../dist/get-video-id.js');

describe('bundled CJS module', () => {
test('has the expected API', () => {
expect(typeof getVideoId).toBe('function');
expect(getVideoId('https://www.youtube.com/watch?v=1234').id).toBe('1234');
});
});
2 changes: 1 addition & 1 deletion __tests__/builds/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<span id="video-id"></span>

<!-- Load getVideoId UMD module -->
<script src="../../dist/get-video-id.js"></script>
<script src="../../dist/get-video-id.umd.js"></script>

<!-- Now we are free to use getVideoId in the browser -->
<script>
Expand Down
2 changes: 1 addition & 1 deletion __tests__/builds/umd-module.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* istanbul ignore file */
/* eslint-disable unicorn/prefer-module */
const getVideoId = require('../../dist/get-video-id.js');
const getVideoId = require('../../dist/get-video-id.umd.js');

describe('bundled umd module', () => {
test('has the expected API', () => {
Expand Down
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
30 changes: 30 additions & 0 deletions __tests__/utils/extract-google-redirection-url.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import extractGoogleRedirectionUrl from '../../src/utils/extract-google-redirection-url.js';

describe('extractGoogleRedirectionUrl', () => {
test('throws TypeError if input is not a string', () => {
expect(() => extractGoogleRedirectionUrl(123)).toThrow(TypeError);
expect(() => extractGoogleRedirectionUrl({})).toThrow(TypeError);
expect(() => extractGoogleRedirectionUrl([])).toThrow(TypeError);
});

test('returns input as is if it does not contain Google redirect URL', () => {
const nonGoogleUrl = 'https://www.example.com';
expect(extractGoogleRedirectionUrl(nonGoogleUrl)).toBe(nonGoogleUrl);
});

test('extracts and decodes URL from a Google redirect URL', () => {
const expectedUrl = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
const googleRedirectUrl = `https://www.google.cz/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=123&url=${encodeURIComponent(expectedUrl)}`;
expect(extractGoogleRedirectionUrl(googleRedirectUrl)).toBe(expectedUrl);
});

test('returns trimmed input if it does not match Google redirect pattern', () => {
const nonMatchingUrl = ' https://www.google.com/search?q=openai ';
expect(extractGoogleRedirectionUrl(nonMatchingUrl)).toBe(nonMatchingUrl.trim());
});

test('returns input as-is if the value is not a URL', () => {
const nonMatchingUrl = 'lorem ipsum';
expect(extractGoogleRedirectionUrl(nonMatchingUrl)).toBe(nonMatchingUrl);
});
});
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();
});
});
33 changes: 33 additions & 0 deletions __tests__/utils/sanitize-url.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sanitizeUrl from '../../src/utils/sanitize-url.js';

describe('sanitize-url', () => {
test('returns the URL without modification for valid URL strings', () => {
const url = 'https://example.com';
expect(sanitizeUrl(url)).toBe(url);
});

test('throws TypeError for non-string inputs', () => {
const input = 123;
expect(() => sanitizeUrl(input)).toThrow(TypeError);
});

test('trims leading or trailing spaces', () => {
const url = ' https://example.com ';
expect(sanitizeUrl(url)).toBe('https://example.com');
});

test('removes leading "www."', () => {
const url = 'https://www.example.com';
expect(sanitizeUrl(url)).toBe('https://example.com');
});

test('extracts src from an iframe input', () => {
const iframeHtml = '<iframe src="https://example.com"></iframe>';
expect(sanitizeUrl(iframeHtml)).toBe('https://example.com');
});

test('returns an empty string when getSrc returns undefined for an iframe', () => {
const iframeHtml = '<iframe></iframe>';
expect(sanitizeUrl(iframeHtml)).toBe('');
});
});
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
8 changes: 0 additions & 8 deletions babel.config.js

This file was deleted.

9 changes: 9 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"env": {
"test": {
"plugins": [
"@babel/plugin-transform-modules-commonjs"
]
}
}
}
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;
};
Loading
Loading