From e3f75a55e97cbb75bb66b6a7e18b4edc732b07a7 Mon Sep 17 00:00:00 2001 From: Travis CI Date: Mon, 1 Jan 2018 16:45:34 +0000 Subject: [PATCH] Travis build: 802 [ci skip] --- README.md | 103 ++++++++++++++++++++++++++++++++++++ docs/index.html | 22 +++++++- snippets/hasFlags.md | 4 +- snippets/invertKeyValues.md | 8 ++- snippets/isTravisCI.md | 1 + snippets/untildify.md | 2 +- 6 files changed, 132 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 48e178ea4..1b5c500a2 100644 --- a/README.md +++ b/README.md @@ -187,8 +187,11 @@
View contents +* [`hasFlags`](#hasflags) +* [`isTravisCI`](#istravisci) * [`JSONToFile`](#jsontofile) * [`readFileLines`](#readfilelines) +* [`untildify`](#untildify) * [`UUIDGeneratorNode`](#uuidgeneratornode)
@@ -199,6 +202,7 @@ View contents * [`cleanObj`](#cleanobj) +* [`invertKeyValues`](#invertkeyvalues) * [`lowercaseKeys`](#lowercasekeys) * [`objectFromPairs`](#objectfrompairs) * [`objectToPairs`](#objecttopairs) @@ -3171,6 +3175,57 @@ sumPower(10, 3, 5); //2925 --- ## 📦 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. + +```js +const hasFlags = (...flags) => + flags.every(flag => process.argv.includes(/^-{1,2}/.test(flag) ? flag : '--' + flag)); +``` + +
+Examples + +```js +// node myScript.js -s --test --cool=true +hasFlags('-s'); // true +hasFlags('test', 'cool=true'); // true +hasFlags('--test', 'cool=true', '-s'); // true +hasFlags('special'); // false +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + +### isTravisCI + +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; +``` + +
+Examples + +```js +isTravisCI(); // true (if code is running on Travis CI) +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### JSONToFile Writes a JSON object to a file. @@ -3233,6 +3288,28 @@ console.log(arr); // ['line1', 'line2', 'line3']
[⬆ Back to top](#table-of-contents) +### 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. + +```js +const untildify = str => str.replace(/^~($|\/|\\)/, `${require('os').homedir()}$1`); +``` + +
+Examples + +```js +untildify('~/node'); // '/Users/aUser/node' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### UUIDGeneratorNode Generates a UUID in Node.JS. @@ -3294,6 +3371,32 @@ cleanObj(testObj, ['a'], 'children'); // { a: 1, children : { a: 1}}
[⬆ Back to top](#table-of-contents) +### 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. + +```js +const invertKeyValues = obj => + Object.keys(obj).reduce((acc, key) => { + acc[obj[key]] = key; + return acc; + }, {}); +``` + +
+Examples + +```js +invertKeyValues({ name: 'John', age: 20 }); // { 20: 'age', John: 'name' } +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### lowercaseKeys Creates a new object from the specified object, where all the keys are in lowercase. diff --git a/docs/index.html b/docs/index.html index 3d297e8b7..a7d6baf94 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 ]
@@ -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

Checks if the current environment is Travis CI.

Checks if the current environment has the TRAVIS and CI environment variables (reference).


+const isTravisCI = ()) => 'TRAVIS' in process.env && 'CI' in process.env;
+
isTravisCI(); // true (if code is running on Travis CI)
+

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'
 ```