diff --git a/README.md b/README.md index 08c12c464..c364788a7 100644 --- a/README.md +++ b/README.md @@ -6906,9 +6906,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), + {} + ); ```
@@ -6916,6 +6917,7 @@ const getURLParameters = url => ```js getURLParameters('http://url.com/page?name=Adam&surname=Smith'); // {name: 'Adam', surname: 'Smith'} +getURLParameters('google.com'); // {} ```
diff --git a/docs/index.html b/docs/index.html index 7c85f7213..ef0b37fa0 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1572,10 +1572,12 @@ Foo.prototypeShow examples
extendHex('#03f'); // '#0033ff'
 extendHex('05a'); // '#0055aa'
 

getURLParameters

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'); // {}
 

hexToRGBadvanced

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