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 ]
@@ -641,7 +641,17 @@ standardDeviation([10, 2, 38, 23, 38, 23, 21], true); // 12.29899614287479 (popu
sumPower(10); // 385
sumPower(10, 3); //3025
sumPower(10, 3, 5); //2925
-
Node
JSONToFile
Writes a JSON object to a file.
Use fs.writeFile(), template literals and JSON.stringify() to write a json object to a .json file.
const fs = require('fs');
+
Node
hasFlags
Check if the current process's arguments contain the specified flags.
Use Array.every() and Array.includes() to check if process.argv contains all the specified flags. Use a regular expression to test if the specified flags are prefixed with - or -- and prefix them accordingly.
const hasFlags = (...flags) =>
+ flags.every(flag => process.argv.includes(/^-{1,2}/.test(flag) ? flag : '--' + flag));
+
// node myScript.js -s --test --cool=true
+hasFlags('-s'); // true
+hasFlags('test', 'cool=true'); // true
+hasFlags('--test', 'cool=true', '-s'); // true
+hasFlags('special'); // false
+
isTravisCI
JSONToFile
Writes a JSON object to a file.
Use fs.writeFile(), template literals and JSON.stringify() to write a json object to a .json file.
const fs = require('fs');
const JSONToFile = (obj, filename) =>
fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2));
JSONToFile({ test: 'is passed' }, 'testJsonFile'); // writes the object to 'testJsonFile.json'
@@ -660,6 +670,8 @@ contents of test.txt :
*/
let arr = readFileLines('test.txt');
console.log(arr); // ['line1', 'line2', 'line3']
+
untildify
Converts a tilde path to an absolute path.
Use String.replace() with a regular expression and OS.homedir() to replace the ~ in the start of the path with the home directory.
const untildify = str => str.replace(/^~($|\/|\\)/, `${require('os').homedir()}$1`);
+
untildify('~/node'); // '/Users/aUser/node'
UUIDGeneratorNode
Generates a UUID in Node.JS.
Use crypto API to generate a UUID, compliant with RFC4122 version 4.
const crypto = require('crypto');
const UUIDGeneratorNode = () =>
([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
@@ -678,6 +690,12 @@ const UUIDGeneratorNode = () =>
};
const testObj = { a: 1, b: 2, children: { a: 1, b: 2 } };
cleanObj(testObj, ['a'], 'children'); // { a: 1, children : { a: 1}}
+
invertKeyValues
Inverts the key-value pairs of an object, without mutating it.
Use Object.keys() and Array.reduce() to invert the key-value pairs of an object.
const invertKeyValues = obj =>
+ Object.keys(obj).reduce((acc, key) => {
+ acc[obj[key]] = key;
+ return acc;
+ }, {});
+
invertKeyValues({ name: 'John', age: 20 }); // { 20: 'age', John: 'name' }
lowercaseKeys
Creates a new object from the specified object, where all the keys are in lowercase.
Use Object.keys() and Array.reduce() to create a new object from the specified object. Convert each key in the original object to lowercase, using String.toLowerCase().
const lowercaseKeys = obj =>
Object.keys(obj).reduce((acc, key) => {
acc[key.toLowerCase()] = obj[key];
diff --git a/snippets/hasFlags.md b/snippets/hasFlags.md
index 124a72aac..6e8f42575 100644
--- a/snippets/hasFlags.md
+++ b/snippets/hasFlags.md
@@ -7,9 +7,7 @@ Use a regular expression to test if the specified flags are prefixed with `-` or
```js
const hasFlags = (...flags) =>
- flags.every(flag => process.argv.includes(
- /^-{1,2}/.test(flag) ? flag : '--' + flag
- ));
+ flags.every(flag => process.argv.includes(/^-{1,2}/.test(flag) ? flag : '--' + flag));
```
```js
diff --git a/snippets/invertKeyValues.md b/snippets/invertKeyValues.md
index b3a7ce11d..2d4302145 100644
--- a/snippets/invertKeyValues.md
+++ b/snippets/invertKeyValues.md
@@ -5,9 +5,13 @@ Inverts the key-value pairs of an object, without mutating it.
Use `Object.keys()` and `Array.reduce()` to invert the key-value pairs of an object.
```js
-const invertKeyValues = obj => Object.keys(obj).reduce((acc,key) => { acc[obj[key]] = key; return acc;},{});
+const invertKeyValues = obj =>
+ Object.keys(obj).reduce((acc, key) => {
+ acc[obj[key]] = key;
+ return acc;
+ }, {});
```
```js
-invertKeyValues({name:'John', age: 20}) // { 20: 'age', John: 'name' }
+invertKeyValues({ name: 'John', age: 20 }); // { 20: 'age', John: 'name' }
```
diff --git a/snippets/isTravisCI.md b/snippets/isTravisCI.md
index 3bffeb110..6039d2ba8 100644
--- a/snippets/isTravisCI.md
+++ b/snippets/isTravisCI.md
@@ -5,6 +5,7 @@ Checks if the current environment is [Travis CI](https://travis-ci.org/).
Checks if the current environment has the `TRAVIS` and `CI` environment variables ([reference](https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables)).
```js
+
const isTravisCI = ()) => 'TRAVIS' in process.env && 'CI' in process.env;
```
diff --git a/snippets/untildify.md b/snippets/untildify.md
index 67e51efe8..4d83d3cb7 100644
--- a/snippets/untildify.md
+++ b/snippets/untildify.md
@@ -9,5 +9,5 @@ const untildify = str => str.replace(/^~($|\/|\\)/, `${require('os').homedir()}$
```
```js
-untildify('~/node') // '/Users/aUser/node'
+untildify('~/node'); // '/Users/aUser/node'
```