diff --git a/.travis.yml b/.travis.yml
index 2ae9d62..94ab01f 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,5 +1,4 @@
language: node_js
node_js:
+ - '12'
- '10'
- - '8'
- - '6'
diff --git a/index.d.ts b/index.d.ts
index c72a414..630313c 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -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;
diff --git a/index.js b/index.js
index a5a24c1..6f61522 100644
--- a/index.js
+++ b/index.js
@@ -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;
diff --git a/lenient.js b/lenient.js
index 51cd836..68012d7 100644
--- a/lenient.js
+++ b/lenient.js
@@ -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;
}
@@ -101,5 +101,5 @@ module.exports = (input, options) => {
return false;
}
- return options.default;
+ return default_;
};
diff --git a/package.json b/package.json
index 47d86f2..ff95e59 100644
--- a/package.json
+++ b/package.json
@@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
- "node": ">=6"
+ "node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
@@ -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"
}
}
diff --git a/readme.md b/readme.md
index 7be3114..1a66017 100644
--- a/readme.md
+++ b/readme.md
@@ -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`
+Type: `boolean`\
Default: `false`
Use a key distance-based score to leniently accept typos of `yes` and `no`.
##### default
-Type: `boolean`
+Type: `boolean`\
Default: `null`
Default value if no match was found.
-
-
-## License
-
-MIT © [Sindre Sorhus](https://sindresorhus.com)
diff --git a/test.js b/test.js
index 2611a67..3f95d2d 100644
--- a/test.js
+++ b/test.js
@@ -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}));
}
});
@@ -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}));
}
});
@@ -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);
}
});
@@ -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 => {