From 2d8a4021d19adc11075047e02f1330e823e1d522 Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Tue, 23 Jan 2018 18:55:36 +0000 Subject: [PATCH] Travis build: 1352 --- README.md | 24 ++++++++++++++++++++++++ docs/index.html | 7 +++++-- snippets/castArray.md | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 365063973..ed61b94d0 100644 --- a/README.md +++ b/README.md @@ -365,6 +365,7 @@ average(1, 2, 3);
View contents +* [`castArray`](#castarray) * [`cloneRegExp`](#cloneregexp) * [`coalesce`](#coalesce) * [`coalesceFactory`](#coalescefactory) @@ -6080,6 +6081,29 @@ isValidJSON(null); // true --- ## 🔧 Utility +### castArray + +Casts the provided value as an array if it's not one. + +Use `Array.isArray()` to determine if `val` is an array and return it as-is or encapsulated in an array accordingly. + +```js +const castArray = val => (Array.isArray(val) ? val : [val]); +``` + +
+Examples + +```js +castArray('foo'); // ['foo'] +castArray([1]); // [1] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### cloneRegExp Clones a regular expression. diff --git a/docs/index.html b/docs/index.html index 2e0a86db1..a1945a1c7 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 ]
@@ -1398,7 +1398,10 @@ Foo.prototypeShow examples
isValidJSON('{"name":"Adam","age":20}'); // true
 isValidJSON('{"name":"Adam",age:"20"}'); // false
 isValidJSON(null); // true
-

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

Utility

castArray

Casts the provided value as an array if it's not one.

Use Array.isArray() to determine if val is an array and return it as-is or encapsulated in an array accordingly.

const castArray = val => (Array.isArray(val) ? val : [val]);
+
castArray('foo'); // ['foo']
+castArray([1]); // [1]
+

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(_));
diff --git a/snippets/castArray.md b/snippets/castArray.md
index dc6d97c77..67f5206ce 100644
--- a/snippets/castArray.md
+++ b/snippets/castArray.md
@@ -5,7 +5,7 @@ Casts the provided value as an array if it's not one.
 Use `Array.isArray()` to determine if `val` is an array and return it as-is or encapsulated in an array accordingly.
 
 ```js
-const castArray = val => Array.isArray(val) ? val : [val];
+const castArray = val => (Array.isArray(val) ? val : [val]);
 ```
 
 ```js