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 5, 2019
1 parent 50483ce commit 9408184
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 14 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .travis.yml
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
6 changes: 6 additions & 0 deletions index.d.ts
@@ -0,0 +1,6 @@
/**
* Capitalize every word in a string: `unicorn cake` → `Unicorn Cake`.
*
* @param input - The string to titleize.
*/
export default function titleize(input: string): string;
5 changes: 4 additions & 1 deletion index.js
@@ -1,8 +1,11 @@
'use strict';
module.exports = input => {
const titleize = input => {
if (typeof input !== 'string') {
throw new TypeError('Expected a string');
}

return input.toLowerCase().replace(/(?:^|\s|-)\S/g, x => x.toUpperCase());
};

module.exports = titleize;
module.exports.default = titleize;
4 changes: 4 additions & 0 deletions index.test-d.ts
@@ -0,0 +1,4 @@
import {expectType} from 'tsd-check';
import titleize from '.';

expectType<string>(titleize('foo bar'));
10 changes: 6 additions & 4 deletions package.json
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": [
"titleize",
Expand All @@ -31,7 +32,8 @@
"convert"
],
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.2.1",
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
}
}
14 changes: 7 additions & 7 deletions test.js
@@ -1,11 +1,11 @@
import test from 'ava';
import m from '.';
import titleize 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('unicorns-and-rainbows'), 'Unicorns-And-Rainbows');
t.is(m('UNICORNS-AND-RAINBOWS'), 'Unicorns-And-Rainbows');
t.is(m('unicorns and rainbows'), 'Unicorns And Rainbows');
t.is(titleize(''), '');
t.is(titleize('unicorns and rainbows'), 'Unicorns And Rainbows');
t.is(titleize('UNICORNS AND RAINBOWS'), 'Unicorns And Rainbows');
t.is(titleize('unicorns-and-rainbows'), 'Unicorns-And-Rainbows');
t.is(titleize('UNICORNS-AND-RAINBOWS'), 'Unicorns-And-Rainbows');
t.is(titleize('unicorns and rainbows'), 'Unicorns And Rainbows');
});

0 comments on commit 9408184

Please sign in to comment.