diff --git a/bin/test-all.js b/bin/test-all.js index fcdcdb6..624b248 100644 --- a/bin/test-all.js +++ b/bin/test-all.js @@ -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'); diff --git a/bin/test-server.js b/bin/test-server.js index fb28068..78e2358 100644 --- a/bin/test-server.js +++ b/bin/test-server.js @@ -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'); diff --git a/modules.json b/modules.json index eb9035c..2d5cd71 100644 --- a/modules.json +++ b/modules.json @@ -138,6 +138,10 @@ } ], "strings": [ + { + "name": "capitalize", + "license": "amp" + }, { "name": "escape", "license": "underscore" diff --git a/modules/capitalize/LICENSE.md b/modules/capitalize/LICENSE.md new file mode 100644 index 0000000..c044826 --- /dev/null +++ b/modules/capitalize/LICENSE.md @@ -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. diff --git a/modules/capitalize/README.md b/modules/capitalize/README.md new file mode 100644 index 0000000..36fc4e0 --- /dev/null +++ b/modules/capitalize/README.md @@ -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). \ No newline at end of file diff --git a/modules/capitalize/capitalize.js b/modules/capitalize/capitalize.js new file mode 100644 index 0000000..1786a3b --- /dev/null +++ b/modules/capitalize/capitalize.js @@ -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); +}; diff --git a/modules/capitalize/doc.md b/modules/capitalize/doc.md new file mode 100644 index 0000000..e8cb537 --- /dev/null +++ b/modules/capitalize/doc.md @@ -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. diff --git a/modules/capitalize/example.js b/modules/capitalize/example.js new file mode 100644 index 0000000..88100a6 --- /dev/null +++ b/modules/capitalize/example.js @@ -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' diff --git a/modules/capitalize/package.json b/modules/capitalize/package.json new file mode 100644 index 0000000..cb5ccd5 --- /dev/null +++ b/modules/capitalize/package.json @@ -0,0 +1,32 @@ +{ + "name": "amp-capitalize", + "description": "capitalize function part of http://amp.ampersandjs.com.", + "version": "1.0.0", + "author": "Henrik Joreteg ", + "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" + } +} diff --git a/modules/capitalize/sig.js b/modules/capitalize/sig.js new file mode 100644 index 0000000..8b2c73a --- /dev/null +++ b/modules/capitalize/sig.js @@ -0,0 +1 @@ +capitalize(string, [upperCase]); diff --git a/modules/capitalize/test.js b/modules/capitalize/test.js new file mode 100644 index 0000000..530b176 --- /dev/null +++ b/modules/capitalize/test.js @@ -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(); +}); diff --git a/modules/to-camel-case/to-camel-case.js b/modules/to-camel-case/to-camel-case.js index 626cc5c..5b159bb 100644 --- a/modules/to-camel-case/to-camel-case.js +++ b/modules/to-camel-case/to-camel-case.js @@ -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); };