Skip to content

Commit

Permalink
Return undefined instead of null when no match
Browse files Browse the repository at this point in the history
sindresorhus committed Dec 1, 2019
1 parent bbc3121 commit b1cd76b
Showing 5 changed files with 22 additions and 22 deletions.
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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_}\``);
}

4 changes: 2 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expectType} from 'tsd';
import yn = require('.');

expectType<boolean | null>(yn('y'));
expectType<boolean | null>(yn('mo', {lenient: true}));
expectType<boolean | undefined>(yn('y'));
expectType<boolean | undefined>(yn('mo', {lenient: true}));
expectType<boolean>(yn('abomasum', {default: false}));
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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.
22 changes: 11 additions & 11 deletions test.js
Original file line number Diff line number Diff line change
@@ -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);
}
});

0 comments on commit b1cd76b

Please sign in to comment.