diff --git a/README.md b/README.md index b3607ac90..05101872a 100644 --- a/README.md +++ b/README.md @@ -314,6 +314,7 @@ average(1, 2, 3); * [`toSnakeCase`](#tosnakecase) * [`truncateString`](#truncatestring) * [`unescapeHTML`](#unescapehtml) +* [`URLJoin`](#urljoin) * [`words`](#words) @@ -5118,6 +5119,36 @@ unescapeHTML('<a href="#">Me & you</a>'); // '[⬆ Back to top](#table-of-contents) +### URLJoin + +Joins all given URL segments together, then normalizes the resulting URL. + +Use `String.join('/')` to combine URL segments, then a series of `String.replace()` calls with various regexps to normalize the resulting URL (remove double slashes, add proper slashes for protocol, remove slashes before parameters, combine parameters with `'&'` and normalize first parameter delimiter). + +```js +const URLJoin = (...args) => + args + .join('/') + .replace(/[\/]+/g, '/') + .replace(/^(.+):\//, '$1://') + .replace(/^file:/, 'file:/') + .replace(/\/(\?|&|#[^!])/g, '$1') + .replace(/\?/g, '&') + .replace('&', '?'); +``` + +
+Examples + +```js +URLJoin('http://www.google.com', 'a', '/b/cd', '?foo=123', '?bar=foo'); // 'http://www.google.com/a/b/cd?foo=123&bar=foo' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### words Converts a given string into an array of words. diff --git a/docs/index.html b/docs/index.html index 35d97f198..f11855dff 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -1165,6 +1165,16 @@ Foo.prototype}[tag] || tag)
   );
 
unescapeHTML('&lt;a href=&quot;#&quot;&gt;Me &amp; you&lt;/a&gt;'); // '<a href="#">Me & you</a>'
+

URLJoin

Joins all given URL segments together, then normalizes the resulting URL.

Use String.join('/') to combine URL segments, then a series of String.replace() calls with various regexps to normalize the resulting URL (remove double slashes, add proper slashes for protocol, remove slashes before parameters, combine parameters with '&' and normalize first parameter delimiter).

const URLJoin = (...args) =>
+  args
+    .join('/')
+    .replace(/[\/]+/g, '/')
+    .replace(/^(.+):\//, '$1://')
+    .replace(/^file:/, 'file:/')
+    .replace(/\/(\?|&|#[^!])/g, '$1')
+    .replace(/\?/g, '&')
+    .replace('&', '?');
+
URLJoin('http://www.google.com', 'a', '/b/cd', '?foo=123', '?bar=foo'); // 'http://www.google.com/a/b/cd?foo=123&bar=foo'
 

words

Converts a given string into an array of words.

Use String.split() with a supplied pattern (defaults to non-alpha as a regexp) to convert to an array of strings. Use Array.filter() to remove any empty strings. Omit the second argument to use the default regexp.

const words = (str, pattern = /[^a-zA-Z-]+/) => str.split(pattern).filter(Boolean);
 
words('I love javaScript!!'); // ["I", "love", "javaScript"]
 words('python, javaScript & coffee'); // ["python", "javaScript", "coffee"]
diff --git a/snippets/URLJoin.md b/snippets/URLJoin.md
index 67d921003..aa7492782 100644
--- a/snippets/URLJoin.md
+++ b/snippets/URLJoin.md
@@ -6,13 +6,14 @@ Use `String.join('/')` to combine URL segments, then a series of `String.replace
 
 ```js
 const URLJoin = (...args) =>
-  args.join('/')
-  .replace(/[\/]+/g,'/')
-  .replace(/^(.+):\//,'$1://')
-  .replace(/^file:/,'file:/')
-  .replace(/\/(\?|&|#[^!])/g, '$1')
-  .replace(/\?/g,'&')
-  .replace('&','?');
+  args
+    .join('/')
+    .replace(/[\/]+/g, '/')
+    .replace(/^(.+):\//, '$1://')
+    .replace(/^file:/, 'file:/')
+    .replace(/\/(\?|&|#[^!])/g, '$1')
+    .replace(/\?/g, '&')
+    .replace('&', '?');
 ```
 
 ```js