├── .editorconfig ├── .gitattributes ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── lenient.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/yn 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: actions/setup-node@v2 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - run: npm install 20 | - run: npm test 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export interface Options { 2 | /** 3 | Use a key distance-based score to leniently accept typos of `yes` and `no`. 4 | 5 | @default false 6 | */ 7 | readonly lenient?: boolean; 8 | 9 | /** 10 | The default value if no match was found. 11 | 12 | @default undefined 13 | */ 14 | readonly default?: boolean | undefined; 15 | } 16 | 17 | export interface OptionsWithDefault extends Options { 18 | readonly default: boolean; 19 | } 20 | 21 | /** 22 | Parse yes/no like values. 23 | 24 | The following case-insensitive values are recognized: `'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0`, 'on', 'off' 25 | 26 | @param input - The value that should be converted. 27 | @returns The parsed input if it can be parsed or the default value defined in the `default` option. 28 | 29 | @example 30 | ``` 31 | import yn from 'yn'; 32 | 33 | yn('y'); 34 | //=> true 35 | 36 | yn('NO'); 37 | //=> false 38 | 39 | yn(true); 40 | //=> true 41 | 42 | yn('abomasum'); 43 | //=> undefined 44 | 45 | yn('abomasum', {default: false}); 46 | //=> false 47 | 48 | yn('mo', {lenient: true}); 49 | //=> false 50 | ``` 51 | */ 52 | export default function yn(input: unknown, options: OptionsWithDefault): boolean; 53 | export default function yn(input: unknown, options?: Options): boolean | undefined; 54 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import lenientFunction from './lenient.js'; 2 | 3 | export default function yn(value, { 4 | lenient = false, 5 | default: default_, 6 | } = {}) { 7 | if (default_ !== undefined && typeof default_ !== 'boolean') { 8 | throw new TypeError(`Expected the \`default\` option to be of type \`boolean\`, got \`${typeof default_}\``); 9 | } 10 | 11 | if (value === undefined || value === null) { 12 | return default_; 13 | } 14 | 15 | value = String(value).trim(); 16 | 17 | if (/^(?:y|yes|true|1|on)$/i.test(value)) { 18 | return true; 19 | } 20 | 21 | if (/^(?:n|no|false|0|off)$/i.test(value)) { 22 | return false; 23 | } 24 | 25 | if (lenient === true) { 26 | return lenientFunction(value, default_); 27 | } 28 | 29 | return default_; 30 | } 31 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import yn from './index.js'; 3 | 4 | expectType(yn('y')); 5 | expectType(yn('mo', {lenient: true})); 6 | expectType(yn('abomasum', {default: false})); 7 | -------------------------------------------------------------------------------- /lenient.js: -------------------------------------------------------------------------------- 1 | const YES_MATCH_SCORE_THRESHOLD = 2; 2 | const NO_MATCH_SCORE_THRESHOLD = 1.25; 3 | 4 | const yMatch = new Map([ 5 | [5, 0.25], 6 | [6, 0.25], 7 | [7, 0.25], 8 | ['t', 0.75], 9 | ['y', 1], 10 | ['u', 0.75], 11 | ['g', 0.25], 12 | ['h', 0.25], 13 | ['j', 0.25], 14 | ]); 15 | 16 | // eslint-disable-next-line unicorn/prevent-abbreviations 17 | const eMatch = new Map([ 18 | [2, 0.25], 19 | [3, 0.25], 20 | [4, 0.25], 21 | ['w', 0.75], 22 | ['e', 1], 23 | ['r', 0.75], 24 | ['s', 0.25], 25 | ['d', 0.25], 26 | ['f', 0.25], 27 | ]); 28 | 29 | const sMatch = new Map([ 30 | ['q', 0.25], 31 | ['w', 0.25], 32 | ['e', 0.25], 33 | ['a', 0.75], 34 | ['s', 1], 35 | ['d', 0.75], 36 | ['z', 0.25], 37 | ['x', 0.25], 38 | ['c', 0.25], 39 | ]); 40 | 41 | const nMatch = new Map([ 42 | ['h', 0.25], 43 | ['j', 0.25], 44 | ['k', 0.25], 45 | ['b', 0.75], 46 | ['n', 1], 47 | ['m', 0.75], 48 | ]); 49 | 50 | const oMatch = new Map([ 51 | [9, 0.25], 52 | [0, 0.25], 53 | ['i', 0.75], 54 | ['o', 1], 55 | ['p', 0.75], 56 | ['k', 0.25], 57 | ['l', 0.25], 58 | ]); 59 | 60 | function getYesMatchScore(value) { 61 | // eslint-disable-next-line unicorn/prevent-abbreviations 62 | const [y, e, s] = value; 63 | let score = 0; 64 | 65 | if (yMatch.has(y)) { 66 | score += yMatch.get(y); 67 | } 68 | 69 | if (eMatch.has(e)) { 70 | score += eMatch.get(e); 71 | } 72 | 73 | if (sMatch.has(s)) { 74 | score += sMatch.get(s); 75 | } 76 | 77 | return score; 78 | } 79 | 80 | function getNoMatchScore(value) { 81 | const [n, o] = value; 82 | let score = 0; 83 | 84 | if (nMatch.has(n)) { 85 | score += nMatch.get(n); 86 | } 87 | 88 | if (oMatch.has(o)) { 89 | score += oMatch.get(o); 90 | } 91 | 92 | return score; 93 | } 94 | 95 | export default function lenient(input, default_) { 96 | if (getYesMatchScore(input) >= YES_MATCH_SCORE_THRESHOLD) { 97 | return true; 98 | } 99 | 100 | if (getNoMatchScore(input) >= NO_MATCH_SCORE_THRESHOLD) { 101 | return false; 102 | } 103 | 104 | return default_; 105 | } 106 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "yn", 3 | "version": "5.0.0", 4 | "description": "Parse yes/no like values", 5 | "license": "MIT", 6 | "repository": "sindresorhus/yn", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "lenient.js", 24 | "index.d.ts" 25 | ], 26 | "keywords": [ 27 | "yn", 28 | "yes", 29 | "no", 30 | "cli", 31 | "prompt", 32 | "validate", 33 | "input", 34 | "answer", 35 | "true", 36 | "false", 37 | "parse", 38 | "lenient" 39 | ], 40 | "devDependencies": { 41 | "ava": "^3.15.0", 42 | "tsd": "^0.17.0", 43 | "xo": "^0.44.0" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # yn 2 | 3 | > Parse yes/no like values 4 | 5 | Useful for validating answers of a CLI prompt. 6 | 7 | --- 8 | 9 | The following case-insensitive values are recognized: 10 | 11 | ```js 12 | 'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0, 'on', 'off' 13 | ``` 14 | 15 | *Enable lenient mode to gracefully handle typos.* 16 | 17 | ## Install 18 | 19 | ``` 20 | $ npm install yn 21 | ``` 22 | 23 | ## Usage 24 | 25 | ```js 26 | import yn from 'yn'; 27 | 28 | yn('y'); 29 | //=> true 30 | 31 | yn('NO'); 32 | //=> false 33 | 34 | yn(true); 35 | //=> true 36 | 37 | yn('abomasum'); 38 | //=> undefined 39 | 40 | yn('abomasum', {default: false}); 41 | //=> false 42 | 43 | yn('mo', {lenient: true}); 44 | //=> false 45 | ``` 46 | 47 | Unrecognized values return `undefined`. 48 | 49 | ## API 50 | 51 | ### yn(input, options?) 52 | 53 | #### input 54 | 55 | Type: `unknown` 56 | 57 | The value that should be converted. 58 | 59 | #### options 60 | 61 | Type: `object` 62 | 63 | ##### lenient 64 | 65 | Type: `boolean`\ 66 | Default: `false` 67 | 68 | Use a key distance-based score to leniently accept typos of `yes` and `no`. 69 | 70 | ##### default 71 | 72 | Type: `boolean`\ 73 | Default: `undefined` 74 | 75 | The default value if no match was found. 76 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import yn from './index.js'; 3 | 4 | const truthyCases = [ 5 | 'y', 6 | 'Y', 7 | 'yes', 8 | 'YES', 9 | 'Yes', 10 | 'true', 11 | 'TRUE', 12 | 'True', 13 | true, 14 | '1', 15 | 1, 16 | 'on', 17 | ]; 18 | test('truthy cases', t => { 19 | for (const case_ of truthyCases) { 20 | t.true(yn(case_)); 21 | t.true(yn(case_, {lenient: true})); 22 | } 23 | }); 24 | 25 | const falseyCases = [ 26 | 'n', 27 | 'N', 28 | 'no', 29 | 'NO', 30 | 'No', 31 | 'false', 32 | 'FALSE', 33 | 'False', 34 | false, 35 | '0', 36 | 0, 37 | 'off', 38 | ]; 39 | test('falsey cases', t => { 40 | for (const case_ of falseyCases) { 41 | t.false(yn(case_)); 42 | t.false(yn(case_, {lenient: true})); 43 | } 44 | }); 45 | 46 | const undefinedCases = [ 47 | // Falsey cases that don't work 48 | Number.NaN, 49 | null, 50 | undefined, 51 | '', 52 | [], 53 | {}, 54 | // Numbers: only works on 0 and 1 55 | '10', 56 | 10, 57 | '-1', 58 | -1, 59 | '0.5', 60 | 0.5, 61 | '1BadIntParsing', 62 | '0x000', 63 | // Strings with a low edit-distance don't work 64 | 'flase', 65 | 'ture', 66 | 'n o', 67 | 'yn', 68 | // Other 69 | 'unicorn', 70 | ]; 71 | test('undefined cases', t => { 72 | for (const case_ of undefinedCases) { 73 | t.is(yn(case_), undefined); 74 | t.is(yn(case_, {lenient: true}), undefined); 75 | } 76 | }); 77 | 78 | test('lenient option - truthy value cases', t => { 79 | t.true(yn('ues', {lenient: true})); 80 | t.true(yn('ywa', {lenient: true})); 81 | t.true(yn('tes', {lenient: true})); 82 | t.true(yn('twa', {lenient: true})); 83 | t.true(yn('urd', {lenient: true})); 84 | }); 85 | 86 | test('lenient option - falsey value cases', t => { 87 | t.false(yn('ni', {lenient: true})); 88 | t.false(yn('bi', {lenient: true})); 89 | t.false(yn('mo', {lenient: true})); 90 | }); 91 | 92 | test('default option throws error if not a boolean type', t => { 93 | t.throws(() => { 94 | yn('10', {default: 10}); 95 | }, { 96 | message: 'Expected the `default` option to be of type `boolean`, got `number`', 97 | }); 98 | }); 99 | 100 | test('default option', t => { 101 | t.true(yn('10', {default: true})); 102 | t.false(yn('10', {default: false})); 103 | }); 104 | 105 | test('default option with lenient option', t => { 106 | t.true(yn('10', {default: true, lenient: true})); 107 | t.false(yn('10', {default: false, lenient: true})); 108 | }); 109 | --------------------------------------------------------------------------------