Skip to content

Commit

Permalink
Add TypeScript definition (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 6, 2019
1 parent 76c994a commit 5fadce6
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 17 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Convert a camelized/dasherized/underscored string into a humanized one: `fooBar-Baz_Faz` → `Foo bar baz faz`.
*
* @param text - The string to make human readable.
*/
export default function humanizeString(text: string): string;
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const decamelize = require('decamelize');

module.exports = input => {
const humanizeString = input => {
if (typeof input !== 'string') {
throw new TypeError('Expected a string');
}
Expand All @@ -12,3 +12,6 @@ module.exports = input => {

return input;
};

module.exports = humanizeString;
module.exports.default = humanizeString;
4 changes: 4 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {expectType} from 'tsd-check';
import humanizeString from '.';

expectType<string>(humanizeString('fooBar'));
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"humanize",
Expand All @@ -37,7 +38,8 @@
"decamelize": "^2.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.2.1",
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
}
}
20 changes: 10 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import test from 'ava';
import m from '.';
import humanizeString from '.';

test('main', t => {
t.is(m(''), '');
t.is(m('unicorns and rainbows'), 'Unicorns and rainbows');
t.is(m('unicorns-and-rainbows'), 'Unicorns and rainbows');
t.is(m('UnicornsAndRainbows'), 'Unicorns and rainbows');
t.is(m('unicornsAndRainbows'), 'Unicorns and rainbows');
t.is(m('unicorns_and_rainbows'), 'Unicorns and rainbows');
t.is(m('-UnicornsAndRainbows'), 'Unicorns and rainbows');
t.is(m(' unicorns and rainbows '), 'Unicorns and rainbows');
t.is(m('unicorns_and-Rainbows_andPonies '), 'Unicorns and rainbows and ponies');
t.is(humanizeString(''), '');
t.is(humanizeString('unicorns and rainbows'), 'Unicorns and rainbows');
t.is(humanizeString('unicorns-and-rainbows'), 'Unicorns and rainbows');
t.is(humanizeString('UnicornsAndRainbows'), 'Unicorns and rainbows');
t.is(humanizeString('unicornsAndRainbows'), 'Unicorns and rainbows');
t.is(humanizeString('unicorns_and_rainbows'), 'Unicorns and rainbows');
t.is(humanizeString('-UnicornsAndRainbows'), 'Unicorns and rainbows');
t.is(humanizeString(' unicorns and rainbows '), 'Unicorns and rainbows');
t.is(humanizeString('unicorns_and-Rainbows_andPonies '), 'Unicorns and rainbows and ponies');
});

0 comments on commit 5fadce6

Please sign in to comment.