camelcase
Advanced tools
Comparing version 6.0.0 to 6.1.0
@@ -9,2 +9,26 @@ declare namespace camelcase { | ||
readonly pascalCase?: boolean; | ||
/** | ||
The locale parameter indicates the locale to be used to convert to upper/lower case according to any locale-specific case mappings. If multiple locales are given in an array, the best available locale is used. | ||
Default: The host environment’s current locale. | ||
@example | ||
``` | ||
import camelCase = require('camelcase'); | ||
camelCase('lorem-ipsum', {locale: 'en-US'}); | ||
//=> 'loremIpsum' | ||
camelCase('lorem-ipsum', {locale: 'tr-TR'}); | ||
//=> 'loremİpsum' | ||
camelCase('lorem-ipsum', {locale: ['en-US', 'en-GB']}); | ||
//=> 'loremIpsum' | ||
camelCase('lorem-ipsum', {locale: ['tr', 'TR', 'tr-TR']}); | ||
//=> 'loremİpsum' | ||
``` | ||
*/ | ||
readonly locale?: string | readonly string[]; | ||
} | ||
@@ -55,2 +79,5 @@ } | ||
//=> 'FooBar' | ||
camelCase('lorem-ipsum', {locale: 'en-US'}); | ||
//=> 'loremIpsum' | ||
``` | ||
@@ -57,0 +84,0 @@ */ |
20
index.js
'use strict'; | ||
const preserveCamelCase = string => { | ||
const preserveCamelCase = (string, locale) => { | ||
let isLastCharLower = false; | ||
@@ -23,5 +23,5 @@ let isLastCharUpper = false; | ||
} else { | ||
isLastCharLower = character.toLocaleLowerCase() === character && character.toLocaleUpperCase() !== character; | ||
isLastCharLower = character.toLocaleLowerCase(locale) === character && character.toLocaleUpperCase(locale) !== character; | ||
isLastLastCharUpper = isLastCharUpper; | ||
isLastCharUpper = character.toLocaleUpperCase() === character && character.toLocaleLowerCase() !== character; | ||
isLastCharUpper = character.toLocaleUpperCase(locale) === character && character.toLocaleLowerCase(locale) !== character; | ||
} | ||
@@ -43,3 +43,3 @@ } | ||
const postProcess = x => options.pascalCase ? x.charAt(0).toLocaleUpperCase() + x.slice(1) : x; | ||
const postProcess = x => options.pascalCase ? x.charAt(0).toLocaleUpperCase(options.locale) + x.slice(1) : x; | ||
@@ -59,9 +59,9 @@ if (Array.isArray(input)) { | ||
if (input.length === 1) { | ||
return options.pascalCase ? input.toLocaleUpperCase() : input.toLocaleLowerCase(); | ||
return options.pascalCase ? input.toLocaleUpperCase(options.locale) : input.toLocaleLowerCase(options.locale); | ||
} | ||
const hasUpperCase = input !== input.toLocaleLowerCase(); | ||
const hasUpperCase = input !== input.toLocaleLowerCase(options.locale); | ||
if (hasUpperCase) { | ||
input = preserveCamelCase(input); | ||
input = preserveCamelCase(input, options.locale); | ||
} | ||
@@ -71,5 +71,5 @@ | ||
.replace(/^[_.\- ]+/, '') | ||
.toLocaleLowerCase() | ||
.replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) => p1.toLocaleUpperCase()) | ||
.replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, m => m.toLocaleUpperCase()); | ||
.toLocaleLowerCase(options.locale) | ||
.replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) => p1.toLocaleUpperCase(options.locale)) | ||
.replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, m => m.toLocaleUpperCase(options.locale)); | ||
@@ -76,0 +76,0 @@ return postProcess(input); |
{ | ||
"name": "camelcase", | ||
"version": "6.0.0", | ||
"version": "6.1.0", | ||
"description": "Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: `foo-bar` → `fooBar`", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -1,2 +0,2 @@ | ||
# camelcase [![Build Status](https://travis-ci.org/sindresorhus/camelcase.svg?branch=master)](https://travis-ci.org/sindresorhus/camelcase) | ||
# camelcase [![Build Status](https://travis-ci.com/sindresorhus/camelcase.svg?branch=master)](https://travis-ci.com/sindresorhus/camelcase) | ||
@@ -13,2 +13,4 @@ > Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: `foo-bar` → `fooBar` | ||
*If you need to support Firefox, stay on version 5 as version 6 uses regex features not available in Firefox.* | ||
## Usage | ||
@@ -50,2 +52,5 @@ | ||
//=> 'FooBar' | ||
camelCase('lorem-ipsum', {locale: 'en-US'}); | ||
//=> 'loremIpsum' | ||
``` | ||
@@ -74,2 +79,25 @@ | ||
##### locale | ||
Type: `string | string[]`\ | ||
Default: The host environment’s current locale. | ||
The locale parameter indicates the locale to be used to convert to upper/lower case according to any locale-specific case mappings. If multiple locales are given in an array, the best available locale is used. | ||
```js | ||
const camelCase = require('camelcase'); | ||
camelCase('lorem-ipsum', {locale: 'en-US'}); | ||
//=> 'loremIpsum' | ||
camelCase('lorem-ipsum', {locale: 'tr-TR'}); | ||
//=> 'loremİpsum' | ||
camelCase('lorem-ipsum', {locale: ['en-US', 'en-GB']}); | ||
//=> 'loremIpsum' | ||
camelCase('lorem-ipsum', {locale: ['tr', 'TR', 'tr-TR']}); | ||
//=> 'loremİpsum' | ||
``` | ||
## camelcase for enterprise | ||
@@ -87,1 +115,2 @@ | ||
- [humanize-string](https://github.com/sindresorhus/humanize-string) - Convert a camelized/dasherized/underscored string into a humanized one | ||
- [camelcase-keys](https://github.com/sindresorhus/camelcase-keys) - Convert object keys to camel case |
9234
126
113