Skip to content

Commit

Permalink
Support on and off words
Browse files Browse the repository at this point in the history
Fixes #25
  • Loading branch information
sindresorhus committed Dec 1, 2019
1 parent b1cd76b commit 65e8faf
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ declare namespace yn {
/**
Parse yes/no like values.
The following case-insensitive values are recognized: `'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0`
The following case-insensitive values are recognized: `'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0`, 'on', 'off'
@param input - Value that should be converted.
@returns The parsed input if it can be parsed or the default value defined in the `default` option.
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const yn = (value, {
throw new TypeError(`Expected the \`default\` option to be of type \`boolean\`, got \`${typeof default_}\``);
}

if (/^(?:y|yes|true|1)$/i.test(value)) {
if (/^(?:y|yes|true|1|on)$/i.test(value)) {
return true;
}

if (/^(?:n|no|false|0)$/i.test(value)) {
if (/^(?:n|no|false|0|off)$/i.test(value)) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Useful for validating answers of a CLI prompt.
The following case-insensitive values are recognized:

```js
'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0
'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0, 'on', 'off'
```

*Enable lenient mode to gracefully handle typos.*
Expand Down
6 changes: 4 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const truthyCases = [
'True',
true,
'1',
1
1,
'on'
];
test('truthy cases', t => {
for (const case_ of truthyCases) {
Expand All @@ -32,7 +33,8 @@ const falseyCases = [
'False',
false,
'0',
0
0,
'off'
];
test('falsey cases', t => {
for (const case_ of falseyCases) {
Expand Down

0 comments on commit 65e8faf

Please sign in to comment.