camelcase
Advanced tools
Comparing version 4.1.0 to 5.0.0
47
index.js
'use strict'; | ||
function preserveCamelCase(str) { | ||
const preserveCamelCase = input => { | ||
let isLastCharLower = false; | ||
@@ -8,7 +8,7 @@ let isLastCharUpper = false; | ||
for (let i = 0; i < str.length; i++) { | ||
const c = str[i]; | ||
for (let i = 0; i < input.length; i++) { | ||
const c = input[i]; | ||
if (isLastCharLower && /[a-zA-Z]/.test(c) && c.toUpperCase() === c) { | ||
str = str.substr(0, i) + '-' + str.substr(i); | ||
input = input.slice(0, i) + '-' + input.slice(i); | ||
isLastCharLower = false; | ||
@@ -19,3 +19,3 @@ isLastLastCharUpper = isLastCharUpper; | ||
} else if (isLastCharUpper && isLastLastCharUpper && /[a-zA-Z]/.test(c) && c.toLowerCase() === c) { | ||
str = str.substr(0, i - 1) + '-' + str.substr(i - 1); | ||
input = input.slice(0, i - 1) + '-' + input.slice(i - 1); | ||
isLastLastCharUpper = isLastCharUpper; | ||
@@ -31,37 +31,44 @@ isLastCharUpper = false; | ||
return str; | ||
} | ||
return input; | ||
}; | ||
module.exports = function (str) { | ||
if (arguments.length > 1) { | ||
str = Array.from(arguments) | ||
.map(x => x.trim()) | ||
module.exports = (input, options) => { | ||
options = Object.assign({ | ||
pascalCase: false | ||
}, options); | ||
const postProcess = x => options.pascalCase ? x.charAt(0).toUpperCase() + x.slice(1) : x; | ||
if (Array.isArray(input)) { | ||
input = input.map(x => x.trim()) | ||
.filter(x => x.length) | ||
.join('-'); | ||
} else { | ||
str = str.trim(); | ||
input = input.trim(); | ||
} | ||
if (str.length === 0) { | ||
if (input.length === 0) { | ||
return ''; | ||
} | ||
if (str.length === 1) { | ||
return str.toLowerCase(); | ||
if (input.length === 1) { | ||
return options.pascalCase ? input.toUpperCase() : input.toLowerCase(); | ||
} | ||
if (/^[a-z0-9]+$/.test(str)) { | ||
return str; | ||
if (/^[a-z\d]+$/.test(input)) { | ||
return postProcess(input); | ||
} | ||
const hasUpperCase = str !== str.toLowerCase(); | ||
const hasUpperCase = input !== input.toLowerCase(); | ||
if (hasUpperCase) { | ||
str = preserveCamelCase(str); | ||
input = preserveCamelCase(input); | ||
} | ||
return str | ||
input = input | ||
.replace(/^[_.\- ]+/, '') | ||
.toLowerCase() | ||
.replace(/[_.\- ]+(\w|$)/g, (m, p1) => p1.toUpperCase()); | ||
return postProcess(input); | ||
}; |
{ | ||
"name": "camelcase", | ||
"version": "4.1.0", | ||
"description": "Convert a dash/dot/underscore/space separated string to camelCase: foo-bar → fooBar", | ||
"license": "MIT", | ||
"repository": "sindresorhus/camelcase", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"camelcase", | ||
"camel-case", | ||
"camel", | ||
"case", | ||
"dash", | ||
"hyphen", | ||
"dot", | ||
"underscore", | ||
"separator", | ||
"string", | ||
"text", | ||
"convert" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"esnext": true | ||
} | ||
"name": "camelcase", | ||
"version": "5.0.0", | ||
"description": "Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: `foo-bar` → `fooBar`", | ||
"license": "MIT", | ||
"repository": "sindresorhus/camelcase", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"camelcase", | ||
"camel-case", | ||
"camel", | ||
"case", | ||
"dash", | ||
"hyphen", | ||
"dot", | ||
"underscore", | ||
"separator", | ||
"string", | ||
"text", | ||
"convert", | ||
"pascalcase", | ||
"pascal-case" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
} | ||
} |
# 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 or PascalCase: `foo-bar` → `fooBar` | ||
@@ -9,3 +9,3 @@ | ||
``` | ||
$ npm install --save camelcase | ||
$ npm install camelcase | ||
``` | ||
@@ -28,6 +28,6 @@ | ||
camelCase('--foo.bar'); | ||
//=> 'fooBar' | ||
camelCase('Foo-Bar', {pascalCase: true}); | ||
//=> 'FooBar' | ||
camelCase('__foo__bar__'); | ||
camelCase('--foo.bar', {pascalCase: false}); | ||
//=> 'fooBar' | ||
@@ -43,10 +43,32 @@ | ||
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 | ||
@@ -56,2 +78,4 @@ | ||
- [uppercamelcase](https://github.com/SamVerschueren/uppercamelcase) - Like this module, but to PascalCase instead of camelCase | ||
- [titleize](https://github.com/sindresorhus/titleize) - Capitalize every word in string | ||
- [humanize-string](https://github.com/sindresorhus/humanize-string) - Convert a camelized/dasherized/underscored string into a humanized one | ||
@@ -58,0 +82,0 @@ |
Sorry, the diff of this file is not supported yet
5089
57
82