-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor TypeScript definition to CommonJS compatible export (#23)
- Loading branch information
1 parent
723245e
commit 6fb9f33
Showing
4 changed files
with
72 additions
and
35 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 |
---|---|---|
@@ -1,24 +1,65 @@ | ||
export interface Options { | ||
/** | ||
* Use a key distance-based score to leniently accept typos of `yes` and `no`. | ||
* | ||
* @default false | ||
*/ | ||
lenient?: boolean; | ||
declare namespace yn { | ||
interface Options { | ||
/** | ||
Use a key distance-based score to leniently accept typos of `yes` and `no`. | ||
/** | ||
* Default value if no match was found. | ||
* | ||
* @default null | ||
*/ | ||
default?: boolean | null; | ||
@default false | ||
*/ | ||
readonly lenient?: boolean; | ||
|
||
/** | ||
Default value if no match was found. | ||
@default null | ||
*/ | ||
readonly default?: boolean | null; | ||
} | ||
|
||
interface OptionsWithDefault extends Options { | ||
default: boolean; | ||
} | ||
} | ||
|
||
/** | ||
* 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; | ||
declare const yn: { | ||
/** | ||
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. | ||
@example | ||
``` | ||
import yn = require('yn'); | ||
yn('y'); | ||
//=> true | ||
yn('NO'); | ||
//=> false | ||
yn(true); | ||
//=> true | ||
yn('abomasum'); | ||
//=> null | ||
yn('abomasum', {default: false}); | ||
//=> false | ||
yn('mo', {lenient: true}); | ||
//=> false | ||
``` | ||
*/ | ||
(input: unknown, options: yn.OptionsWithDefault): boolean; | ||
(input: unknown, options?: yn.Options): boolean | null; | ||
|
||
// TODO: Remove this for the next major release, refactor the whole definition to: | ||
// declare function yn(input: unknown, options: yn.OptionsWithDefault): boolean; | ||
// declare function yn(input: unknown, options?: yn.Options): boolean | null; | ||
// export = yn; | ||
default: typeof yn; | ||
}; | ||
|
||
export = yn; |
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
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 |
---|---|---|
@@ -1,11 +1,6 @@ | ||
import {expectType} from 'tsd-check'; | ||
import yn from '.'; | ||
import {expectType} from 'tsd'; | ||
import yn = require('.'); | ||
|
||
// 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})); | ||
expectType<boolean | null>(yn('y')); | ||
expectType<boolean | null>(yn('mo', {lenient: true})); | ||
expectType<boolean>(yn('abomasum', {default: false})); |
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