Add yesNo snippet

This commit is contained in:
Angelos Chalaris
2017-12-30 18:35:54 +02:00
parent 7288eabd27
commit c35a957a0f
2 changed files with 19 additions and 0 deletions

18
snippets/yesNo.md Normal file
View File

@ -0,0 +1,18 @@
### yesNo
Returns `true` if the string is `y`/`yes` or `false` if the string is `n`/`no`.
Use `RegExp.test()` to check if the string evaluates to `y/yes` or `n/no`.
Omit the second argument, `def` to set the default answer as `no`.
```js
const yesNo = (val, def = false) =>
/^(y|yes)$/i.test(val) ? true : /^(n|no)$/i.test(val) ? false : def;
```
```js
yesNo('Y') // true
yesNo('yes') // true
yesNo('No') // false
yesNo('Foo', true) // true
```

View File

@ -148,5 +148,6 @@ UUIDGeneratorNode:node
validateNumber:utility
without:array
words:string
yesNo:utility
zip:array
zipObject:array