Comparing version 3.1.1 to 4.0.0
@@ -13,5 +13,5 @@ declare namespace yn { | ||
@default null | ||
@default undefined | ||
*/ | ||
readonly default?: boolean | null; | ||
readonly default?: boolean | undefined; | ||
} | ||
@@ -24,44 +24,36 @@ | ||
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`, '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. | ||
@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'); | ||
//=> undefined | ||
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; | ||
yn('mo', {lenient: true}); | ||
//=> false | ||
``` | ||
*/ | ||
declare function yn(input: unknown, options: yn.OptionsWithDefault): boolean; | ||
declare function yn(input: unknown, options?: yn.Options): boolean | undefined; | ||
// 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; | ||
}; | ||
export = yn; |
30
index.js
'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_ | ||
} = {}) => { | ||
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_ !== undefined && 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|on)$/i.test(value)) { | ||
return true; | ||
} | ||
if (/^(?:n|no|false|0)$/i.test(input)) { | ||
if (/^(?:n|no|false|0|off)$/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; |
@@ -95,3 +95,3 @@ 'use strict'; | ||
module.exports = (input, options) => { | ||
module.exports = (input, default_) => { | ||
if (getYesMatchScore(input) >= YES_MATCH_SCORE_THRESHOLD) { | ||
@@ -105,3 +105,3 @@ return true; | ||
return options.default; | ||
return default_; | ||
}; |
{ | ||
"name": "yn", | ||
"version": "3.1.1", | ||
"version": "4.0.0", | ||
"description": "Parse yes/no like values", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=10" | ||
}, | ||
@@ -39,6 +39,6 @@ "scripts": { | ||
"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" | ||
} | ||
} |
@@ -12,3 +12,3 @@ # yn [![Build Status](https://travis-ci.org/sindresorhus/yn.svg?branch=master)](https://travis-ci.org/sindresorhus/yn) | ||
```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' | ||
``` | ||
@@ -41,3 +41,3 @@ | ||
yn('abomasum'); | ||
//=> null | ||
//=> undefined | ||
@@ -51,3 +51,3 @@ yn('abomasum', {default: false}); | ||
Unrecognized values return `null`. | ||
Unrecognized values return `undefined`. | ||
@@ -57,7 +57,7 @@ | ||
### yn(input, [options]) | ||
### yn(input, options?) | ||
#### input | ||
Type: `any` | ||
Type: `unknown` | ||
@@ -68,7 +68,7 @@ Value that should be converted. | ||
Type: `Object` | ||
Type: `object` | ||
##### lenient | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `false` | ||
@@ -80,10 +80,5 @@ | ||
Type: `boolean`<br> | ||
Default: `null` | ||
Type: `boolean`\ | ||
Default: `undefined` | ||
Default value if no match was found. | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
5890
151
79