2018-04-15 02:51:02 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const urlPattern = require('url-pattern');
|
|
|
|
|
|
|
|
const hosts = [{
|
2018-04-17 22:18:04 +00:00
|
|
|
method: 'self',
|
|
|
|
pattern: new urlPattern('http(s)\\://(www.)reddit.com/r/:subreddit/comments/:id/:uri/')
|
|
|
|
}, {
|
|
|
|
method: 'reddit',
|
|
|
|
pattern: new urlPattern('http(s)\\://i.redd.it/:id.:ext')
|
|
|
|
}, {
|
2018-04-15 02:51:02 +00:00
|
|
|
method: 'imgurImage',
|
|
|
|
pattern: new urlPattern('http(s)\\://(i.)imgur.com/:id(.:ext)(?:num)')
|
|
|
|
}, {
|
|
|
|
method: 'imgurAlbum',
|
2018-04-17 22:18:04 +00:00
|
|
|
pattern: new urlPattern('http(s)\\://(m.)imgur.com/:type/:id')
|
2018-04-16 21:46:39 +00:00
|
|
|
}, {
|
|
|
|
method: 'gfycat',
|
|
|
|
pattern: new urlPattern('http(s)\\://(:server.)gfycat.com/:id(.:ext)')
|
2018-04-15 02:51:02 +00:00
|
|
|
}];
|
|
|
|
|
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}, null);
|
|
|
|
};
|