camelcase
Advanced tools
Comparing version 5.3.1 to 6.0.0
@@ -12,53 +12,51 @@ declare namespace camelcase { | ||
declare const camelcase: { | ||
/** | ||
Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: `foo-bar` → `fooBar`. | ||
/** | ||
Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: `foo-bar` → `fooBar`. | ||
@param input - String to convert to camel case. | ||
Correctly handles Unicode strings. | ||
@example | ||
``` | ||
import camelCase = require('camelcase'); | ||
@param input - String to convert to camel case. | ||
camelCase('foo-bar'); | ||
//=> 'fooBar' | ||
@example | ||
``` | ||
import camelCase = require('camelcase'); | ||
camelCase('foo_bar'); | ||
//=> 'fooBar' | ||
camelCase('foo-bar'); | ||
//=> 'fooBar' | ||
camelCase('Foo-Bar'); | ||
//=> 'fooBar' | ||
camelCase('foo_bar'); | ||
//=> 'fooBar' | ||
camelCase('Foo-Bar', {pascalCase: true}); | ||
//=> 'FooBar' | ||
camelCase('Foo-Bar'); | ||
//=> 'fooBar' | ||
camelCase('--foo.bar', {pascalCase: false}); | ||
//=> 'fooBar' | ||
camelCase('розовый_пушистый_единороги'); | ||
//=> 'розовыйПушистыйЕдинороги' | ||
camelCase('foo bar'); | ||
//=> 'fooBar' | ||
camelCase('Foo-Bar', {pascalCase: true}); | ||
//=> 'FooBar' | ||
console.log(process.argv[3]); | ||
//=> '--foo-bar' | ||
camelCase(process.argv[3]); | ||
//=> 'fooBar' | ||
camelCase('--foo.bar', {pascalCase: false}); | ||
//=> 'fooBar' | ||
camelCase(['foo', 'bar']); | ||
//=> 'fooBar' | ||
camelCase('foo bar'); | ||
//=> 'fooBar' | ||
camelCase(['__foo__', '--bar'], {pascalCase: true}); | ||
//=> 'FooBar' | ||
``` | ||
*/ | ||
(input: string | ReadonlyArray<string>, options?: camelcase.Options): string; | ||
console.log(process.argv[3]); | ||
//=> '--foo-bar' | ||
camelCase(process.argv[3]); | ||
//=> 'fooBar' | ||
// TODO: Remove this for the next major release, refactor the whole definition to: | ||
// declare function camelcase( | ||
// input: string | ReadonlyArray<string>, | ||
// options?: camelcase.Options | ||
// ): string; | ||
// export = camelcase; | ||
default: typeof camelcase; | ||
}; | ||
camelCase(['foo', 'bar']); | ||
//=> 'fooBar' | ||
camelCase(['__foo__', '--bar'], {pascalCase: true}); | ||
//=> 'FooBar' | ||
``` | ||
*/ | ||
declare function camelcase( | ||
input: string | readonly string[], | ||
options?: camelcase.Options | ||
): string; | ||
export = camelcase; |
27
index.js
@@ -11,3 +11,3 @@ 'use strict'; | ||
if (isLastCharLower && /[a-zA-Z]/.test(character) && character.toUpperCase() === character) { | ||
if (isLastCharLower && /[\p{Lu}]/u.test(character)) { | ||
string = string.slice(0, i) + '-' + string.slice(i); | ||
@@ -18,3 +18,3 @@ isLastCharLower = false; | ||
i++; | ||
} else if (isLastCharUpper && isLastLastCharUpper && /[a-zA-Z]/.test(character) && character.toLowerCase() === character) { | ||
} else if (isLastCharUpper && isLastLastCharUpper && /[\p{Ll}]/u.test(character)) { | ||
string = string.slice(0, i - 1) + '-' + string.slice(i - 1); | ||
@@ -25,5 +25,5 @@ isLastLastCharUpper = isLastCharUpper; | ||
} else { | ||
isLastCharLower = character.toLowerCase() === character && character.toUpperCase() !== character; | ||
isLastCharLower = character.toLocaleLowerCase() === character && character.toLocaleUpperCase() !== character; | ||
isLastLastCharUpper = isLastCharUpper; | ||
isLastCharUpper = character.toUpperCase() === character && character.toLowerCase() !== character; | ||
isLastCharUpper = character.toLocaleUpperCase() === character && character.toLocaleLowerCase() !== character; | ||
} | ||
@@ -40,7 +40,8 @@ } | ||
options = Object.assign({ | ||
pascalCase: false | ||
}, options); | ||
options = { | ||
...{pascalCase: false}, | ||
...options | ||
}; | ||
const postProcess = x => options.pascalCase ? x.charAt(0).toUpperCase() + x.slice(1) : x; | ||
const postProcess = x => options.pascalCase ? x.charAt(0).toLocaleUpperCase() + x.slice(1) : x; | ||
@@ -60,6 +61,6 @@ if (Array.isArray(input)) { | ||
if (input.length === 1) { | ||
return options.pascalCase ? input.toUpperCase() : input.toLowerCase(); | ||
return options.pascalCase ? input.toLocaleUpperCase() : input.toLocaleLowerCase(); | ||
} | ||
const hasUpperCase = input !== input.toLowerCase(); | ||
const hasUpperCase = input !== input.toLocaleLowerCase(); | ||
@@ -72,5 +73,5 @@ if (hasUpperCase) { | ||
.replace(/^[_.\- ]+/, '') | ||
.toLowerCase() | ||
.replace(/[_.\- ]+(\w|$)/g, (_, p1) => p1.toUpperCase()) | ||
.replace(/\d+(\w|$)/g, m => m.toUpperCase()); | ||
.toLocaleLowerCase() | ||
.replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) => p1.toLocaleUpperCase()) | ||
.replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, m => m.toLocaleUpperCase()); | ||
@@ -77,0 +78,0 @@ return postProcess(input); |
{ | ||
"name": "camelcase", | ||
"version": "5.3.1", | ||
"version": "6.0.0", | ||
"description": "Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: `foo-bar` → `fooBar`", | ||
"license": "MIT", | ||
"repository": "sindresorhus/camelcase", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=10" | ||
}, | ||
@@ -40,5 +41,5 @@ "scripts": { | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.1", | ||
"xo": "^0.24.0" | ||
"tsd": "^0.11.0", | ||
"xo": "^0.28.3" | ||
} | ||
} |
@@ -5,16 +5,4 @@ # camelcase [![Build Status](https://travis-ci.org/sindresorhus/camelcase.svg?branch=master)](https://travis-ci.org/sindresorhus/camelcase) | ||
--- | ||
Correctly handles Unicode strings. | ||
<div align="center"> | ||
<b> | ||
<a href="https://tidelift.com/subscription/pkg/npm-camelcase?utm_source=npm-camelcase&utm_medium=referral&utm_campaign=readme">Get professional support for 'camelcase' with a Tidelift subscription</a> | ||
</b> | ||
<br> | ||
<sub> | ||
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. | ||
</sub> | ||
</div> | ||
--- | ||
## Install | ||
@@ -26,3 +14,2 @@ | ||
## Usage | ||
@@ -42,2 +29,5 @@ | ||
camelCase('розовый_пушистый_единороги'); | ||
//=> 'розовыйПушистыйЕдинороги' | ||
camelCase('Foo-Bar', {pascalCase: true}); | ||
@@ -64,10 +54,9 @@ //=> 'FooBar' | ||
## API | ||
### camelCase(input, [options]) | ||
### camelCase(input, options?) | ||
#### input | ||
Type: `string` `string[]` | ||
Type: `string | string[]` | ||
@@ -78,7 +67,7 @@ String to convert to camel case. | ||
Type: `Object` | ||
Type: `object` | ||
##### pascalCase | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `false` | ||
@@ -88,8 +77,8 @@ | ||
## camelcase for enterprise | ||
## Security | ||
Available as part of the Tidelift Subscription. | ||
To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. | ||
The maintainers of camelcase and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-camelcase?utm_source=npm-camelcase&utm_medium=referral&utm_campaign=enterprise&utm_term=repo) | ||
## Related | ||
@@ -101,6 +90,1 @@ | ||
- [humanize-string](https://github.com/sindresorhus/humanize-string) - Convert a camelized/dasherized/underscored string into a humanized one | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
Sorry, the diff of this file is not supported yet
7415
107
84