Merge pull request #451 from Chalarangelo/micro-modules

[FEATURE] Micro modules
This commit is contained in:
Angelos Chalaris
2018-01-01 18:44:40 +02:00
committed by GitHub
5 changed files with 64 additions and 0 deletions

21
snippets/hasFlags.md Normal file
View File

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

View File

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

13
snippets/isTravisCI.md Normal file
View File

@ -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)
```

13
snippets/untildify.md Normal file
View File

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

View File

@ -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