Comparing version 2.0.0 to 3.0.0
26
index.js
'use strict'; | ||
const lenient = require('./lenient'); | ||
module.exports = (val, opts) => { | ||
val = String(val).trim(); | ||
opts = Object.assign({ | ||
const yn = (input, options) => { | ||
input = String(input).trim(); | ||
options = Object.assign({ | ||
lenient: false, | ||
default: null | ||
}, opts); | ||
}, options); | ||
if (opts.default !== null && typeof opts.default !== 'boolean') { | ||
throw new TypeError(`Expected the \`default\` option to be of type \`boolean\`, got \`${typeof opts.default}\``); | ||
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 (/^(?:y|yes|true|1)$/i.test(val)) { | ||
if (/^(?:y|yes|true|1)$/i.test(input)) { | ||
return true; | ||
} | ||
if (/^(?:n|no|false|0)$/i.test(val)) { | ||
if (/^(?:n|no|false|0)$/i.test(input)) { | ||
return false; | ||
} | ||
if (opts.lenient === true) { | ||
return lenient(val, opts); | ||
if (options.lenient === true) { | ||
return lenient(input, options); | ||
} | ||
return opts.default; | ||
return options.default; | ||
}; | ||
module.exports = yn; | ||
module.exports.default = yn; |
@@ -61,7 +61,5 @@ 'use strict'; | ||
function getYesMatchScore(val) { | ||
function getYesMatchScore(value) { | ||
const [y, e, s] = value; | ||
let score = 0; | ||
const y = val[0]; | ||
const e = val[1]; | ||
const s = val[2]; | ||
@@ -83,6 +81,5 @@ if (yMatch.has(y)) { | ||
function getNoMatchScore(val) { | ||
function getNoMatchScore(value) { | ||
const [n, o] = value; | ||
let score = 0; | ||
const n = val[0]; | ||
const o = val[1]; | ||
@@ -100,12 +97,12 @@ if (nMatch.has(n)) { | ||
module.exports = (val, opts) => { | ||
if (getYesMatchScore(val) >= YES_MATCH_SCORE_THRESHOLD) { | ||
module.exports = (input, options) => { | ||
if (getYesMatchScore(input) >= YES_MATCH_SCORE_THRESHOLD) { | ||
return true; | ||
} | ||
if (getNoMatchScore(val) >= NO_MATCH_SCORE_THRESHOLD) { | ||
if (getNoMatchScore(input) >= NO_MATCH_SCORE_THRESHOLD) { | ||
return false; | ||
} | ||
return opts.default; | ||
return options.default; | ||
}; |
{ | ||
"name": "yn", | ||
"version": "2.0.0", | ||
"description": "Parse yes/no like values", | ||
"license": "MIT", | ||
"repository": "sindresorhus/yn", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"contributors": [ | ||
"Justin Woo <moomoowoo@gmail.com>", | ||
"Mark Stosberg <mark@rideamigos.com>" | ||
], | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"lenient.js" | ||
], | ||
"keywords": [ | ||
"yn", | ||
"yes", | ||
"no", | ||
"cli", | ||
"prompt", | ||
"validate", | ||
"input", | ||
"answer", | ||
"true", | ||
"false", | ||
"parse", | ||
"lenient" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
} | ||
"name": "yn", | ||
"version": "3.0.0", | ||
"description": "Parse yes/no like values", | ||
"license": "MIT", | ||
"repository": "sindresorhus/yn", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && tsd-check" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"lenient.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"yn", | ||
"yes", | ||
"no", | ||
"cli", | ||
"prompt", | ||
"validate", | ||
"input", | ||
"answer", | ||
"true", | ||
"false", | ||
"parse", | ||
"lenient" | ||
], | ||
"devDependencies": { | ||
"ava": "^0.25.0", | ||
"tsd-check": "^0.2.1", | ||
"xo": "^0.23.0" | ||
} | ||
} |
@@ -7,3 +7,3 @@ # yn [![Build Status](https://travis-ci.org/sindresorhus/yn.svg?branch=master)](https://travis-ci.org/sindresorhus/yn) | ||
- | ||
--- | ||
@@ -22,3 +22,3 @@ The following case-insensitive values are recognized: | ||
``` | ||
$ npm install --save yn | ||
$ npm install yn | ||
``` | ||
@@ -66,3 +66,3 @@ | ||
Type: `object` | ||
Type: `Object` | ||
@@ -86,2 +86,2 @@ ##### lenient | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
Sorry, the diff of this file is not supported yet
5588
6
133
3