Skip to content

Commit

Permalink
Add TypeScript definition (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 9, 2019
1 parent 7a352c0 commit e0537f0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
53 changes: 53 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
declare namespace camelcaseKeys {
interface Options {
/**
Recurse nested objects and objects in arrays.
@default false
*/
readonly deep?: boolean;

/**
Exclude keys from being camelCased.
@default []
*/
readonly exclude?: Array<string | RegExp>;
}
}

/**
Convert object keys to camelCase using [`camelcase`](https://github.com/sindresorhus/camelcase).
@param input - Object or array of objects to camelCase.
@example
```
import camelcaseKeys = require('camelcase-keys');
// Convert an object
camelcaseKeys({'foo-bar': true});
//=> {fooBar: true}
// Convert an array of objects
camelcaseKeys([{'foo-bar': true}, {'bar-foo': false}]);
//=> [{fooBar: true}, {barFoo: false}]
camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true});
//=> {fooBar: true, nested: {unicornRainbow: true}}
import minimist = require('minimist');
const argv = minimist(process.argv.slice(2));
//=> {_: [], 'foo-bar': true}
camelcaseKeys(argv);
//=> {_: [], fooBar: true}
```
*/
declare function camelcaseKeys(
input: unknown,
options?: camelcaseKeys.Options
): {[key: string]: unknown};

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

expectType<{[key: string]: unknown}>(camelcaseKeys({'foo-bar': true}));
expectType<{[key: string]: unknown}>(
camelcaseKeys({'foo-bar': true}, {deep: true})
);
expectType<{[key: string]: unknown}>(
camelcaseKeys({'foo-bar': true}, {exclude: ['foo', /bar/]})
);
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava",
"test": "xo && ava && tsd",
"bench": "matcha bench/bench.js"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"map",
Expand Down Expand Up @@ -46,14 +47,15 @@
"recursive"
],
"dependencies": {
"camelcase": "^5.0.0",
"camelcase": "^5.3.1",
"map-obj": "^3.0.0",
"quick-lru": "^1.0.0"
},
"devDependencies": {
"ava": "^0.25.0",
"ava": "^1.4.1",
"matcha": "^0.7.0",
"xo": "^0.23.0"
"tsd": "^0.7.2",
"xo": "^0.24.0"
},
"xo": {
"overrides": [
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Type: `Object`

##### exclude

Type: `Array<string|RegExp>`<br>
Type: `Array<string | RegExp>`<br>
Default: `[]`

Exclude keys from being camelCased.
Expand Down

0 comments on commit e0537f0

Please sign in to comment.