Created
May 29, 2022 17:34
-
-
Save kidGodzilla/51d3a83d4b77044dc9618e61b798487b to your computer and use it in GitHub Desktop.
Infer Browser by input string & return an icon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* Given a slightly messy set of input strings, attempt to match a well-known browser name, and return an icon | |
* | |
* Examples: | |
* inferBrowserIcon('safari') // exact match | |
* inferBrowserIcon('Safari Mobile 15.4') // matches "safari" from input string | |
* inferBrowserIcon('Firefox 99', 128) // pick a specific size (from sizes) | |
* inferBrowserIcon('unknownbrowser') // fallback if no match | |
* inferBrowserIcon(null, 128, 'brave') // You know the browser-logos repo key | |
*/ | |
function inferBrowserIcon(s, size = 48, browser = 'web') { | |
const browsers = 'avant,basilisk,brave,browsh,chrome,chromium,deno,dolphin,edge,electron,epic,falkon,firefox,geckoview,hermes,icecat,jsdom,konqueror,maxthon,midori,netsurf,node.js,nw.js,opera,otter,puffin,qutebrowser,safari,seamonkey,servo,silk,spidermonkey,tor,uc,v8,vivaldi,web,webkit,yandex'.split(','); | |
const sizes = '16,24,32,48,64,128,256,512'.split(','); | |
const arr = s.toLowerCase().trim().split(' '); | |
let intersection = browsers.filter(value => arr.includes(value)); | |
if (intersection.length) browser = intersection[0]; | |
return `https://cdnjs.cloudflare.com/ajax/libs/browser-logos/72.0.0/${ browser }/${ browser }_${ size }x${ size }.png`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment