Javascript regular expression that slightly smartly puts back a preprod version of random site URLs, but is smart enough to only do it for "mycompany" sites...("mycompany. mycompanyboard.nl, and othercompanysite...)
const preprodUrlSubstring = 'www.mycompanyprepod.'; const exp = [ 'https://mycompany.com/profile/', 'https://www.mycompany.com/profile/', 'https://www.mycompany.co.uk/profile/', 'https://mycompany.co.uk/profile/', 'https://www.mycompany.co.uk', 'https://www.mycompany.co.uk/profile/', 'https://mycompanyboard.nl/profile/', 'https://www.mycompanyboard.nl/profile/', 'https://othercompanysite.com', 'https://www.twitter.com/mycompany/', 'https://facebook.com/othercompanysite.com/', 'https://www.mycompanyprepod.mycompany.com/profile/', ]; const fix = (currentUrl) => { const pattern = /^(https?:\/\/)(www\.)?(mycompany\.| othercompanysite\.com| mycompanyboard\.nl)(.*?)$/i; return currentUrl.replace(pattern, `$1${preprodUrlSubstring}$3$4`); } exp.map((thing)=>{ console.log(`expect(convertUrlToPreproductionVersion('${thing}')).toBe('${fix(thing)}');`) });
I like how I then had it make the jest test lines for the cases I came up with..
Oh and jsitor proved its value, it seems less fiddly then some of the other jsfiddle type sandboxes (sill don't understand the detail of autocomplete quotes and parens and what not though!)
No comments:
Post a Comment