Skip to content

Commit

Permalink
add 'capitalize' and use it in 'to-camel-case'. Closes #47.
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikJoreteg committed Jan 5, 2015
1 parent 77ea42a commit 638d67b
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 3 deletions.
1 change: 1 addition & 0 deletions bin/test-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require('../modules/matches/test');
require('../modules/pairs/test');
require('../modules/property/test');
require('../modules/values/test');
require('../modules/capitalize/test');
require('../modules/escape/test');
require('../modules/includes/test');
require('../modules/to-camel-case/test');
Expand Down
1 change: 1 addition & 0 deletions bin/test-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require('../modules/matches/test');
require('../modules/pairs/test');
require('../modules/property/test');
require('../modules/values/test');
require('../modules/capitalize/test');
require('../modules/escape/test');
require('../modules/includes/test');
require('../modules/to-camel-case/test');
Expand Down
4 changes: 4 additions & 0 deletions modules.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@
}
],
"strings": [
{
"name": "capitalize",
"license": "amp"
},
{
"name": "escape",
"license": "underscore"
Expand Down
20 changes: 20 additions & 0 deletions modules/capitalize/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright © 2014 &yet, LLC and AmpersandJS contributors

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5 changes: 5 additions & 0 deletions modules/capitalize/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# amp-capitalize

See [the documentation](http://amp.ampersandjs.com#amp-capitalize) for more info.

Part of the [amp project](http://amp.ampersandjs.com#amp-capitalize), initially created by [@HenrikJoreteg](http://twitter.com/henrikjoreteg).
7 changes: 7 additions & 0 deletions modules/capitalize/capitalize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var isString = require('amp-is-string');


module.exports = function capitalize(str, uppercase) {
if (!isString(str)) return '';
return str.charAt(0)[uppercase !== false ? 'toUpperCase' : 'toLowerCase']() + str.slice(1);
};
1 change: 1 addition & 0 deletions modules/capitalize/doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Capitalizes first letter of a string. If the optinal second argument is `false` it will lowercase the first character instead of uppercasing it.
9 changes: 9 additions & 0 deletions modules/capitalize/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var capitalize = require('amp-capitalize');

capitalize('hi'); //=> 'Hi'

// does not trim or anything
capitalize(' hi'); //=> ' hi'

// pass `false` to force to lowercase instead
capitalize('Hi', false); //=> 'hi'
32 changes: 32 additions & 0 deletions modules/capitalize/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "amp-capitalize",
"description": "capitalize function part of http://amp.ampersandjs.com.",
"version": "1.0.0",
"author": "Henrik Joreteg <[email protected]>",
"amp": {
"size": {
"original": "38 B",
"minified": "7 B"
},
"internal": false
},
"bugs": "https://github.com/ampersandjs/amp/issues",
"devDependencies": {
"tape": "^3.0.3"
},
"homepage": "http://amp.ampersandjs.com/#amp-capitalize",
"keywords": [
"amp",
"util",
"strings"
],
"license": "MIT",
"main": "capitalize.js",
"repository": {
"type": "git",
"url": "git://github.com/ampersandjs/amp"
},
"scripts": {
"test": "node test.js"
}
}
1 change: 1 addition & 0 deletions modules/capitalize/sig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
capitalize(string, [upperCase]);
11 changes: 11 additions & 0 deletions modules/capitalize/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var test = require('tape');
var capitalize = require('./capitalize');


test('amp-capitalize', function (t) {
t.equal(capitalize('hi'), 'Hi', 'basic functionality');
t.equal(capitalize('hi there'), 'Hi there', 'only does first');
t.equal(capitalize(' hi'), ' hi', 'does not trim whitespace');
t.equal(capitalize('Hi', false), 'hi', 'can be used to lowercase also');
t.end();
});
6 changes: 3 additions & 3 deletions modules/to-camel-case/to-camel-case.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var isString = require('amp-is-string');
var capitalize = require('amp-capitalize');
var re1 = /([\W_\-]+\S?)/g;
var re2 = /[\W_]/g;


module.exports = function toCamelCase(string, capitalizeFirst) {
if (!isString(string)) return '';
var replaced = string.replace(re1, function (match) {
var result = string.replace(re1, function (match) {
return match.replace(re2, '').toUpperCase();
});
var first = replaced.slice(0, 1)[capitalizeFirst ? 'toUpperCase' : 'toLowerCase']();
return first + replaced.slice(1);
return capitalize(result, !!capitalizeFirst);
};

0 comments on commit 638d67b

Please sign in to comment.