camelcase
Advanced tools
Comparing version 6.2.0 to 6.2.1
@@ -60,4 +60,4 @@ declare namespace camelcase { | ||
camelCase('розовый_пушистый_единороги'); | ||
//=> 'розовыйПушистыйЕдинороги' | ||
camelCase('розовый_пушистый_единорог'); | ||
//=> 'розовыйПушистыйЕдинорог' | ||
@@ -64,0 +64,0 @@ camelCase('Foo-Bar', {pascalCase: true}); |
27
index.js
'use strict'; | ||
const UPPERCASE = /[\p{Lu}]/u; | ||
const LOWERCASE = /[\p{Ll}]/u; | ||
const LEADING_CAPITAL = /^[\p{Lu}](?![\p{Lu}])/gu; | ||
const IDENTIFIER = /([\p{Alpha}\p{N}_]|$)/u; | ||
const SEPARATORS = /[_.\- ]+/; | ||
const LEADING_SEPARATORS = new RegExp('^' + SEPARATORS.source); | ||
const SEPARATORS_AND_IDENTIFIER = new RegExp(SEPARATORS.source + IDENTIFIER.source, 'gu'); | ||
const NUMBERS_AND_IDENTIFIER = new RegExp('\\d+' + IDENTIFIER.source, 'gu'); | ||
const preserveCamelCase = (string, locale) => { | ||
@@ -11,3 +21,3 @@ let isLastCharLower = false; | ||
if (isLastCharLower && /[\p{Lu}]/u.test(character)) { | ||
if (isLastCharLower && UPPERCASE.test(character)) { | ||
string = string.slice(0, i) + '-' + string.slice(i); | ||
@@ -18,3 +28,3 @@ isLastCharLower = false; | ||
i++; | ||
} else if (isLastCharUpper && isLastLastCharUpper && /[\p{Ll}]/u.test(character)) { | ||
} else if (isLastCharUpper && isLastLastCharUpper && LOWERCASE.test(character)) { | ||
string = string.slice(0, i - 1) + '-' + string.slice(i - 1); | ||
@@ -35,8 +45,13 @@ isLastLastCharUpper = isLastCharUpper; | ||
const preserveConsecutiveUppercase = input => { | ||
return input.replace(/^[\p{Lu}](?![\p{Lu}])/gu, m1 => m1.toLowerCase()); | ||
LEADING_CAPITAL.lastIndex = 0; | ||
return input.replace(LEADING_CAPITAL, m1 => m1.toLowerCase()); | ||
}; | ||
const postProcess = (input, options) => { | ||
return input.replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) => p1.toLocaleUpperCase(options.locale)) | ||
.replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, m => m.toLocaleUpperCase(options.locale)); | ||
SEPARATORS_AND_IDENTIFIER.lastIndex = 0; | ||
NUMBERS_AND_IDENTIFIER.lastIndex = 0; | ||
return input.replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => identifier.toLocaleUpperCase(options.locale)) | ||
.replace(NUMBERS_AND_IDENTIFIER, m => m.toLocaleUpperCase(options.locale)); | ||
}; | ||
@@ -77,3 +92,3 @@ | ||
input = input.replace(/^[_.\- ]+/, ''); | ||
input = input.replace(LEADING_SEPARATORS, ''); | ||
@@ -80,0 +95,0 @@ if (options.preserveConsecutiveUppercase) { |
{ | ||
"name": "camelcase", | ||
"version": "6.2.0", | ||
"version": "6.2.1", | ||
"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.com/sindresorhus/camelcase.svg?branch=master)](https://travis-ci.com/sindresorhus/camelcase) | ||
# camelcase | ||
@@ -7,2 +7,4 @@ > Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: `foo-bar` → `fooBar` | ||
If you use this on untrusted user input, don't forget to limit the length to something reasonable. | ||
## Install | ||
@@ -14,3 +16,3 @@ | ||
*If you need to support Firefox, stay on version 5 as version 6 uses regex features not available in Firefox.* | ||
*If you need to support Firefox < 78, stay on version 5 as version 6 uses regex features not available in Firefox < 78.* | ||
@@ -31,4 +33,4 @@ ## Usage | ||
camelCase('розовый_пушистый_единороги'); | ||
//=> 'розовыйПушистыйЕдинороги' | ||
camelCase('розовый_пушистый_единорог'); | ||
//=> 'розовыйПушистыйЕдинорог' | ||
@@ -35,0 +37,0 @@ camelCase('Foo-Bar', {pascalCase: true}); |
10892
159
131