diff --git a/README.md b/README.md index 9c0256cd2..1ab0c8c3c 100644 --- a/README.md +++ b/README.md @@ -251,6 +251,7 @@ * [`toDecimalMark`](#todecimalmark) * [`toOrdinalSuffix`](#toordinalsuffix) * [`validateNumber`](#validatenumber) +* [`yesNo`](#yesno) @@ -4202,6 +4203,33 @@ validateNumber('10'); // true
[⬆ Back to top](#table-of-contents) +### 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; +``` + +
+Examples + +```js +yesNo('Y'); // true +yesNo('yes'); // true +yesNo('No'); // false +yesNo('Foo', true); // true +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ## Credits *Icons made by [Smashicons](https://www.flaticon.com/authors/smashicons) from [www.flaticon.com](https://www.flaticon.com/) is licensed by [CC 3.0 BY](http://creativecommons.org/licenses/by/3.0/).* diff --git a/docs/index.html b/docs/index.html index 1459847d1..e4346b4ec 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+    }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -862,4 +862,10 @@ console.log(sdbm('age')); // 808122783
 
toOrdinalSuffix('123'); // "123rd"
 

validateNumber

Returns true if the given value is a number, false otherwise.

Use !isNaN in combination with parseFloat() to check if the argument is a number. Use isFinite() to check if the number is finite. Use Number() to check if the coercion holds.

const validateNumber = n => !isNaN(parseFloat(n)) && isFinite(n) && Number(n) == n;
 
validateNumber('10'); // true
+

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.

const yesNo = (val, def = false) =>
+  /^(y|yes)$/i.test(val) ? true : /^(n|no)$/i.test(val) ? false : def;
+
yesNo('Y'); // true
+yesNo('yes'); // true
+yesNo('No'); // false
+yesNo('Foo', true); // true
 

\ No newline at end of file diff --git a/snippets/yesNo.md b/snippets/yesNo.md index 3783e6fde..5d191cb94 100644 --- a/snippets/yesNo.md +++ b/snippets/yesNo.md @@ -11,8 +11,8 @@ const yesNo = (val, def = false) => ``` ```js -yesNo('Y') // true -yesNo('yes') // true -yesNo('No') // false -yesNo('Foo', true) // true +yesNo('Y'); // true +yesNo('yes'); // true +yesNo('No'); // false +yesNo('Foo', true); // true ```