-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add 'capitalize' and use it in 'to-camel-case'. Closes #47.
- Loading branch information
1 parent
77ea42a
commit 638d67b
Showing
12 changed files
with
95 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
capitalize(string, [upperCase]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |