UNPKG

media-grab

Version:

Grab random media from the internet

38 lines (37 loc) 1.33 kB
// Returns a random value from an array export const getRandomIndex = (inArray) => { return inArray[Math.floor(Math.random() * inArray.length)]; }; // Checks if a url is valid /** * Returns true of a given parameter is not null and not undefined. * @param any any input is feasible * @return {boolean} true if defined, otherwise false */ export const isValidURL = (str) => { const pattern = new RegExp('^(https?:\\/\\/)?' + // protocol '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path '(\\?[;&a-z\\d%_.~+=-]*)?' + // query string '(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator return !!pattern.test(str); }; // Create a reddit formatted URL export const getRedditSlug = (subreddit, rType, timeString = null, afterId = null) => { let query; if (timeString) { query = `?${timeString}${afterId ? '&after=' : ''}${afterId || ''}`; } else { query = `${afterId ? '?after=' : ''}${afterId || ''}`; } return `/r/${subreddit}/${rType}${query}`; }; // Create response object export const createResponse = (valid, data) => { return { valid, ...data, }; };