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 ]
@@ -722,6 +722,10 @@ countVowels('gym'); // 0
fromCamelCase('someDatabaseFieldName', ' '); // 'some database field name'
fromCamelCase('someLabelThatNeedsToBeCamelized', '-'); // 'some-label-that-needs-to-be-camelized'
fromCamelCase('someJavascriptProperty', '_'); // 'some_javascript_property'
+
isAbsoluteURL
Returns true if the given string is an absolute URL, false otherwise.
Use a regular expression to test if the string is an absolute URL.
const isAbsoluteURL = str => /^[a-z][a-z0-9+.-]*:/.test(str);
+
isAbsoluteURL('https://google.com'); // true
+isAbsoluteURL('ftp://www.myserver.net'); // true
+isAbsoluteURL('/foo/bar'); // false
palindrome
Returns true if the given string is a palindrome, false otherwise.
Convert string toLowerCase() and use replace() to remove non-alphanumeric characters from it. Then, split('') into individual characters, reverse(), join('') and compare to the original, unreversed string, after converting it tolowerCase().
const palindrome = str => {
const s = str.toLowerCase().replace(/[\W_]/g, '');
return (
@@ -878,10 +882,10 @@ isString('10'); // true
isSymbol
Checks if the given argument is a symbol.
Use typeof to check if a value is classified as a symbol primitive.
const isSymbol = val => typeof val === 'symbol';
isSymbol('x'); // false
isSymbol(Symbol('x')); // true
-
isValidJSON
Checks if the provided argument is a valid JSON.
Use JSON.parse() and a try... catch block to check if the provided argument is a valid JSON.
const isValidJSON = obj => {
+
isValidJSON
Checks if the provided argument is a valid JSON.
Use JSON.parse() and a try... catch block to check if the provided argument is a valid JSON and non-null.
const isValidJSON = obj => {
try {
JSON.parse(obj);
- return true;
+ return !!obj;
} catch (e) {
return false;
}
diff --git a/snippets/isAbsoluteURL.md b/snippets/isAbsoluteURL.md
index 8f13f5861..00d8d8936 100644
--- a/snippets/isAbsoluteURL.md
+++ b/snippets/isAbsoluteURL.md
@@ -5,7 +5,7 @@ Returns `true` if the given string is an absolute URL, `false` otherwise.
Use a regular expression to test if the string is an absolute URL.
```js
-const isAbsoluteURL = str => /^[a-z][a-z0-9+.-]*:/.test(str);
+const isAbsoluteURL = str => /^[a-z][a-z0-9+.-]*:/.test(str);
```
```js
diff --git a/snippets/isValidJSON.md b/snippets/isValidJSON.md
index dadefb897..ed7672a3a 100644
--- a/snippets/isValidJSON.md
+++ b/snippets/isValidJSON.md
@@ -9,7 +9,7 @@ const isValidJSON = obj => {
try {
JSON.parse(obj);
return !!obj;
-} catch (e) {
+ } catch (e) {
return false;
}
};