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/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/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/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 68a72c1b3..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 @@ -65,6 +66,7 @@ initializeArrayWithRange:array initializeArrayWithValues:array inRange:math intersection:array +invertKeyValues:object isAbsoluteURL:string isArmstrongNumber:math isArray:utility @@ -80,6 +82,7 @@ isPrimitive:utility isPromiseLike:utility isString:utility isSymbol:utility +isTravisCI:node isValidJSON:utility join:array JSONToDate:date @@ -159,6 +162,7 @@ truncateString:string truthCheckCollection:object unescapeHTML:string union:array +untildify:node UUIDGeneratorBrowser:browser UUIDGeneratorNode:node validateNumber:utility