Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25870cd
commit f790df7
Showing
4 changed files
with
41 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export interface Options { | ||
/** | ||
* Use a key distance-based score to leniently accept typos of `yes` and `no`. | ||
* | ||
* @default false | ||
*/ | ||
lenient?: boolean; | ||
|
||
/** | ||
* Default value if no match was found. | ||
* | ||
* @default null | ||
*/ | ||
default?: boolean | null; | ||
} | ||
|
||
/** | ||
* Parse yes/no like values. | ||
* The following case-insensitive values are recognized: `'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0` | ||
* | ||
* @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. | ||
*/ | ||
export default function yn(input: any, options?: Options): boolean | null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,5 @@ module.exports = (val, opts) => { | |
|
||
return opts.default; | ||
}; | ||
|
||
module.exports.default = module.exports; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {expectType} from 'tsd-check'; | ||
import yn from '.'; | ||
|
||
// Should use null as default when no options are given | ||
expectType<boolean | null>(yn('true')); | ||
|
||
// Should use the default type when given | ||
expectType<boolean | null>(yn('true', {default: null})); | ||
|
||
// Should use null as default when only the lenient option is given | ||
expectType<boolean | null>(yn('true', {lenient: true})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters