diff --git a/README.md b/README.md index 1854ec1a5..2e0f09565 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,7 @@
View contents +* [`cloneRegExp`](#cloneregexp) * [`coalesce`](#coalesce) * [`coalesceFactory`](#coalescefactory) * [`extendHex`](#extendhex) @@ -4284,6 +4285,29 @@ words('python, javaScript & coffee'); // ["python", "javaScript", "coffee"] --- ## 🔧 Utility +### cloneRegExp + +Clones a regular expression. + +Use `new RegExp()`, `RegExp.source` and `RegExp.flags` to clone the given regular expression. + +```js +const cloneRegExp = regExp => new RegExp(regExp.source, regExp.flags); +``` + +
+Examples + +```js +const regExp = /lorem ipsum/gi; +const regExp2 = cloneRegExp(regExp); // /lorem ipsum/gi +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### coalesce Returns the first non-null/undefined argument. diff --git a/docs/index.html b/docs/index.html index 81bd9f165..bf8aeebab 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 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);
+    }

 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 ]
@@ -927,7 +927,10 @@ toSnakeCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSo
 

words

Converts a given string into an array of words.

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

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"]
-

Utility

coalesce

Returns the first non-null/undefined argument.

Use Array.find() to return the first non null/undefined argument.

const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_));
+

Utility

cloneRegExp

Clones a regular expression.

Use new RegExp(), RegExp.source and RegExp.flags to clone the given regular expression.

const cloneRegExp = regExp => new RegExp(regExp.source, regExp.flags);
+
const regExp = /lorem ipsum/gi;
+const regExp2 = cloneRegExp(regExp); // /lorem ipsum/gi
+

coalesce

Returns the first non-null/undefined argument.

Use Array.find() to return the first non null/undefined argument.

const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_));
 
coalesce(null, undefined, '', NaN, 'Waldo'); // ""
 

coalesceFactory

Returns a customized coalesce function that returns the first argument that returns true from the provided argument validation function.

Use Array.find() to return the first argument that returns true from the provided argument validation function.

const coalesceFactory = valid => (...args) => args.find(valid);
 
const customCoalesce = coalesceFactory(_ => ![null, undefined, '', NaN].includes(_));