Add hasFlags
Check if the current process's arguments contain the specified flags.
This commit is contained in:
21
snippets/hasFlags.md
Normal file
21
snippets/hasFlags.md
Normal 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
|
||||||
|
```
|
||||||
@ -55,6 +55,7 @@ getURLParameters:utility
|
|||||||
groupBy:array
|
groupBy:array
|
||||||
hammingDistance:math
|
hammingDistance:math
|
||||||
hasClass:browser
|
hasClass:browser
|
||||||
|
hasFlags:node
|
||||||
head:array
|
head:array
|
||||||
hexToRGB:utility
|
hexToRGB:utility
|
||||||
hide:browser
|
hide:browser
|
||||||
|
|||||||
Reference in New Issue
Block a user