ripunzel/dissectLink.js

50 lines
1.2 KiB
JavaScript
Raw Normal View History

'use strict';
const urlPattern = require('url-pattern');
const hosts = [{
method: 'self',
label: 'self',
pattern: new urlPattern('http(s)\\://(www.)reddit.com/r/:subreddit/comments/:id/:uri/')
}, {
method: 'reddit',
label: 'reddit',
pattern: new urlPattern('http(s)\\://i.redd.it/:id.:ext')
}, {
method: 'imgurImage',
label: 'imgur',
pattern: new urlPattern('http(s)\\://(i.)imgur.com/:id(.:ext)(?:num)')
}, {
method: 'imgurAlbum',
label: 'imgur',
pattern: new urlPattern('http(s)\\://(m.)imgur.com/:type/:id')
}, {
method: 'gfycat',
label: 'gfycat',
pattern: new urlPattern('http(s)\\://(:server.)gfycat.com/:id(.:ext)')
}, {
method: 'eroshare',
label: 'eroshare',
pattern: new urlPattern('http(s)\\://eroshare.com/:id')
}];
module.exports = function dissectLink(url) {
return hosts.reduce((acc, host) => {
if(acc) {
return acc;
}
const match = host.pattern.match(url);
if(match) {
return Object.assign(match, {
url: url,
method: host.method,
label: host.label
});
}
return null;
}, null);
};