Skip to content

Commit

Permalink
Refactor TypeScript definition to CommonJS compatible export (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 6, 2019
1 parent 723245e commit 6fb9f33
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 35 deletions.
83 changes: 62 additions & 21 deletions index.d.ts
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;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ const yn = (input, options) => {
};

module.exports = yn;
// TODO: Remove this for the next major release
module.exports.default = yn;
15 changes: 5 additions & 10 deletions index.test-d.ts
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}));
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava && tsd-check"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
Expand All @@ -35,8 +35,8 @@
"lenient"
],
"devDependencies": {
"ava": "^0.25.0",
"tsd-check": "^0.2.1",
"xo": "^0.23.0"
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}

0 comments on commit 6fb9f33

Please sign in to comment.