extendHex('#03f'); // '#0033ff' extendHex('05a'); // '#0055aa'
Returns an object containing the parameters of the current URL.
Use String.match() with an appropriate regular expression to get all key-value pairs, Array.reduce() to map and combine them into a single object. Pass location.search as the argument to apply to the current url.
const getURLParameters = url => - url - .match(/([^?=&]+)(=([^&]*))/g) - .reduce((a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a), {}); + (url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce( + (a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a), + {} + );
getURLParameters('http://url.com/page?name=Adam&surname=Smith'); // {name: 'Adam', surname: 'Smith'} +getURLParameters('google.com'); // {}
Converts a color code to a rgb() or rgba() string if alpha value is provided.
Use bitwise right-shift operator and mask bits with & (and) operator to convert a hexadecimal color code (with or without prefixed with #) to a string with the RGB values. If it's 3-digit color code, first convert to 6-digit version. If an alpha value is provided alongside 6-digit hex, give rgba() string in return.
const hexToRGB = hex => { let alpha = false, h = hex.slice(hex.startsWith('#') ? 1 : 0); diff --git a/snippets/getURLParameters.md b/snippets/getURLParameters.md index 3ac727121..a5fe0cecf 100644 --- a/snippets/getURLParameters.md +++ b/snippets/getURLParameters.md @@ -7,14 +7,10 @@ Pass `location.search` as the argument to apply to the current `url`. ```js const getURLParameters = url => - (url.match(/([^?=&]+)(=([^&]*))/g) || []) - .reduce( - (a, v) => ( - (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), - a - ), - {} - ); + (url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce( + (a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a), + {} + ); ``` ```js