From 73b5d5c691aaaf9bd75b3635bf830d9c15a7ae25 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 1 Jan 2018 17:28:08 +0200 Subject: [PATCH 1/4] Add isTravisCI Checks if the current environment is Travis CI. --- snippets/isTravisCI.md | 13 +++++++++++++ tag_database | 1 + 2 files changed, 14 insertions(+) create mode 100644 snippets/isTravisCI.md diff --git a/snippets/isTravisCI.md b/snippets/isTravisCI.md new file mode 100644 index 000000000..3bffeb110 --- /dev/null +++ b/snippets/isTravisCI.md @@ -0,0 +1,13 @@ +### 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; +``` + +```js +isTravisCI(); // true (if code is running on Travis CI) +``` diff --git a/tag_database b/tag_database index 68a72c1b3..185d2ee1f 100644 --- a/tag_database +++ b/tag_database @@ -80,6 +80,7 @@ isPrimitive:utility isPromiseLike:utility isString:utility isSymbol:utility +isTravisCI:node isValidJSON:utility join:array JSONToDate:date From 61120916ded880446fa3bf9f59ae706bc0d5825e Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 1 Jan 2018 17:33:46 +0200 Subject: [PATCH 2/4] Add invertKeyValues Inverts the key-value pairs of an object --- snippets/invertKeyValues.md | 13 +++++++++++++ tag_database | 1 + 2 files changed, 14 insertions(+) create mode 100644 snippets/invertKeyValues.md diff --git a/snippets/invertKeyValues.md b/snippets/invertKeyValues.md new file mode 100644 index 000000000..b3a7ce11d --- /dev/null +++ b/snippets/invertKeyValues.md @@ -0,0 +1,13 @@ +### 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;},{}); +``` + +```js +invertKeyValues({name:'John', age: 20}) // { 20: 'age', John: 'name' } +``` diff --git a/tag_database b/tag_database index 185d2ee1f..64503984d 100644 --- a/tag_database +++ b/tag_database @@ -65,6 +65,7 @@ initializeArrayWithRange:array initializeArrayWithValues:array inRange:math intersection:array +invertKeyValues:object isAbsoluteURL:string isArmstrongNumber:math isArray:utility From 267a3c035cb4bca9f8983cc72fc7b5a7d4142523 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 1 Jan 2018 17:43:18 +0200 Subject: [PATCH 3/4] Add untildify Converts a tilde path to an absolute path. --- snippets/untildify.md | 13 +++++++++++++ tag_database | 1 + 2 files changed, 14 insertions(+) create mode 100644 snippets/untildify.md diff --git a/snippets/untildify.md b/snippets/untildify.md new file mode 100644 index 000000000..67e51efe8 --- /dev/null +++ b/snippets/untildify.md @@ -0,0 +1,13 @@ +### 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`); +``` + +```js +untildify('~/node') // '/Users/aUser/node' +``` diff --git a/tag_database b/tag_database index 64503984d..e66052213 100644 --- a/tag_database +++ b/tag_database @@ -161,6 +161,7 @@ truncateString:string truthCheckCollection:object unescapeHTML:string union:array +untildify:node UUIDGeneratorBrowser:browser UUIDGeneratorNode:node validateNumber:utility From 995a05f47ee40c109d2e0e3be8494c729aba346a Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 1 Jan 2018 18:24:43 +0200 Subject: [PATCH 4/4] Add hasFlags Check if the current process's arguments contain the specified flags. --- snippets/hasFlags.md | 21 +++++++++++++++++++++ tag_database | 1 + 2 files changed, 22 insertions(+) create mode 100644 snippets/hasFlags.md diff --git a/snippets/hasFlags.md b/snippets/hasFlags.md new file mode 100644 index 000000000..124a72aac --- /dev/null +++ b/snippets/hasFlags.md @@ -0,0 +1,21 @@ +### 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 + )); +``` + +```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 +``` diff --git a/tag_database b/tag_database index e66052213..c4f3cd421 100644 --- a/tag_database +++ b/tag_database @@ -55,6 +55,7 @@ getURLParameters:utility groupBy:array hammingDistance:math hasClass:browser +hasFlags:node head:array hexToRGB:utility hide:browser