Skip to content

Commit a55fd57

Browse files
committed
Merge pull request #29 from cloudfour/feat-capitalize
Add capitalize helpers
2 parents 7c27c34 + a668a49 commit a55fd57

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

lib/capitalize.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
var R = require('ramda');
4+
var Capitalize = require('capitalize');
5+
6+
/**
7+
* Capitalize the first letter of a String.
8+
*
9+
* @since 0.4.0
10+
* @param {String|*} str - String to capitalize. Other types will be converted.
11+
* @return {String}
12+
* @example:
13+
*
14+
* {{capitalize "hello world"}} //=> "Hello world"
15+
*/
16+
17+
module.exports = function capitalize (str) {
18+
if (R.isNil(str)) {
19+
throw new Error('The "capitalize" helper requires one argument.')
20+
}
21+
22+
if (!R.is(String, str)) {
23+
str = str.toString();
24+
}
25+
26+
return Capitalize(str);
27+
};

lib/capitalizeWords.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
var R = require('ramda');
4+
var Capitalize = require('capitalize');
5+
6+
/**
7+
* Capitalize each word in a String. Works with punctuation and international
8+
* characters.
9+
*
10+
* @since 0.4.0
11+
* @param {String|*} str - String to capitalize. Other types will be converted.
12+
* @return {String}
13+
* @example:
14+
*
15+
* {{capitalizeWords "hello world"}} //=> "Hello World"
16+
* {{capitalizeWords "hello-cañapolísas"}} //=> "Hello-Cañapolísas"
17+
* {{capitalizeWords "it's a nice day"}} //=> "It's A Nice Day"
18+
*/
19+
20+
module.exports = function capitalizeWords (str) {
21+
if (R.isNil(str)) {
22+
throw new Error('The "capitalizeWords" helper requires one argument.')
23+
}
24+
25+
if (!R.is(String, str)) {
26+
str = str.toString();
27+
}
28+
29+
return Capitalize.words(str);
30+
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"homepage": "https://github.com/cloudfour/core-hbs-helpers",
2929
"dependencies": {
30+
"capitalize": "^1.0.0",
3031
"chance": "^1.0.3",
3132
"handlebars": "^4.0.3",
3233
"moment": "^2.10.6",

test/capitalize.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
var capitalize = require('../').capitalize;
4+
var tape = require('tape');
5+
var Handlebars = require('handlebars');
6+
7+
Handlebars.registerHelper(capitalize.name, capitalize);
8+
9+
tape('capitalize', function (test) {
10+
var template = Handlebars.compile('{{capitalize content}}');
11+
var expected;
12+
var actual;
13+
14+
test.plan(3);
15+
16+
expected = 'Hello world';
17+
actual = template({ content: 'hello world' });
18+
test.equal(actual, expected, 'Works');
19+
20+
expected = 'True';
21+
actual = template({ content: true });
22+
test.equal(actual, expected, 'Works with non-String arguments');
23+
24+
test.throws(
25+
function () {
26+
template();
27+
},
28+
/requires one argument\.$/,
29+
'Errors when argument is missing'
30+
);
31+
});

test/capitalizeWords.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
var capitalizeWords = require('../').capitalizeWords;
4+
var tape = require('tape');
5+
var Handlebars = require('handlebars');
6+
7+
Handlebars.registerHelper(capitalizeWords.name, capitalizeWords);
8+
9+
tape('capitalizeWords', function (test) {
10+
var template = Handlebars.compile('{{{capitalizeWords content}}}');
11+
var expected;
12+
var actual;
13+
14+
test.plan(3);
15+
16+
expected = '"How\'s It Going?"';
17+
actual = template({ content: '"how\'s it going?"' });
18+
test.equal(actual, expected, 'Works');
19+
20+
expected = 'A,B,C';
21+
actual = template({ content: ['a', 'b', 'c'] });
22+
test.equal(actual, expected, 'Works with non-String arguments');
23+
24+
test.throws(
25+
function () {
26+
template();
27+
},
28+
/requires one argument\.$/,
29+
'Errors when argument is missing'
30+
);
31+
});

0 commit comments

Comments
 (0)