Skip to content

Commit

Permalink
Add TypeScript definition (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
leon19 authored and sindresorhus committed Dec 8, 2018
1 parent 25870cd commit f790df7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
24 changes: 24 additions & 0 deletions index.d.ts
@@ -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;
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -26,3 +26,5 @@ module.exports = (val, opts) => {

return opts.default;
};

module.exports.default = module.exports;
11 changes: 11 additions & 0 deletions index.test-d.ts
@@ -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}));
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -17,11 +17,12 @@
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd-check"
},
"files": [
"index.js",
"lenient.js"
"lenient.js",
"index.d.ts"
],
"keywords": [
"yn",
Expand All @@ -39,6 +40,7 @@
],
"devDependencies": {
"ava": "*",
"tsd-check": "*",
"xo": "*"
}
}

0 comments on commit f790df7

Please sign in to comment.