From b1cd76b93bc525e77527da6a07b458183febd43b Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 1 Dec 2019 16:12:38 +0700 Subject: [PATCH] Return `undefined` instead of `null` when no match --- index.d.ts | 8 ++++---- index.js | 4 ++-- index.test-d.ts | 4 ++-- readme.md | 6 +++--- test.js | 22 +++++++++++----------- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/index.d.ts b/index.d.ts index 630313c..6e1a5b4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -10,9 +10,9 @@ declare namespace yn { /** Default value if no match was found. - @default null + @default undefined */ - readonly default?: boolean | null; + readonly default?: boolean | undefined; } interface OptionsWithDefault extends Options { @@ -42,7 +42,7 @@ yn(true); //=> true yn('abomasum'); -//=> null +//=> undefined yn('abomasum', {default: false}); //=> false @@ -52,6 +52,6 @@ yn('mo', {lenient: true}); ``` */ declare function yn(input: unknown, options: yn.OptionsWithDefault): boolean; -declare function yn(input: unknown, options?: yn.Options): boolean | null; +declare function yn(input: unknown, options?: yn.Options): boolean | undefined; export = yn; diff --git a/index.js b/index.js index 6f61522..daef73b 100644 --- a/index.js +++ b/index.js @@ -3,11 +3,11 @@ const lenientFunction = require('./lenient'); const yn = (value, { lenient = false, - default: default_ = null + default: default_ } = {}) => { value = String(value).trim(); - if (default_ !== null && typeof default_ !== 'boolean') { + if (default_ !== undefined && typeof default_ !== 'boolean') { throw new TypeError(`Expected the \`default\` option to be of type \`boolean\`, got \`${typeof default_}\``); } diff --git a/index.test-d.ts b/index.test-d.ts index 220701d..92ce6d5 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,6 +1,6 @@ import {expectType} from 'tsd'; import yn = require('.'); -expectType(yn('y')); -expectType(yn('mo', {lenient: true})); +expectType(yn('y')); +expectType(yn('mo', {lenient: true})); expectType(yn('abomasum', {default: false})); diff --git a/readme.md b/readme.md index 1a66017..ccdfaf6 100644 --- a/readme.md +++ b/readme.md @@ -37,7 +37,7 @@ yn(true); //=> true yn('abomasum'); -//=> null +//=> undefined yn('abomasum', {default: false}); //=> false @@ -46,7 +46,7 @@ yn('mo', {lenient: true}); //=> false ``` -Unrecognized values return `null`. +Unrecognized values return `undefined`. ## API @@ -73,6 +73,6 @@ Use a key distance-based score to leniently accept typos of `yes` and `no`. ##### default Type: `boolean`\ -Default: `null` +Default: `undefined` Default value if no match was found. diff --git a/test.js b/test.js index 3f95d2d..44367ef 100644 --- a/test.js +++ b/test.js @@ -15,9 +15,9 @@ const truthyCases = [ 1 ]; test('truthy cases', t => { - for (const truthyCase of truthyCases) { - t.true(yn(truthyCase)); - t.true(yn(truthyCase, {lenient: true})); + for (const case_ of truthyCases) { + t.true(yn(case_)); + t.true(yn(case_, {lenient: true})); } }); @@ -35,13 +35,13 @@ const falseyCases = [ 0 ]; test('falsey cases', t => { - for (const falseyCase of falseyCases) { - t.false(yn(falseyCase)); - t.false(yn(falseyCase, {lenient: true})); + for (const case_ of falseyCases) { + t.false(yn(case_)); + t.false(yn(case_, {lenient: true})); } }); -const nullCases = [ +const undefinedCases = [ // Falsey cases that don't work NaN, null, @@ -66,10 +66,10 @@ const nullCases = [ // Other 'unicorn' ]; -test('null cases', t => { - for (const nullCase of nullCases) { - t.is(yn(nullCase), null); - t.is(yn(nullCase, {lenient: true}), null); +test('undefined cases', t => { + for (const case_ of undefinedCases) { + t.is(yn(case_), undefined); + t.is(yn(case_, {lenient: true}), undefined); } });