'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: 'redditImage', label: 'reddit', pattern: new urlPattern('http(s)\\://i.redd.it/:id.:ext') }, { method: 'redditImage', label: 'reddit', pattern: new urlPattern('http(s)\\://i.reddituploads.com/:id(?*)') }, { method: 'redditVideo', label: 'reddit', pattern: new urlPattern('http(s)\\://v.redd.it/:id') }, { method: 'imgurImage', label: 'imgur', pattern: new urlPattern('http(s)\\://(:subdomain.)imgur.com/:id(.:ext)(?*)') }, { method: 'imgurAlbum', label: 'imgur', pattern: new urlPattern('http(s)\\://(:subdomain.)imgur.com/:type/:id') }, { method: 'vidbleImage', label: 'vidble', pattern: new urlPattern('http(s)\\://(www.)vidble.com/(show/):id(.:ext)') }, { method: 'vidbleVideo', label: 'vidble', pattern: new urlPattern('http(s)\\://(www.)vidble.com/watch?v=:id') }, { method: 'vidbleAlbum', label: 'vidble', pattern: new urlPattern('http(s)\\://(www.)vidble.com/album/:id') }, { method: 'gfycat', label: 'gfycat', pattern: new urlPattern('http(s)\\://(:server.)gfycat.com/:id(-size_restricted.gif)') }, { method: 'gfycat', label: 'gfycat', pattern: new urlPattern('http(s)\\://(:server.)gfycat.com/(gifs/detail/):id(.:ext)') }, { method: 'eroshareAlbum', label: 'eroshare', pattern: new urlPattern('http(s)\\://eroshare.com/:id(#)(:query)') }, { method: 'eroshareItem', label: 'eroshare', pattern: new urlPattern('http(s)\\://eroshare.com/i/:id(#)(:query)') }]; module.exports = function dissectLink(url) { return hosts.reduce((acc, host) => { if(acc) { return acc; } const match = host.pattern.match(url.replace(/(https?:\/\/)|(\/)+/g, '$1$2')); // remove double slashes if(match) { return Object.assign(match, { url: url, method: host.method, label: host.label }); } return null; }, null); };