Skip to content

Commit

Permalink
Requiore Node.js 10
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Dec 1, 2019
1 parent 322fa95 commit bbc3121
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 77 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
language: node_js
node_js:
- '12'
- '10'
- '8'
- '6'
56 changes: 24 additions & 32 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,38 @@ declare namespace yn {
}
}

declare const yn: {
/**
Parse yes/no like values.
/**
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`
@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.
@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.
@example
```
import yn = require('yn');
@example
```
import yn = require('yn');
yn('y');
//=> true
yn('y');
//=> true
yn('NO');
//=> false
yn('NO');
//=> false
yn(true);
//=> true
yn(true);
//=> true
yn('abomasum');
//=> null
yn('abomasum');
//=> null
yn('abomasum', {default: false});
//=> false
yn('abomasum', {default: false});
//=> false
yn('mo', {lenient: true});
//=> false
```
*/
(input: unknown, options: yn.OptionsWithDefault): boolean;
(input: unknown, options?: yn.Options): boolean | null;

// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function yn(input: unknown, options: yn.OptionsWithDefault): boolean;
// declare function yn(input: unknown, options?: yn.Options): boolean | null;
// export = yn;
default: typeof yn;
};
yn('mo', {lenient: true});
//=> false
```
*/
declare function yn(input: unknown, options: yn.OptionsWithDefault): boolean;
declare function yn(input: unknown, options?: yn.Options): boolean | null;

export = yn;
30 changes: 13 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
'use strict';
const lenient = require('./lenient');
const lenientFunction = require('./lenient');

const yn = (input, options) => {
input = String(input).trim();
const yn = (value, {
lenient = false,
default: default_ = null
} = {}) => {
value = String(value).trim();

options = Object.assign({
lenient: false,
default: null
}, options);

if (options.default !== null && typeof options.default !== 'boolean') {
throw new TypeError(`Expected the \`default\` option to be of type \`boolean\`, got \`${typeof options.default}\``);
if (default_ !== null && typeof default_ !== 'boolean') {
throw new TypeError(`Expected the \`default\` option to be of type \`boolean\`, got \`${typeof default_}\``);
}

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

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

if (options.lenient === true) {
return lenient(input, options);
if (lenient === true) {
return lenientFunction(value, default_);
}

return options.default;
return default_;
};

module.exports = yn;
// TODO: Remove this for the next major release
module.exports.default = yn;
4 changes: 2 additions & 2 deletions lenient.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function getNoMatchScore(value) {
return score;
}

module.exports = (input, options) => {
module.exports = (input, default_) => {
if (getYesMatchScore(input) >= YES_MATCH_SCORE_THRESHOLD) {
return true;
}
Expand All @@ -101,5 +101,5 @@ module.exports = (input, options) => {
return false;
}

return options.default;
return default_;
};
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -35,8 +35,8 @@
"lenient"
],
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^2.4.0",
"tsd": "^0.11.0",
"xo": "^0.25.3"
}
}
15 changes: 5 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,28 @@ Unrecognized values return `null`.

## API

### yn(input, [options])
### yn(input, options?)

#### input

Type: `any`
Type: `unknown`

Value that should be converted.

#### options

Type: `Object`
Type: `object`

##### lenient

Type: `boolean`<br>
Type: `boolean`\
Default: `false`

Use a key distance-based score to leniently accept typos of `yes` and `no`.

##### default

Type: `boolean`<br>
Type: `boolean`\
Default: `null`

Default value if no match was found.


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
22 changes: 12 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const truthyCases = [
1
];
test('truthy cases', t => {
for (const el of truthyCases) {
t.true(yn(el));
t.true(yn(el, {lenient: true}));
for (const truthyCase of truthyCases) {
t.true(yn(truthyCase));
t.true(yn(truthyCase, {lenient: true}));
}
});

Expand All @@ -35,9 +35,9 @@ const falseyCases = [
0
];
test('falsey cases', t => {
for (const el of falseyCases) {
t.false(yn(el));
t.false(yn(el, {lenient: true}));
for (const falseyCase of falseyCases) {
t.false(yn(falseyCase));
t.false(yn(falseyCase, {lenient: true}));
}
});

Expand Down Expand Up @@ -67,9 +67,9 @@ const nullCases = [
'unicorn'
];
test('null cases', t => {
for (const el of nullCases) {
t.is(yn(el), null);
t.is(yn(el, {lenient: true}), null);
for (const nullCase of nullCases) {
t.is(yn(nullCase), null);
t.is(yn(nullCase, {lenient: true}), null);
}
});

Expand All @@ -88,7 +88,9 @@ test('lenient option - falsey value cases', t => {
});

test('default option throws error if not a boolean type', t => {
t.throws(() => yn('10', {default: 10}), 'Expected the `default` option to be of type `boolean`, got `number`');
t.throws(() => {
yn('10', {default: 10});
}, 'Expected the `default` option to be of type `boolean`, got `number`');
});

test('default option', t => {
Expand Down

0 comments on commit bbc3121

Please sign in to comment.