Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add pascalCase option (#37)
  • Loading branch information
PedroMiguelSS authored and sindresorhus committed Mar 28, 2018
1 parent ee55cbe commit d9960c7
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 20 deletions.
19 changes: 12 additions & 7 deletions index.js
Expand Up @@ -29,10 +29,13 @@ function preserveCamelCase(str) {
return str;
}

module.exports = function (str) {
if (arguments.length > 1) {
str = Array.from(arguments)
.map(x => x.trim())
module.exports = function (str, opts) {
opts = Object.assign({
pascalCase: false
}, opts);

if (Array.isArray(str)) {
str = str.map(x => x.trim())
.filter(x => x.length)
.join('-');
} else {
Expand All @@ -44,11 +47,11 @@ module.exports = function (str) {
}

if (str.length === 1) {
return str.toLowerCase();
return opts.pascalCase ? str.toUpperCase() : str.toLowerCase();
}

if (/^[a-z0-9]+$/.test(str)) {
return str;
return opts.pascalCase ? str.charAt(0).toUpperCase() + str.slice(1) : str;
}

const hasUpperCase = str !== str.toLowerCase();
Expand All @@ -57,8 +60,10 @@ module.exports = function (str) {
str = preserveCamelCase(str);
}

return str
str = str
.replace(/^[_.\- ]+/, '')
.toLowerCase()
.replace(/[_.\- ]+(\w|$)/g, (m, p1) => p1.toUpperCase());

return opts.pascalCase ? str.charAt(0).toUpperCase() + str.slice(1) : str;
};
36 changes: 29 additions & 7 deletions readme.md
@@ -1,6 +1,6 @@
# camelcase [![Build Status](https://travis-ci.org/sindresorhus/camelcase.svg?branch=master)](https://travis-ci.org/sindresorhus/camelcase)

> Convert a dash/dot/underscore/space separated string to camelCase: `foo-bar``fooBar`
> Convert a dash/dot/underscore/space separated string to camelCase: `foo-bar``fooBar` or PascalCase: `foo-bar``FooBar`

## Install
Expand All @@ -24,10 +24,10 @@ camelCase('foo_bar');
camelCase('Foo-Bar');
//=> 'fooBar'

camelCase('--foo.bar');
//=> 'fooBar'
camelCase('Foo-Bar', {pascalCase: true});
//=> 'FooBar'

camelCase('__foo__bar__');
camelCase('--foo.bar', {pascalCase: false});
//=> 'fooBar'

camelCase('foo bar');
Expand All @@ -38,14 +38,36 @@ console.log(process.argv[3]);
camelCase(process.argv[3]);
//=> 'fooBar'

camelCase('foo', 'bar');
camelCase(['foo', 'bar']);
//=> 'fooBar'

camelCase('__foo__', '--bar');
//=> 'fooBar'
camelCase(['__foo__', '--bar'], {pascalCase: true});
//=> 'FooBar'
```


## API

### camelCase(input, [options])

#### input

Type: `string` `string[]`

String to convert to camel case.

#### options

Type: `Object`

##### pascalCase

Type: `boolean`<br>
Default: `false`

Uppercase the first character: `foo-bar``FooBar`


## Related

- [decamelize](https://github.com/sindresorhus/decamelize) - The inverse of this module
Expand Down
61 changes: 55 additions & 6 deletions test.js
@@ -1,7 +1,7 @@
import test from 'ava';
import m from '.';

test('camelCase', t => {
test('camelCase default - with no options object', t => {
t.is(m('foo'), 'foo');
t.is(m('foo-bar'), 'fooBar');
t.is(m('foo-bar-baz'), 'fooBarBaz');
Expand Down Expand Up @@ -31,19 +31,68 @@ test('camelCase', t => {
t.is(m('FooBar'), 'fooBar');
t.is(m('Foo'), 'foo');
t.is(m('FOO'), 'foo');
t.is(m('foo', 'bar'), 'fooBar');
t.is(m('foo', '-bar'), 'fooBar');
t.is(m('foo', '-bar', 'baz'), 'fooBarBaz');
t.is(m('', ''), '');
t.is(m(['foo', 'bar']), 'fooBar');
t.is(m(['foo', '-bar']), 'fooBar');
t.is(m(['foo', '-bar', 'baz']), 'fooBarBaz');
t.is(m(['', '']), '');
t.is(m('--'), '');
t.is(m(''), '');
t.is(m('--__--_--_'), '');
t.is(m('---_', '--', '', '-_- '), '');
t.is(m(['---_', '--', '', '-_- ']), '');
t.is(m('foo bar?'), 'fooBar?');
t.is(m('foo bar!'), 'fooBar!');
t.is(m('foo bar$'), 'fooBar$');
t.is(m('foo-bar#'), 'fooBar#');
t.is(m('XMLHttpRequest'), 'xmlHttpRequest');
t.is(m('AjaxXMLHttpRequest'), 'ajaxXmlHttpRequest');
t.is(m('Ajax-XMLHttpRequest'), 'ajaxXmlHttpRequest');
t.is(m([]), '');
});

test('camelCase with pascalCase option set to true', t => {
t.is(m('foo', {pascalCase: true}), 'Foo');
t.is(m('foo-bar', {pascalCase: true}), 'FooBar');
t.is(m('foo-bar-baz', {pascalCase: true}), 'FooBarBaz');
t.is(m('foo--bar', {pascalCase: true}), 'FooBar');
t.is(m('--foo-bar', {pascalCase: true}), 'FooBar');
t.is(m('--foo--bar', {pascalCase: true}), 'FooBar');
t.is(m('FOO-BAR', {pascalCase: true}), 'FooBar');
t.is(m('FOÈ-BAR', {pascalCase: true}), 'FoèBar');
t.is(m('-foo-bar-', {pascalCase: true}), 'FooBar');
t.is(m('--foo--bar--', {pascalCase: true}), 'FooBar');
t.is(m('foo.bar', {pascalCase: true}), 'FooBar');
t.is(m('foo..bar', {pascalCase: true}), 'FooBar');
t.is(m('..foo..bar..', {pascalCase: true}), 'FooBar');
t.is(m('foo_bar', {pascalCase: true}), 'FooBar');
t.is(m('__foo__bar__', {pascalCase: true}), 'FooBar');
t.is(m('__foo__bar__', {pascalCase: true}), 'FooBar');
t.is(m('foo bar', {pascalCase: true}), 'FooBar');
t.is(m(' foo bar ', {pascalCase: true}), 'FooBar');
t.is(m('-', {pascalCase: true}), '-');
t.is(m(' - ', {pascalCase: true}), '-');
t.is(m('fooBar', {pascalCase: true}), 'FooBar');
t.is(m('fooBar-baz', {pascalCase: true}), 'FooBarBaz');
t.is(m('foìBar-baz', {pascalCase: true}), 'FoìBarBaz');
t.is(m('fooBarBaz-bazzy', {pascalCase: true}), 'FooBarBazBazzy');
t.is(m('FBBazzy', {pascalCase: true}), 'FbBazzy');
t.is(m('F', {pascalCase: true}), 'F');
t.is(m('FooBar', {pascalCase: true}), 'FooBar');
t.is(m('Foo', {pascalCase: true}), 'Foo');
t.is(m('FOO', {pascalCase: true}), 'Foo');
t.is(m(['foo', 'bar'], {pascalCase: true}), 'FooBar');
t.is(m(['foo', '-bar'], {pascalCase: true}), 'FooBar');
t.is(m(['foo', '-bar', 'baz'], {pascalCase: true}), 'FooBarBaz');
t.is(m(['', ''], {pascalCase: true}), '');
t.is(m('--', {pascalCase: true}), '');
t.is(m('', {pascalCase: true}), '');
t.is(m('--__--_--_', {pascalCase: true}), '');
t.is(m(['---_', '--', '', '-_- '], {pascalCase: true}), '');
t.is(m('foo bar?', {pascalCase: true}), 'FooBar?');
t.is(m('foo bar!', {pascalCase: true}), 'FooBar!');
t.is(m('foo bar$', {pascalCase: true}), 'FooBar$');
t.is(m('foo-bar#', {pascalCase: true}), 'FooBar#');
t.is(m('XMLHttpRequest', {pascalCase: true}), 'XmlHttpRequest');
t.is(m('AjaxXMLHttpRequest', {pascalCase: true}), 'AjaxXmlHttpRequest');
t.is(m('Ajax-XMLHttpRequest', {pascalCase: true}), 'AjaxXmlHttpRequest');
t.is(m([], {pascalCase: true}), '');
});

0 comments on commit d9960c7

Please sign in to comment.