camelcase
Advanced tools
Comparing version 6.1.0 to 6.2.0
@@ -11,2 +11,9 @@ declare namespace camelcase { | ||
/** | ||
Preserve the consecutive uppercase characters: `foo-BAR` → `FooBAR`. | ||
@default false | ||
*/ | ||
readonly preserveConsecutiveUppercase?: 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. | ||
@@ -22,9 +29,6 @@ | ||
//=> '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']}); | ||
@@ -67,2 +71,8 @@ //=> 'loremİpsum' | ||
camelCase('Foo-BAR', {preserveConsecutiveUppercase: true}); | ||
//=> 'fooBAR' | ||
camelCase('fooBAR', {pascalCase: true, preserveConsecutiveUppercase: true})); | ||
//=> 'FooBAR' | ||
camelCase('foo bar'); | ||
@@ -82,2 +92,5 @@ //=> 'fooBar' | ||
camelCase(['foo', 'BAR'], {pascalCase: true, preserveConsecutiveUppercase: true}) | ||
//=> 'FooBAR' | ||
camelCase('lorem-ipsum', {locale: 'en-US'}); | ||
@@ -84,0 +97,0 @@ //=> 'loremIpsum' |
32
index.js
@@ -32,2 +32,11 @@ 'use strict'; | ||
const preserveConsecutiveUppercase = input => { | ||
return input.replace(/^[\p{Lu}](?![\p{Lu}])/gu, 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)); | ||
}; | ||
const camelCase = (input, options) => { | ||
@@ -39,8 +48,7 @@ if (!(typeof input === 'string' || Array.isArray(input))) { | ||
options = { | ||
...{pascalCase: false}, | ||
pascalCase: false, | ||
preserveConsecutiveUppercase: false, | ||
...options | ||
}; | ||
const postProcess = x => options.pascalCase ? x.charAt(0).toLocaleUpperCase(options.locale) + x.slice(1) : x; | ||
if (Array.isArray(input)) { | ||
@@ -68,9 +76,15 @@ input = input.map(x => x.trim()) | ||
input = input | ||
.replace(/^[_.\- ]+/, '') | ||
.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)); | ||
input = input.replace(/^[_.\- ]+/, ''); | ||
return postProcess(input); | ||
if (options.preserveConsecutiveUppercase) { | ||
input = preserveConsecutiveUppercase(input); | ||
} else { | ||
input = input.toLocaleLowerCase(); | ||
} | ||
if (options.pascalCase) { | ||
input = input.charAt(0).toLocaleUpperCase(options.locale) + input.slice(1); | ||
} | ||
return postProcess(input, options); | ||
}; | ||
@@ -77,0 +91,0 @@ |
{ | ||
"name": "camelcase", | ||
"version": "6.1.0", | ||
"version": "6.2.0", | ||
"description": "Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: `foo-bar` → `fooBar`", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -38,2 +38,8 @@ # camelcase [![Build Status](https://travis-ci.com/sindresorhus/camelcase.svg?branch=master)](https://travis-ci.com/sindresorhus/camelcase) | ||
camelCase('Foo-BAR', {preserveConsecutiveUppercase: true}); | ||
//=> 'fooBAR' | ||
camelCase('fooBAR', {pascalCase: true, preserveConsecutiveUppercase: true})); | ||
//=> 'FooBAR' | ||
camelCase('foo bar'); | ||
@@ -53,2 +59,5 @@ //=> 'fooBar' | ||
camelCase(['foo', 'BAR'], {pascalCase: true, preserveConsecutiveUppercase: true}) | ||
//=> 'FooBAR' | ||
camelCase('lorem-ipsum', {locale: 'en-US'}); | ||
@@ -79,2 +88,9 @@ //=> 'loremIpsum' | ||
##### preserveConsecutiveUppercase | ||
Type: `boolean`\ | ||
Default: `false` | ||
Preserve the consecutive uppercase characters: `foo-BAR` → `FooBAR`. | ||
##### locale | ||
@@ -81,0 +97,0 @@ |
10385
148
129